以下代码(playground)无法编译:
错误信息为:
我找到了一些类似的内容,提到更高级别的特征边界 (例如 这个 Rust 问题 和 这个问题),并尝试使用
这里实际上的问题是什么?是否有一种解决方法可以使其编译,例如增加边界等?
async fn wrapper<F, Fut>(func: F)
where
F: FnOnce(&i32) -> Fut,
Fut: Future<Output = ()>,
{
let i = 5;
func(&i).await;
}
async fn myfunc(_: &i32) {}
fn main() {
wrapper(myfunc);
}
错误信息为:
error: implementation of `std::ops::FnOnce` is not general enough
--> src/main.rs:15:5
|
15 | wrapper(myfunc);
| ^^^^^^^ implementation of `std::ops::FnOnce` is not general enough
|
= note: `std::ops::FnOnce<(&'0 i32,)>` would have to be implemented for the type `for<'_> fn(&i32) -> impl std::future::Future {myfunc}`, for some specific lifetime `'0`...
= note: ...but `std::ops::FnOnce<(&i32,)>` is actually implemented for the type `for<'_> fn(&i32) -> impl std::future::Future {myfunc}`
我找到了一些类似的内容,提到更高级别的特征边界 (例如 这个 Rust 问题 和 这个问题),并尝试使用
for<'a>
和附加生命周期边界进行实验,但错误对我来说仍然不清楚。
我不明白为什么编译器无法确定生命周期,尤其是因为一个没有 futures 的简单版本可以编译通过:
fn myfunc(_: &i32) {}
fn wrapper<F: FnOnce(&i32)>(func: F) {
let i = 5;
func(&i);
}
fn main() {
wrapper(myfunc);
}
这里实际上的问题是什么?是否有一种解决方法可以使其编译,例如增加边界等?