This commit is contained in:
2026-07-30 05:35:03 +02:00
parent 6c0bfe7c74
commit 535c6a2964
4 changed files with 42 additions and 49 deletions
+28 -32
View File
@@ -35,41 +35,37 @@ pub async fn send_raw(data: &[u8]) -> tokio::io::Result<()> {
Ok(())
}
macro_rules! engine_methods {
($t:ty) => {
async fn start(&self) -> tokio::io::Result<()> {
let mut stdin = tokio::io::stdin();
loop {
let mut len_buf = [0u8; size_of::<usize>()];
let size = stdin.read_exact(&mut len_buf).await?;
let len = usize::from_le_bytes(len_buf);
if size == 0 || len == 0 {
break Ok(());
}
let mut buffer = vec![0u8; len];
stdin.read_exact(&mut buffer).await?;
self.on_raw(&buffer).await?;
}
}
async fn send(&self, msg: &$t) -> tokio::io::Result<()> {
$crate::send_raw(&$crate::map_postcard_err(
$crate::prelude::postcard::to_allocvec(msg),
)?)
.await
}
};
}
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
#[allow(async_fn_in_trait)]
pub trait Strategy {
engine_methods!(prelude::StrategyMessage);
async fn start(&self) -> tokio::io::Result<()> {
let mut stdin = tokio::io::stdin();
loop {
let mut len_buf = [0u8; size_of::<usize>()];
let size = stdin.read_exact(&mut len_buf).await?;
let len = usize::from_le_bytes(len_buf);
if size == 0 || len == 0 {
break Ok(());
}
let mut buffer = vec![0u8; len];
stdin.read_exact(&mut buffer).await?;
self.on_raw(&buffer).await?;
}
}
async fn send(&self, msg: &prelude::StrategyMessage) -> tokio::io::Result<()> {
send_raw(&map_postcard_err(prelude::postcard::to_allocvec(msg))?).await
}
async fn on_raw(&self, data: &[u8]) -> tokio::io::Result<()> {
match map_postcard_err(postcard::from_bytes(&data))? {