summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-10-13 14:04:14 +0100
committerAria <me@aria.rip>2023-10-13 14:04:14 +0100
commitc063f4da42a538138cc3e80a0e1faaf813a13bd2 (patch)
tree9a927c20c420bfa32eed18aafa319362df706303 /common
parente9ae00db22f30fcf0cfa8f7e05232aed46efb39b (diff)
use global executor and some fixes
Diffstat (limited to 'common')
-rw-r--r--common/src/lib.rs25
1 files changed, 15 insertions, 10 deletions
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<String>, output: Output<Self::Body>) -> Self;
- fn handle(&self, header: MessageHeader, body: Self::Body) -> impl Future<Output = ()> + Send;
+ fn handle(
+ &self,
+ header: MessageHeader,
+ body: Self::Body,
+ ) -> impl Future<Output = ()> + Send + '_;
}
pub fn run_server<H: Handler>() {
@@ -40,14 +43,16 @@ pub fn run_server<H: Handler>() {
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();
}
- }));
+ });
});
}