dtolnay / async-trait

Type erasure for async trait methods
Apache License 2.0
1.84k stars 85 forks source link

Error using async trait helper methods with async trait bound on associated type #38

Closed inanna-malick closed 5 years ago

inanna-malick commented 5 years ago

I'm not experienced enough with async/await to be sure that this is a library issue and not a user error, but I didn't expect to see this type of type error in this use case. (minimized example)

#[async_trait]
pub trait TestCap {
    async fn foo(&self) -> String;
}

#[async_trait]
pub trait HasTestCap {
    type Output: TestCap;

    fn test_caps(&self) -> &Self::Output;

    async fn test_foo(&self) -> String {
        self.test_caps().foo()
    }
}

results in:

error[E0271]: type mismatch resolving `<impl core::future::future::Future as core::future::future::Future>::Output == std::string::String`
  --> src/capabilities/mod.rs:26:33
   |
26 |     async fn test_foo(&self) -> String {
   |                                 ^^^^^^ expected struct `std::pin::Pin`, found struct `std::string::String`
   |
   = note: expected type `std::pin::Pin<std::boxed::Box<dyn core::future::future::Future<Output = std::string::String> + std::marker::Send>>`
              found type `std::string::String`
   = note: the return type of a function must have a statically known size
dtolnay commented 5 years ago
inanna-malick commented 5 years ago

awesome, thanks - I had assumed you didn't need to .await unless you wanted to use the result of an async function, such that (eg) having an async function as the last expression of an async function wouldn't require await