You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use async_trait::async_trait;use futures::executor::block_on;pubstructSomething<'a>{name:&'astr,}#[async_trait]pubtraitAbstract<'a>{asyncfnhello(thing:Something<'a>) -> String;asyncfnhello_ref(thing:Something<'a>) -> &'astr;}pubstructReal();#[async_trait]impl<'a>Abstract<'a>forReal{asyncfnhello(thing:Something<'a>) -> String{String::from(thing.name)}asyncfnhello_ref(thing:Something<'a>) -> &'astr{
thing.name}}fnmain(){let thing = Something{name:"name"};let name = block_on(Real::hello(thing));println!("name: {}", name);}
But it will throw an Error:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in function call due to conflicting requirements
--> src\main.rs:14:1
|
14 | #[async_trait]
| ^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl at 15:7...
--> src\main.rs:15:7
|
15 | impl <'a> Abstract<'a> for Real {
| ^^
= note: ...so that the expression is assignable:
expected Something<'_>
found Something<'a>
= note: but, the lifetime must be valid for the static lifetime...
= note: ...so that the expression is assignable:
expected std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = std::string::String> + std::marker::Send + 'static)>>
found std::pin::Pin<std::boxed::Box<dyn core::future::future::Future<Output = std::string::String> + std::marker::Send>>
I wonder am I do anything wrong? How to set the lifetime correctly?
The text was updated successfully, but these errors were encountered:
I want to write a trait with lifetime, say:
But it will throw an Error:
I wonder am I do anything wrong? How to set the lifetime correctly?
The text was updated successfully, but these errors were encountered: