Skip to content

Commit 081e799

Browse files
committed
Add Event::encode_data_to
1 parent 1f6baa0 commit 081e799

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,7 @@ impl TypedEnvBackend for EnvInstance {
992992
.chunks_exact(32)
993993
.map(|c| c.try_into().unwrap())
994994
.collect::<ink_prelude::vec::Vec<[u8; 32]>>();
995-
let enc_data = scope.take_encoded_with(|buffer| {
996-
let encoded = event.encode_data();
997-
let len = encoded.len();
998-
// NOTE: panics if buffer isn't large enough.
999-
// This behavior is similar to `ScopedBuffer::take_encoded`.
1000-
buffer[..len].copy_from_slice(&encoded);
1001-
len
1002-
});
995+
let enc_data = scope.take_encoded_with(|buffer| event.encode_data_to(buffer));
1003996

1004997
ext::deposit_event(&enc_topics[..], enc_data);
1005998
}

crates/env/src/event.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ pub trait Event<Abi = crate::DefaultAbi>: AbiEncodeWith<Abi> {
252252

253253
/// ABI encode the dynamic data of this event.
254254
fn encode_data(&self) -> ink_prelude::vec::Vec<u8>;
255+
256+
/// ABI encode the dynamic data of this event into the given buffer, and returns the
257+
/// number of bytes written.
258+
fn encode_data_to(&self, buffer: &mut [u8]) -> usize;
255259
}
256260

257261
mod private {

crates/ink/macro/src/event/mod.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,15 @@ fn event_derive_struct(s: synstructure::Structure) -> syn::Result<TokenStream2>
204204
}
205205
);
206206

207-
let encode_data = match abi {
208-
Abi::Ink => quote! {
209-
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
210-
},
207+
let (encode_data, encode_data_to) = match abi {
208+
Abi::Ink => (
209+
quote! {
210+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
211+
},
212+
quote! {
213+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
214+
}
215+
),
211216
Abi::Sol => {
212217
// For Solidity ABI encoding, only un-indexed fields are encoded as data.
213218
let data_field_tys = data_fields.iter().map(|field| {
@@ -217,15 +222,24 @@ fn event_derive_struct(s: synstructure::Structure) -> syn::Result<TokenStream2>
217222
let data_field_values = data_fields.iter().map(|field| {
218223
&field.binding
219224
});
220-
quote! {
221-
match self {
222-
#pat => {
223-
::ink::sol::encode_sequence::<( #( #data_field_tys, )* )>(
224-
&( #( #data_field_values, )* ),
225-
)
225+
let ty = quote! { ( #( #data_field_tys, )* ) };
226+
let val = quote! { ( #( #data_field_values, )* ) };
227+
(
228+
quote! {
229+
match self {
230+
#pat => {
231+
::ink::sol::encode_sequence::<#ty>(&#val)
232+
}
233+
}
234+
},
235+
quote! {
236+
match self {
237+
#pat => {
238+
::ink::sol::encode_sequence_to::<#ty>(&#val, buffer)
239+
}
226240
}
227241
}
228-
}
242+
)
229243
},
230244
};
231245

@@ -248,6 +262,10 @@ fn event_derive_struct(s: synstructure::Structure) -> syn::Result<TokenStream2>
248262
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
249263
#encode_data
250264
}
265+
266+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
267+
#encode_data_to
268+
}
251269
})
252270
}))
253271
}

crates/ink/macro/src/tests/event.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ fn unit_struct_works() {
5757
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
5858
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
5959
}
60+
61+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
62+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
63+
}
6064
}
6165
};
6266
}
@@ -98,6 +102,10 @@ fn unit_struct_anonymous_has_no_topics() {
98102
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
99103
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
100104
}
105+
106+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
107+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
108+
}
101109
}
102110
};
103111
} no_build
@@ -143,6 +151,10 @@ fn struct_with_fields_no_topics() {
143151
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
144152
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
145153
}
154+
155+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
156+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
157+
}
146158
}
147159
};
148160
}
@@ -192,6 +204,10 @@ fn struct_with_fields_and_some_topics() {
192204
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
193205
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
194206
}
207+
208+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
209+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
210+
}
195211
}
196212
};
197213
} no_build
@@ -234,6 +250,10 @@ fn custom_signature_topic() {
234250
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
235251
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
236252
}
253+
254+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
255+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
256+
}
237257
}
238258
};
239259
} no_build
@@ -276,6 +296,10 @@ fn name_override_works() {
276296
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
277297
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
278298
}
299+
300+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
301+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
302+
}
279303
}
280304
};
281305
} no_build
@@ -319,6 +343,10 @@ fn custom_signature_topic_precedence() {
319343
fn encode_data(&self) -> ::ink::prelude::vec::Vec<::core::primitive::u8> {
320344
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_with(self)
321345
}
346+
347+
fn encode_data_to(&self, buffer: &mut [::core::primitive::u8]) -> ::core::primitive::usize {
348+
::ink::abi::AbiEncodeWith::<::ink::abi::Ink>::encode_to_slice(self, buffer)
349+
}
322350
}
323351
};
324352
} no_build

0 commit comments

Comments
 (0)