I'm trying to pass an async function as an argument. The async fucntion accepts a reference as it's arguemnt.
use std::future::Future;
async fn f(x: &i32) -> i32 {
todo!()
}
async fn g<F, Fut>(f: F)
where
F: Send + Sync + 'static + for<'a> Fn(&'a i32) -> Fut,
Fut: Future<Output = i32> + Send + Sync,
{
// let x = 3;
// f(&x).await;
}
#[tokio::main]
async fn main() {
g(f).await;
}
But I've got compile error.
error[E0308]: mismatched types
--> src/main.rs:18:5
|
18 | g(f).await;
| ^ lifetime mismatch
|
= note: expected associated type `<for<'_> fn(&i32) -> impl Future<Output = i32> {f} as FnOnce<(&i32,)>>::Output`
found associated type `<for<'_> fn(&i32) -> impl Future<Output = i32> {f} as FnOnce<(&'a i32,)>>::Output`
= note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
--> src/main.rs:9:55
|
9 | F: Send + Sync + 'static + for<'a> Fn(&'a i32) -> Fut,
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `test-async-tokio` due to previous error
Why lifetime is introduced by Fut here?
How to specify the lifetime bound for this code?
Best regards!