前向方法不满足 rust tokio trait bounds
rust tokio trait bounds were not satisfied on forward method
我在我的 rust 项目中升级了 wrap
和 tokio
,升级后,forward 方法出错了。查了下文档,新版tokio framework中没有forward方法
错误
error[E0599]: the method `forward` exists for struct `tokio::sync::mpsc::UnboundedReceiver<_>`, but its trait bounds were not satisfied
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
^^^^^^^ method cannot be called on `tokio::sync::mpsc::UnboundedReceiver<_>` due to unsatisfied trait bounds
40 | pub struct UnboundedReceiver<T> {
| -------------------------------
| |
| doesn't satisfy `_: warp::Stream`
| doesn't satisfy `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
|
= note: the following trait bounds were not satisfied:
`tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
`&tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `&tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
`&mut tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `&mut tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
代码:
let (client_ws_sender, mut client_ws_rcv) = ws.split();
let (client_sender, client_rcv) = mpsc::unbounded_channel();
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
if let Err(e) = result {
eprintln!("error sending websocket msg: {}", e);
}
}));
货物相关性:
[dependencies]
tokio = { version = "1.6.0", features = ["full"] }
warp = "0.3.1"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = { version = "0.3", default-features = false }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
该错误消息中最有说服力的几行如下:
| doesn't satisfy `_: warp::Stream`
| doesn't satisfy `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
forward
method is defined in the StreamExt
trait; due to a blanket implementation, anything that implements Stream
also implements StreamExt
. However, as of Tokio v1.6.0, UnboundedReceiver
不再实现 Stream
。文档改为指出:
This receiver can be turned into a Stream
using UnboundedReceiverStream
.
因此:
let (client_ws_sender, mut client_ws_rcv) = ws.split();
let (client_sender, client_rcv) = mpsc::unbounded_channel();
let client_rcv = UnboundedReceiverStream::new(client_rcv); // <-- this
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
if let Err(e) = result {
eprintln!("error sending websocket msg: {}", e);
}
}));
我在我的 rust 项目中升级了 wrap
和 tokio
,升级后,forward 方法出错了。查了下文档,新版tokio framework中没有forward方法
错误
error[E0599]: the method `forward` exists for struct `tokio::sync::mpsc::UnboundedReceiver<_>`, but its trait bounds were not satisfied
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
^^^^^^^ method cannot be called on `tokio::sync::mpsc::UnboundedReceiver<_>` due to unsatisfied trait bounds
40 | pub struct UnboundedReceiver<T> {
| -------------------------------
| |
| doesn't satisfy `_: warp::Stream`
| doesn't satisfy `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
|
= note: the following trait bounds were not satisfied:
`tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
`&tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `&tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
`&mut tokio::sync::mpsc::UnboundedReceiver<_>: warp::Stream`
which is required by `&mut tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
代码:
let (client_ws_sender, mut client_ws_rcv) = ws.split();
let (client_sender, client_rcv) = mpsc::unbounded_channel();
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
if let Err(e) = result {
eprintln!("error sending websocket msg: {}", e);
}
}));
货物相关性:
[dependencies]
tokio = { version = "1.6.0", features = ["full"] }
warp = "0.3.1"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = { version = "0.3", default-features = false }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
该错误消息中最有说服力的几行如下:
| doesn't satisfy `_: warp::Stream`
| doesn't satisfy `tokio::sync::mpsc::UnboundedReceiver<_>: StreamExt`
forward
method is defined in the StreamExt
trait; due to a blanket implementation, anything that implements Stream
also implements StreamExt
. However, as of Tokio v1.6.0, UnboundedReceiver
不再实现 Stream
。文档改为指出:
This receiver can be turned into a
Stream
usingUnboundedReceiverStream
.
因此:
let (client_ws_sender, mut client_ws_rcv) = ws.split();
let (client_sender, client_rcv) = mpsc::unbounded_channel();
let client_rcv = UnboundedReceiverStream::new(client_rcv); // <-- this
tokio::task::spawn(client_rcv.forward(client_ws_sender).map(|result| {
if let Err(e) = result {
eprintln!("error sending websocket msg: {}", e);
}
}));