Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default impl for methods with impl trait arguments? #126

Closed
wfraser opened this issue Sep 1, 2020 · 2 comments · Fixed by #143
Closed

default impl for methods with impl trait arguments? #126

wfraser opened this issue Sep 1, 2020 · 2 comments · Fixed by #143

Comments

@wfraser
Copy link

wfraser commented Sep 1, 2020

Given the code:

use async_trait::async_trait; // 0.1.36
use tokio; // 0.2.21

#[async_trait]
trait MyTrait {
    async fn blah(&self, values: impl Iterator<Item = i32> + Send + 'async_trait) {
        println!("this is the default");
    }
}

struct S;

#[async_trait]
impl MyTrait for S {
    async fn blah(&self, values: impl Iterator<Item = i32> + Send + 'async_trait) {
        for value in values {
            println!("{}", value);
        }
    }
}

#[tokio::main]
async fn main() {
    let s = S;
    s.blah([1, 2, 3].iter().cloned()).await;
}

this fails to compile with the error:

error[E0632]: cannot provide explicit generic arguments when `impl Trait` is used in argument position
 --> src/main.rs:4:1
  |
4 | #[async_trait]
  | ^^^^^^^^^^^^^^ explicit generic argument not allowed
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

If I remove the default body, it works.

Is this intended to be supported? I can work around it by boxing and using dyn, i.e. values: Box<dyn Iterator<Item=i32> + Send + 'async_trait> but that's not ideal.

Thanks for this crate!

@wfraser
Copy link
Author

wfraser commented Sep 2, 2020

Looks like the expanded code is: (cleaned up a little)

trait MyTrait {
    #[must_use]
    fn blah<'life0, 'async_trait>(&'life0 self, values: impl Iterator<Item = i32> + Send + 'async_trait)
        -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
        where 'life0: 'async_trait,
              Self: Sync + 'async_trait
    {
        async fn __blah<'async_trait, AsyncTrait: ?Sized + MyTrait + Sync>(
            _self: &AsyncTrait,
            values: impl Iterator<Item = i32> + Send + 'async_trait,
        ) {
            println!( ... );
        }
        Box::pin(__blah::<Self>(self, values))
    }
}

and rustc does not like the explicit generic argument in __blah::<Self>(self, values). Simply removing that ::<Self> turbofish works for this case at least.

@taiki-e taiki-e mentioned this issue Sep 4, 2020
@attila-lin
Copy link

Same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants