小编gav*_*rie的帖子

在 async fn 中实现 FnOnce 不够通用?

以下代码 ( playground ) 无法编译:

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);
}
Run Code Online (Sandbox Code Playgroud)

错误信息是:

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 …
Run Code Online (Sandbox Code Playgroud)

rust

8
推荐指数
1
解决办法
1226
查看次数

标签 统计

rust ×1