1

我正在玩 async/await,方法是使用 Tokio 并在我的Cargo.toml(以及最新的 Rust nightly 2018 版)中启用了 async/await 功能:

tokio = { version = "0.1.11", features = ["async-await-preview"] }

我遇到了一个我不明白的错误,在这个最小的例子中重现:

#![feature(await_macro, async_await, futures_api)]
use tokio::prelude::*;

pub fn main() {
    tokio::run_async(async {
        let s: Option<Box<dyn Sink<SinkItem = u8, SinkError = ()> + Send + Sync + 'static>> = None;

        if let Some(sink) = s {
            await!(sink.send_async(100));
        }
    });
}

错误是:

error[E0277]: the trait bound `for<'r> (dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 'r): futures::sink::Sink` is not satisfied
 --> src/main.rs:5:5
  |
5 |     tokio::run_async(async {
  |     ^^^^^^^^^^^^^^^^ the trait `for<'r> futures::sink::Sink` is not implemented for `dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send`
  |
  = note: required because of the requirements on the impl of `futures::sink::Sink` for `std::boxed::Box<dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send>`
  = note: required because it appears within the type `tokio_async_await::sink::send::Send<'_, std::boxed::Box<dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send>>`
  = note: required because it appears within the type `for<'r, 's, 't0, 't1> {std::option::Option<std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 'r)>>, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 's)>, tokio_async_await::sink::send::Send<'t0, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 't1)>>, ()}`
  = note: required because it appears within the type `[static generator@src/main.rs:5:28: 11:6 for<'r, 's, 't0, 't1> {std::option::Option<std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 'r)>>, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 's)>, tokio_async_await::sink::send::Send<'t0, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 't1)>>, ()}]`
  = note: required because it appears within the type `std::future::GenFuture<[static generator@src/main.rs:5:28: 11:6 for<'r, 's, 't0, 't1> {std::option::Option<std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 'r)>>, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 's)>, tokio_async_await::sink::send::Send<'t0, std::boxed::Box<(dyn futures::sink::Sink<SinkItem=u8, SinkError=()> + std::marker::Sync + std::marker::Send + 't1)>>, ()}]>`
  = note: required because it appears within the type `impl std::future::Future`
  = note: required by `tokio::async_await::run_async`

如果我删除以“等待!”开头的行,它就会消失。

Box<dyn Sink>在没有实验性 async/await 支持的情况下使用 Tokio 时,我的程序对 a是 a的想法很满意Sink,所以我不确定为什么使用 async/await 的东西会出现错误。

错误是什么意思?我怎样才能解决这个问题?

4

1 回答 1

1

我的解决方法是将Boxed包装Sink成一个新类型并在其Sink上实现特征。我认为这可能Box<dyn Sink>不会Sink在这个夜间实现,这基本上是错误消息所暗示的(我猜 async/await 垫片重新定义Sink并且不实现它Box)。

我的包装器最终看起来像这样:

struct BoxedSink<I, E>(Box<dyn Sink<SinkItem = I, SinkError = E> + Send + Sync + 'static>);

impl<I, E> Sink for BoxedSink<I, E> {
    type SinkItem = I;
    type SinkError = E;

    fn start_send(
        &mut self,
        input: Self::SinkItem,
    ) -> Result<AsyncSink<Self::SinkItem>, Self::SinkError> {
        self.0.start_send(input)
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.0.poll_complete()
    }
}

您必须将您的包装Box<Sinks>在其中以使它们再次实施Sink

于 2018-11-14T21:48:09.097 回答