streamingfast / substreams-rs

7 stars 3 forks source link

Cargo clippy reports modules using the `map` handler with params as being unsafe #19

Open beeb opened 1 year ago

beeb commented 1 year ago

When using params as the first argument to a map module, clippy will report:

error: this public function might dereference a raw pointer but is not marked `unsafe`
#[substreams::handlers::map]
pub fn map_pools_created(params: String, block: Block) -> Result<Pools, Error> {
     // ...
}

This doesn't prevent code compilation but is definitely quite annoying. Adding the unsafe keyword to my module function definition doesn't solve the problem, because (I suspect) the underlying macro produces output that doesn't have the unsafe keyword.

I see the following when I expand the map proc macro:

#[no_mangle]
pub extern "C" fn map_pools_created(
    params_ptr: *mut u8,
    params_len: usize,
    block_ptr: *mut u8,
    block_len: usize,
) {
    substreams::register_panic_hook();
    let func = || -> Result<Pools, Error> {
        let params: String = std::mem::ManuallyDrop::new(unsafe {
            String::from_raw_parts(params_ptr, params_len, params_len)
        })
        .to_string();
    // ...
}

which indeed contains some unsafe code.

maoueh commented 10 months ago

Is there a way that in the macro we disable clippy for this line?