dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.81k stars 84 forks source link

Readme Lifetime documentation #127

Closed martinitus closed 1 year ago

martinitus commented 4 years ago

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

dtolnay commented 1 year ago

Using #[async_trait] on the trait and not on the impl is not a supported use of this library. The readme indicates it needs to be written on both.