From 98391681ad283a631a9deafa6922eccc2cbf64f1 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 19 Nov 2021 11:06:02 +0000 Subject: [PATCH 1/2] Extract non-generic part of `push_topic` --- crates/env/src/engine/on_chain/impls.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 31a9cd5230..29c6a68e03 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -151,6 +151,21 @@ where { let mut split = self.scoped_buffer.split(); let encoded = split.take_encoded(topic_value); + let result = Self::push_topic_encoded(encoded); + self.scoped_buffer.append_encoded(&result); + } + + fn output(mut self) -> Self::Output { + let encoded_topics = self.scoped_buffer.take_appended(); + (self.scoped_buffer, encoded_topics) + } +} + +impl<'a, E> TopicsBuilder<'a, E> +where + E: Environment, +{ + fn push_topic_encoded(encoded: &mut [u8]) -> ::Hash { let len_encoded = encoded.len(); let mut result = ::Hash::clear(); let len_result = result.as_ref().len(); @@ -162,12 +177,7 @@ where let copy_len = core::cmp::min(hash_output.len(), len_result); result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]); } - self.scoped_buffer.append_encoded(&result); - } - - fn output(mut self) -> Self::Output { - let encoded_topics = self.scoped_buffer.take_appended(); - (self.scoped_buffer, encoded_topics) + result } } From f73d8c8950e9308a1153f0a21655d073700153ee Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 19 Nov 2021 12:50:08 +0000 Subject: [PATCH 2/2] Move to inner fn --- crates/env/src/engine/on_chain/impls.rs | 37 +++++++++++-------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 29c6a68e03..990cabceb6 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -149,9 +149,24 @@ where where T: scale::Encode, { + fn inner(encoded: &mut [u8]) -> ::Hash { + let len_encoded = encoded.len(); + let mut result = ::Hash::clear(); + let len_result = result.as_ref().len(); + if len_encoded <= len_result { + result.as_mut()[..len_encoded].copy_from_slice(encoded); + } else { + let mut hash_output = ::Type::default(); + ::hash(encoded, &mut hash_output); + let copy_len = core::cmp::min(hash_output.len(), len_result); + result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]); + } + result + } + let mut split = self.scoped_buffer.split(); let encoded = split.take_encoded(topic_value); - let result = Self::push_topic_encoded(encoded); + let result = inner::(encoded); self.scoped_buffer.append_encoded(&result); } @@ -161,26 +176,6 @@ where } } -impl<'a, E> TopicsBuilder<'a, E> -where - E: Environment, -{ - fn push_topic_encoded(encoded: &mut [u8]) -> ::Hash { - let len_encoded = encoded.len(); - let mut result = ::Hash::clear(); - let len_result = result.as_ref().len(); - if len_encoded <= len_result { - result.as_mut()[..len_encoded].copy_from_slice(encoded); - } else { - let mut hash_output = ::Type::default(); - ::hash(encoded, &mut hash_output); - let copy_len = core::cmp::min(hash_output.len(), len_result); - result.as_mut()[0..copy_len].copy_from_slice(&hash_output[0..copy_len]); - } - result - } -} - impl EnvInstance { /// Returns a new scoped buffer for the entire scope of the static 16 kB buffer. fn scoped_buffer(&mut self) -> ScopedBuffer {