From c063f4da42a538138cc3e80a0e1faaf813a13bd2 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 13 Oct 2023 14:04:14 +0100 Subject: use global executor and some fixes --- common/src/lib.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'common/src') diff --git a/common/src/lib.rs b/common/src/lib.rs index 5317b3e..d58d10b 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -2,6 +2,7 @@ use std::{ future::Future, io::{self, Read, Write}, + sync::Arc, thread, }; @@ -11,19 +12,21 @@ use serde::{Deserialize, Serialize}; use serde_json::Deserializer; use smol::{ channel::{self, Receiver, Sender}, - future, stream::StreamExt, - Executor, }; pub mod msg; pub mod msg_id; -pub trait Handler { +pub trait Handler: Send + Sync + 'static { type Body: Serialize + for<'a> Deserialize<'a> + Send + Clone; fn init(node_id: String, node_ids: Vec, output: Output) -> Self; - fn handle(&self, header: MessageHeader, body: Self::Body) -> impl Future + Send; + fn handle( + &self, + header: MessageHeader, + body: Self::Body, + ) -> impl Future + Send + '_; } pub fn run_server() { @@ -40,14 +43,16 @@ pub fn run_server() { s.spawn(|| send_loop(io::stdout(), out_recv)); // As we receive messages, spawn a future for each - let executor = Executor::new(); - future::block_on(executor.run(async { + let handler = Arc::new(handler); + smol::block_on(async move { while let Some(msg) = inp_recv.next().await { - executor - .spawn(handler.handle(msg.header, msg.body)) - .detach(); + let handler = handler.clone(); + smol::spawn(async move { + handler.handle(msg.header, msg.body).await; + }) + .detach(); } - })); + }); }); } -- cgit v1.2.3