在特征中使用异步时如何解决“不透明类型`impl Future<Output = Self>`不满足其关联类型边界”警告?

问题描述 投票:0回答:1

我从以下代码中收到警告:

trait AsyncClient {
    async fn new(host: &str, port: u16) -> Self;
}
warning: opaque type `impl Future<Output = Self>` does not satisfy its associated type bounds
  --> src/lib.rs:2:5
   |
2  |     async fn new(host: &str, port: u16) -> Self;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
  ::: /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/future/future.rs:40:5
   |
40 |     type Output;
   |     ------------ this associated type bound is unsatisfied for `Self`
   |
   = note: `#[warn(opaque_hidden_inferred_bound)]` on by default

如何修复警告?

rust async-await traits
1个回答
0
投票

添加

: Sized
绑定解决了问题:

trait AsyncClient: Sized {
    async fn new(host: &str, port: u16) -> Self;
}
© www.soinside.com 2019 - 2024. All rights reserved.