4 releases
0.1.2 | Jul 29, 2023 |
---|---|
0.1.1 | May 10, 2023 |
0.1.0 | May 9, 2023 |
0.0.0 | May 9, 2023 |
#30 in #worker-thread
14KB
223 lines
Affair
A simple tokio-based library to spawn a worker and communicate with it, the worker can be spawned on a dedicated thread or as a tokio task.
Example
use affair::*;
#[derive(Default)]
struct CounterWorker {
current: u64,
}
impl Worker for CounterWorker {
type Request = u64;
type Response = u64;
fn handle(&mut self, req: Self::Request) -> Self::Response {
self.current += req;
self.current
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let port = DedicatedThread::spawn(CounterWorker::default());
assert_eq!(port.run(10).await.unwrap(), 10);
assert_eq!(port.run(3).await.unwrap(), 13);
}
lib.rs
:
A simple tokio-based library to spawn a worker and communicate with it, the worker can be spawned on a dedicated thread or as a tokio task.
use affair::*;
#[derive(Default)]
struct CounterWorker {
current: u64,
}
impl Worker for CounterWorker {
type Request = u64;
type Response = u64;
fn handle(&mut self, req: Self::Request) -> Self::Response {
self.current += req;
self.current
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let socket = DedicatedThread::spawn(CounterWorker::default());
assert_eq!(socket.run(10).await.unwrap(), 10);
assert_eq!(socket.run(3).await.unwrap(), 13);
}
Dependencies
~2.8–9MB
~77K SLoC