Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

Rust: Compatibility issues between tokio_core and futures_channel, futures::future::Future and impl std::future::Future

First of all, I am quite new to Rust, so please don't blame me if this is quite a very simple problem. In my application, I want to extensively use a library which is based on the tokio_core runtime and hence I am forced to stick to this deprecated piece of code. I want to add a websocket functionality to it by making use of tokio_tungstenite.

I am trying to compile the following code which is in close resemblance to the code shown in https://bryangilbert.com/post/code/rust/adventures-futures-tokio-rust/.

use tokio_core::reactor::Core;
use futures_util::{StreamExt};
use tokio_tungstenite::{tungstenite::protocol::Message};

async fn run_worker(receiver : futures_channel::mpsc::UnboundedReceiver<Message>) {
   
   let mut core = Core::new().unwrap();
   let handle = core.handle();
       
   let rcv = {receiver.for_each(|message| async move {
               println!("received message "{}"", message);
               //TODO spawn tasks for the core here
           })};

//ERROR is here:
   core.run(rcv);
//alternatively using this instead does also not work
//     loop { core.turn(None) };
//     rcv.await
}

#[tokio::main]
async fn main() {
 let (server_to_worker_sender, server_to_worker_receiver) = futures_channel::mpsc::unbounded();
 let worker = tokio::spawn(async move {run_worker(server_to_worker_receiver).await});
 server_to_worker_sender.unbounded_send(Message::binary("test-message")).expect("Couldnt send it.");
 worker.await.expect("Worker panicked")
}

I am getting the following error :

the trait futures::future::Future is not implemented for `ForEach<UnboundedReceiver, impl std::future::Future.

My current understanding is that there seems to be a compatibility problem between the futures used by tokio_core and those using the async/await semantics. For me the most confusing thing is the excessive usage of the word future and I am specially not aware of what futures::future::Future differs from std::future::Future and why there is a bunch of different possibilities to handle futures in rust. If I am wrong, which is likely the case, please let me know. Any comments are highly welcome. I also tried different solutions, trying to forward the receiver to any sink which can be run() by tokio_core but no solution is in sight.

question from:https://stackoverflow.com/questions/65858636/rust-compatibility-issues-between-tokio-core-and-futures-channel-futuresfutu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...