async-trait icon indicating copy to clipboard operation
async-trait copied to clipboard

Readme Lifetime documentation

Open martinitus opened this issue 5 years ago • 0 comments

Hey there! ❤️

I'm just playing around with rust and found the following:

#[async_trait]
trait Foo{
    fn bar(&self)->isize;
}

// no #[async_trait] here intentionally I want more controll within my method bodies.
struct MyFoo{}
impl Foo for MyFoo{
    // essentially i want a blocking computation and return a ready future.
    fn bar(&self)->... {
        let value :isize = self.do_something();
        Box::pin(async{value})
    }
}

I managed to get this work with the following by looking at generated code with macros expanded (thanks to your other beautiful tool 👍 )

impl Foo for MyFoo{
 fn bar<'life0, 'async_trait>(&'life0 self) -> Pin<Box<dyn Future<Output =isize> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait,
    {
         let value :isize = self.do_something();
        Box::pin(async { value })
    }
}

I just wonder if there is an easier solution using other annotations from your package. Also i think the readme part showing the expanded code is slightly of (no 'life0 / only 'async_trait lifetime).

Regards and HtH, Marti

martinitus avatar Sep 08 '20 20:09 martinitus