gpt4 book ai didi

rust - 如何通过 future channel 将异步消息发送到接收器?

转载 作者:行者123 更新时间:2023-12-03 11:48:07 26 4
gpt4 key购买 nike

我曾经能够做到这一点,但我正为0.3 future 而苦苦挣扎。
这是我从WebSocket获得的接收器和流:

let (mut sink, stream) = ws_stream.split();
我创建了一个无界 channel ,以在异步tokio任务之间进行通信:
let (unbounded_sender, unbounded_receiver) = mpsc::unbounded::<tungstenite::Message>();
这是我坚持的部分。我产生了一个异步任务,该任务应该将无边界接收器与接收器连接起来;我的想法是我沿着 unbounded_sender发送消息:
tokio::spawn(async {
sink.send_all(&mut unbounded_receiver);
Ok(())
});
错误消息显示 send_all:
 " expected enum `tungstenite::protocol::message::Message`, found enum `std::result::Result`"
并且
"type mismatch resolving `<futures_channel::mpsc::UnboundedReceiver<tungstenite::protocol::message::Message> as futures_core::stream::Stream>::Item == std::result::Result<_, _>`"
并且
type mismatch resolving `<futures_channel::mpsc::UnboundedReceiver<tungstenite::protocol::message::Message> as futures_core::stream::Stream>::Item == std::result::Result<tungstenite::protocol::message::Message, tungstenite::error::Error>`
at the docs,我可以看到 unbounded_receiver实现了 TryStream所需的 send_all特性,因此我不确定该怎么做。

最佳答案

future 现在需要unbounded_channel来实现futures_core::stream::TryStream只需将每个消息包装在Ok结果中即可完成:

    sink.send_all(
&mut unbounded_receiver.map(Ok::<tungstenite::Message, tungstenite::error::Error>),
);

关于rust - 如何通过 future channel 将异步消息发送到接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63036717/

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