Skip to content

Commit d76b2f1

Browse files
authored
Extract non-generic part of push_topic to reduce code size (#1026)
* Extract non-generic part of `push_topic` * Move to inner fn
1 parent 83448d4 commit d76b2f1

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

crates/env/src/engine/on_chain/impls.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,24 @@ where
149149
where
150150
T: scale::Encode,
151151
{
152+
fn inner<E: Environment>(encoded: &mut [u8]) -> <E as Environment>::Hash {
153+
let len_encoded = encoded.len();
154+
let mut result = <E as Environment>::Hash::clear();
155+
let len_result = result.as_ref().len();
156+
if len_encoded <= len_result {
157+
result.as_mut()[..len_encoded].copy_from_slice(encoded);
158+
} else {
159+
let mut hash_output = <Blake2x256 as HashOutput>::Type::default();
160+
<Blake2x256 as CryptoHash>::hash(encoded, &mut hash_output);
161+
let copy_len = core::cmp::min(hash_output.len(), len_result);
162+
result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]);
163+
}
164+
result
165+
}
166+
152167
let mut split = self.scoped_buffer.split();
153168
let encoded = split.take_encoded(topic_value);
154-
let len_encoded = encoded.len();
155-
let mut result = <E as Environment>::Hash::clear();
156-
let len_result = result.as_ref().len();
157-
if len_encoded <= len_result {
158-
result.as_mut()[..len_encoded].copy_from_slice(encoded);
159-
} else {
160-
let mut hash_output = <Blake2x256 as HashOutput>::Type::default();
161-
<Blake2x256 as CryptoHash>::hash(encoded, &mut hash_output);
162-
let copy_len = core::cmp::min(hash_output.len(), len_result);
163-
result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]);
164-
}
169+
let result = inner::<E>(encoded);
165170
self.scoped_buffer.append_encoded(&result);
166171
}
167172

0 commit comments

Comments
 (0)