gpt4 book ai didi

async-await - 为什么 `Box` 在使用 Tokio 和实验性异步/等待支持时不实现 `Sink` 特征?

转载 作者:行者123 更新时间:2023-11-29 08:18:19 33 4
gpt4 key购买 nike

我正在使用 Tokio 玩异步/等待功能,并在我的 Cargo.toml 中启用了异步/等待功能。 (以及 2018 年版的最新 Rust nightly):

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`

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

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

错误是什么意思?我该如何解决这个问题?

最佳答案

我的解决方法是包装 Box编辑Sink将其转换为新类型并实现 Sink的特点。我想也许Box<dyn Sink>不执行 Sink in this nightly,这基本上就是错误消息所暗示的(我猜 async/await shims 重新定义了 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 .

关于async-await - 为什么 `Box<dyn Sink>` 在使用 Tokio 和实验性异步/等待支持时不实现 `Sink` 特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53302482/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com