swimos/swim-rust

Work around unavoidable clippy lint on some event handlers.

horned-sphere opened this issue · 0 comments

Consider an agent:

#[derive(AgentLaneModel)]
#[projections]
pub struct ExampleAgent {
    command: CommandLane<String>,
}

Currently, an event handler for its lane must be writen as:

#[on_command(command)]
    pub fn on_update(
        &self,
        context: HandlerContext<ExampleAgent>,
        value: &String,
    ) -> impl EventHandler<ExampleAgent> {
        let s = value.clone();
        context.effect(move || println!("Commanded: {}", s))
    }

This will cause clippy to complain that value should have type &str. Making this change will currently cause the generated code to no compile.

This can be fixed by replacing the use of FnHandler<F> with a new type:

struct BorrowHandler<F, U> {
    f: F,
    _borrow_type: PhantomData<fn(&U)>,
}

Then for a lane of type T, the the event handler trait can be implemented for BorrowHandler<F, U> where T: Borrow<U>. The lifecycle macro can get the type U from the signature of the event handler (this is already done in the code but not used yet).

This needs to be done for all of the event handler types (which will be quite long winded).

A similar construction should work for map lanes.