Skip to content

Commit

Permalink
Added support for specifying let mut <param>: <Type> when using the…
Browse files Browse the repository at this point in the history
… `#[substreams::handlers::map]` macros.

This enables in-place trimming of received data.
  • Loading branch information
maoueh committed Feb 14, 2024
1 parent f24b7c9 commit b289e21
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <param>: <Type>` 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.
Expand Down
8 changes: 7 additions & 1 deletion substreams-macro/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))); })
}
}
_ => {
Expand Down
29 changes: 29 additions & 0 deletions substreams-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down

0 comments on commit b289e21

Please sign in to comment.