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

  • An executor that provides the worker with its dedicated std::thread.
  • 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.
  • An executor that runs the worker as a tokio task.
  • A weak reference to a Socket that does not keep the worker alive.

Enums

Traits

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