传递给异步回调的引用的生命周期
Lifetime of a reference passed to async callback
我有异步函数,我正在向其传递异步回调。回调将引用作为参数。
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;
}
如果我尝试构建这个 (playground),我会收到以下错误:
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,
| ^^^
我知道它对引用的生命周期不满意。但是,我不明白为什么。
- 我们借用“test”
- 我们执行回调 f(即“foo”)
- baz 无法在 f 完成之前退出
因此,看起来借用不可能比声明测试的地方更有效。
我错过了什么?
foo()
has a hidden lifetime返回的未来。脱糖后的签名如下:
fn foo<'a>(bar: &'a u32) -> impl Future<Output = ()> + 'a {
async move {}
}
这样做是为了使函数可以在 .await
个点上保持 bar
。遗憾的是,它的意思是函数不满足 baz()
的界限。错误被混淆了,因为生命周期是隐藏的,但这就是编译器试图告诉你的:边界应该是类似 where F: for<'a> FnOnce(&'a u32) -> impl Future<Output = ()> + 'a
的东西,但你不能在当前的 Rust 中表达它。
有关更多和潜在的解决方案,请参见示例:
- Generic for FnOnce that returns a future with a lifetime
只是为了解决问题,您可以使用Box
而不是引用。
我有异步函数,我正在向其传递异步回调。回调将引用作为参数。
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;
}
如果我尝试构建这个 (playground),我会收到以下错误:
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,
| ^^^
我知道它对引用的生命周期不满意。但是,我不明白为什么。
- 我们借用“test”
- 我们执行回调 f(即“foo”)
- baz 无法在 f 完成之前退出
因此,看起来借用不可能比声明测试的地方更有效。
我错过了什么?
foo()
has a hidden lifetime返回的未来。脱糖后的签名如下:
fn foo<'a>(bar: &'a u32) -> impl Future<Output = ()> + 'a {
async move {}
}
这样做是为了使函数可以在 .await
个点上保持 bar
。遗憾的是,它的意思是函数不满足 baz()
的界限。错误被混淆了,因为生命周期是隐藏的,但这就是编译器试图告诉你的:边界应该是类似 where F: for<'a> FnOnce(&'a u32) -> impl Future<Output = ()> + 'a
的东西,但你不能在当前的 Rust 中表达它。
有关更多和潜在的解决方案,请参见示例:
- Generic for FnOnce that returns a future with a lifetime
只是为了解决问题,您可以使用Box
而不是引用。