Attribute Macro substreams::handlers::map

source ·
#[map]
Expand description

Marks function to setup substreams map handler WASM boilerplate

§Usage with Result<T, …>


#[substreams::handlers::map]
fn map_handler(blk: eth::Block) -> Result<proto::Custom, substreams::errors::Error> {
    unimplemented!("do something");
}

Equivalent code not using #[substreams::handlers::map]


#[no_mangle]
pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) {
    substreams::register_panic_hook();
    let func = || -> Result<proto::Custom, substreams::errors::Error> {
        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap();
        {
            unimplemented!("do something");
        }
    };
    let result = func();
    if result.is_err() {
        panic!(result.err().unwrap())
    }
    substreams::output(substreams::proto::encode(&result.unwrap()).unwrap());
}

§Usage with Option


#[substreams::handlers::map]
fn map_handler(blk: eth::Block) -> Option<proto::Custom> {
    unimplemented!("do something");
}

Equivalent code not using #[substreams::handlers::map]


#[no_mangle]
pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) {
    substreams::register_panic_hook();
    let func = || -> Option<proto::Custom> {
        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap();
        {
            unimplemented!("do something");
        }
    };
    let result = func();
    if result.is_none() {
        panic!("None returned from map function")
    }
    substreams::output(result.unwrap());
}

§Usage with T


#[substreams::handlers::map]
fn map_handler(blk: eth::Block) -> proto::Custom {
    unimplemented!("do something");
}

Equivalent code not using #[substreams::handlers::map]


#[no_mangle]
pub extern "C" fn map_handler(blk_ptr: *mut u8, blk_len: usize) {
    substreams::register_panic_hook();
    let func = || -> proto::Custom {
        let blk: eth::Block = substreams::proto::decode_ptr(blk_ptr, blk_len).unwrap();
        {
            unimplemented!("do something");
        }
    };
    let result = func();
    substreams::output(result);
}