Async fn in trait with lifetime GAT

Hi!

Now that support for async functions in traits might be coming in the near future I've been trying to use the nightly implementation to add async support to my crate but I've been facing the following issue.

Minimal reproducible example (playground link):

#![feature(async_fn_in_trait)]

trait IntoStateMachine {
    type Context<'a>;
}

trait State<M> where M: IntoStateMachine {
    async fn handle(&mut self, context: &mut M::Context<'_>);
}

struct MyStateMachine;
struct MyState;

impl IntoStateMachine for MyStateMachine {
    type Context<'a> = ();
}

impl State<MyStateMachine> for MyState {
    async fn handle(&mut self, context: &mut ()) {}
}

fn main() {
    println!("Hello, world!");
}

Gives the following error:

error[E0195]: lifetime parameters or bounds on method `handle` do not match the trait declaration
  --> src/main.rs:19:20
   |
8  |     async fn handle(&mut self, context: &mut M::Context<'_>);
   |                    - lifetimes in impl do not match this method in trait
...
19 |     async fn handle(&mut self, context: &mut ()) {}
   |                    ^ lifetimes do not match method in trait

I don't really understand what the issue is here or how I might resolve it?

Thanks!

Don't ask me why, but this compiles:

#![feature(async_fn_in_trait)]

trait IntoStateMachine {
    type Context<'a>;
}

trait State<M> where M: IntoStateMachine {
    async fn handle(&mut self, context: &mut M::Context<'_>);
}

struct MyStateMachine;
struct MyState;

impl IntoStateMachine for MyStateMachine {
    type Context<'a> = ();
}

impl State<MyStateMachine> for MyState {
    async fn handle(&mut self, context: &mut <MyStateMachine as IntoStateMachine>::Context<'_>) {}
}

fn main() {
    println!("Hello, world!");
}

Playground.

Oh wow, and here I was adding lifetimes left and right. Thank you!

I assume this is a bug that will worked out eventually?

I guess so. The async_fn_in_trait feature is still marked as incomplete, so there will definitely be further changes before it will be even considered for stable.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.