i have a vector of a struct
let list = vec![
ListenerList {
listener: Pattern {
cmd: "sum"
},
handler: |listener, connection| Box::pin(simple_handler(listener, connection))
}
];
here is my struct
pub struct ListenerList<'a>{
pub listener: Pattern<'a>,
pub handler: fn(Subscription, &Connection) -> Pin<Box<dyn futures::Future<Output = ()>>>
}
and here is my simple_handler function
async fn simple_handler(listener: Subscription, connection: &Connection){
println!("work?")
}
when i compile it i get this error
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter '_ in function call due to conflicting requirements
--> src/main.rs:19:54
|
19 | handler: |listener, connection| Box::pin(simple_handler(listener, connection))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
>Solution :
The problem is in the definition of the handler field of ListenerList, which has this type:
fn(Subscription, &Connection) -> Pin<Box<dyn futures::Future<Output = ()>>>
Due to Rust’s default trait object lifetime rules, it desugars to the trait object getting a 'static bound, which we don’t want:
fn(Subscription, &Connection) -> Pin<Box<dyn futures::Future<Output = ()> + 'static>>
To fix this, specify the lifetime as '_:
fn(Subscription, &Connection) -> Pin<Box<dyn futures::Future<Output = ()> + '_>>