Expand description
Actor model framework for Rust built on Tokio.
Tactix provides an ergonomic actor model with hierarchical supervision, async
handlers, automatic restart on panic, and support for both fire-and-forget
(tell) and request-response (ask) message patterns.
§Installation
cargo add tactix§Getting started
use tactix::{Actor, Ctx, Handler, Message, Recipient, Sender};
// --- Actor ---
struct Counter {
count: u64,
}
impl Actor for Counter {}
// --- Messages ---
#[derive(Message)]
struct Increment;
#[derive(Message)]
struct Decrement;
#[derive(Message)]
#[response(u64)]
struct GetCount;
// --- Handlers ---
impl Handler<Increment> for Counter {
async fn handle(&mut self, _: Increment, _: &Ctx<Self>) {
self.count += 1;
}
}
impl Handler<Decrement> for Counter {
async fn handle(&mut self, _: Decrement, _: &Ctx<Self>) {
self.count -= 1;
}
}
impl Handler<GetCount> for Counter {
async fn handle(&mut self, _: GetCount, _: &Ctx<Self>) -> u64 {
self.count
}
}
#[tokio::main]
async fn main() -> Result<(), String> {
let counter = Counter { count: 0 }.start();
// Fire-and-forget with `tell`
counter.tell(Increment);
counter.tell(Increment);
counter.tell(Decrement);
// Type-erased recipient for dependency injection
let r: Recipient<Decrement> = counter.clone().recipient();
r.tell(Decrement);
// Use `ask` with a no-response message to synchronise — the response
// is sent only after all prior messages have been handled.
let _ = counter.ask(Increment).await;
// Request-response with `ask`
assert_eq!(counter.ask(GetCount).await, 1);
Ok(())
}Structs§
- Actor
System - The global root actor system.
- Addr
- Address of an actor. Messages are sent through this handle.
- Ctx
- Context handle for an actor.
- Envelope
- Wraps a message payload with an optional oneshot channel for the response.
- Recipient
- Type-erased sender for a specific message type.
- Shutdown
- Message to gracefully shut down the global actor system.
Enums§
- Interrupt
- Outcome of an actor’s event loop iteration that drives lifecycle decisions.
- Supervision
Strategy - Supervision strategy that controls how panics in an actor are handled.
Traits§
- Actor
- Trait implemented by all actors.
- Actor
Message - Internal trait for type-erased message dispatch inside the actor task.
- Handler
- Trait implemented on an
Actorto process a specificMessagetype. - Message
- Trait implemented by all message types.
- Sender
- Capability to send messages of type
Mto an actor. - Stoppable
- Interface for objects that can be stopped and awaited for shutdown.