传递给异步回调的引用的生命周期

Vic*_*nin 4 rust

我有一个异步函数,我正在向其传递异步回调。回调将引用作为参数。

use core::future::Future;

async fn foo(bar: &u32) {}

async fn baz<F, Fut>(f: F)
where
    F: FnOnce(&u32) -> Fut,
    Fut: Future<Output = ()>,
{
    let test: u32 = 42;
    f(&test).await;
}

#[tokio::main]
async fn main() {
    baz(foo).await;
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试构建这个(游乐场) ,我会收到以下错误:

error[E0308]: mismatched types
  --> src/main.rs:16:5
   |
16 |     baz(foo).await;
   |     ^^^ lifetime mismatch
   |
   = note: expected associated type `<for<'_> fn(&u32) -> impl Future<Output = ()> {foo} as FnOnce<(&u32,)>>::Output`
              found associated type `<for<'_> fn(&u32) -> impl Future<Output = ()> {foo} as FnOnce<(&u32,)>>::Output`
   = note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
  --> src/main.rs:7:24
   |
7  |     F: FnOnce(&u32) -> Fut,
   |                        ^^^
Run Code Online (Sandbox Code Playgroud)

我知道它对引用的生命周期不满意。但是,我不明白为什么。

  • 我们借用“测试”
  • 我们执行回调 f(即“foo”)
  • 在 f 完成之前 baz 无法退出

因此,看起来借用不可能比声明测试的地方更长久。

我缺少什么?

Cha*_*man 5

返回的未来foo() 有隐藏的一生。脱糖后的签名如下:

fn foo<'a>(bar: &'a u32) -> impl Future<Output = ()> + 'a {
    async move {}
}
Run Code Online (Sandbox Code Playgroud)

这样做是为了使函数可以bar.await点保存。遗憾的是,这意味着该函数未满足baz()的界限。这些错误被混淆了,因为生命周期是隐藏的,但这就是编译器试图告诉你的:界限应该类似于where F: for<'a> FnOnce(&'a u32) -> impl Future<Output = ()> + 'a,但你无法在当前的 Rust 中表达它。

有关更多潜在的解决方案,请参阅以下示例: