< 返回版块

Clownsw 发表于 2022-04-01 12:09

pub async fn run_dyn_test<T>(f: Box<dyn Future<Output = T>>) -> T {
    *f.await
}

error[E0277]: `dyn std::future::Future<Output = T>` cannot be unpinned
  --> src\util\common_util.rs:15:7
   |
15 |     *f.await
   |       ^^^^^^ the trait `Unpin` is not implemented for `dyn std::future::Future<Output = T>`
   |
   = note: consider using `Box::pin`
   = note: required because of the requirements on the impl of `std::future::Future` for `Box<dyn std::future::Future<Output = T>>`
   = note: required because of the requirements on the impl of `IntoFuture` for `Box<dyn std::future::Future<Output = T>>`
help: remove the `.await`
   |
15 -     *f.await
15 +     *f
   |

error[E0614]: type `T` cannot be dereferenced
  --> src\util\common_util.rs:15:5
   |
15 |     *f.await
   |  

大佬们, 有没有办法解决

评论区

写评论
czw0818 2022-04-06 21:26

Box是有所有权的,只要不解引用就啥事没有

作者 Clownsw 2022-04-01 12:22

感谢

--
👇
Easonzero: ```rust pub async fn run_dyn_test(f: Pin<Box<dyn Future<Output = T>>>) -> T { f.await }


Easonzero 2022-04-01 12:19
pub async fn run_dyn_test<T>(f: Pin<Box<dyn Future<Output = T>>>) -> T {
    f.await
}
1 共 3 条评论, 1 页