Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cannot infer an appropriate lifetime for lifetime parameter '_ in function call due to conflicting requirements

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 = ()> + '_>>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading