diff --git a/CHANGELOG.md b/CHANGELOG.md index c076b26..4daddbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.5.13 + +Added support for specifying `let mut : ` when using the `#[substreams::handlers::map]` macros, this enables in-place trimming of received data. + ## 0.5.12 Added back some lost binary operations on `BigInt` like `BigInt - BigInt` for example. diff --git a/substreams-macro/src/handler.rs b/substreams-macro/src/handler.rs index 7562782..6dcd299 100644 --- a/substreams-macro/src/handler.rs +++ b/substreams-macro/src/handler.rs @@ -99,7 +99,13 @@ pub fn main(item: TokenStream, module_type: ModuleType) -> TokenStream { } else if input_obj.is_string { proto_decodings.push(quote! { let #var_name: String = std::mem::ManuallyDrop::new(unsafe {String::from_raw_parts(#var_ptr, #var_len, #var_len)}).to_string(); }); } else { - proto_decodings.push(quote! { let #var_name: #argument_type = substreams::proto::decode_ptr(#var_ptr, #var_len).unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", #var_len, stringify!(#argument_type))); }) + let mutability = if v.mutability.is_some() { + quote! { mut } + } else { + quote! {} + }; + + proto_decodings.push(quote! { let #mutability #var_name: #argument_type = substreams::proto::decode_ptr(#var_ptr, #var_len).unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", #var_len, stringify!(#argument_type))); }) } } _ => { diff --git a/substreams-macro/src/lib.rs b/substreams-macro/src/lib.rs index 0f7a84d..3fdd3ee 100644 --- a/substreams-macro/src/lib.rs +++ b/substreams-macro/src/lib.rs @@ -56,6 +56,35 @@ mod test { ); } + #[test] + fn test_map_mut() { + let item = quote! { + fn map_transfers(mut blk: eth::Block) -> pb::Custom { + unimplemented!("do something"); + } + }; + + assert_ast_eq( + main(item, ModuleType::Map).into(), + quote! { + #[no_mangle] + pub extern "C" fn map_transfers(blk_ptr: *mut u8, blk_len: usize) { + substreams::register_panic_hook(); + let func = || -> pb::Custom { + let mut blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len) + .unwrap_or_else(|_| panic!("Unable to decode Protobuf data ({} bytes) to '{}' message's struct", blk_len, stringify!(eth::Block))); + let result = { + unimplemented!("do something"); + }; + result + }; + let result = func(); + substreams::output(result); + } + }, + ); + } + #[test] fn test_map_option() { let item = quote! {