Crate affair

Source
Expand description

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);
}

Structs§

DedicatedThread
An executor that provides the worker with its dedicated std::thread.
Socket
A socket that can be used to communicate with a worker, you can use a socket to send requests to a worker and wait for the response.
Task
TokioSpawn
An executor that runs the worker as a tokio task.
WeakSocket
A weak reference to a Socket that does not keep the worker alive.

Enums§

RunError

Traits§

AsyncWorker
An awaiting worker that processes requests and returns a response.
Executor
An execution engine that handles spawning a Worker.
Worker
A non-awaiting worker that processes requests and returns a response.