Lifetime mismatch between function in async trait and implementations

46 views Asked by At

Background

I'm trying to create a trait object with a single associated function, but that function needs to be async. So, I've been trying to use the async_trait crate to enable just that.

Problem Statement

In my example below however, the compiler complains about mismatched lifetime parameters. For the life of me, I cannot figure out why this lifetime error appears. May I humbly ask for your input on the issue? Am I overlooking something obvious?

error[E0195]: lifetime parameters or bounds on method `run` do not match the trait declaration
  --> src/lib.rs:13:17
   |
9  |     async fn run(&mut self, res: &Resources, t: Duration, dt: Duration);
   |     -----    ---------------------------------------------------------- lifetimes in impl do not match this method in trait
   |     |
   |     this bound might be missing in the impl
...
13 |     async fn run(&mut self, _: &Resources, _: Duration, _: Duration)
   |                 ^ lifetimes do not match method in trait

For more information about this error, try `rustc --explain E0195`.
error: could not compile `playground` (lib) due to 1 previous error

Steps to Diagnose

Based on the documentation, I've gathered that async_trait adds additional lifetime parameters during expansion, but I'm not good with procedural macro code, so I wasn't able to figure out the exact used parameters and bounds from the code. Implementing the lifetime parameters from the example expansion (AutoplayingVideo) in the documentation didn't work.

Minimal Reproducing Example

Rust Playground Code

use std::time::Duration;
use async_trait::async_trait;

#[derive(Debug)]
struct Resources(Vec<usize>);

#[async_trait]
pub trait System {
    async fn run(&mut self, res: &Resources, t: Duration, dt: Duration);
}

impl System for () {
    async fn run(&mut self, _: &Resources, _: Duration, _: Duration) {}
}
0

There are 0 answers