Expand description
Theatre: A concise async actor model implementation
Example:
use theatre::prelude::*;
#[derive(Default)]
struct Accumulator {
count: i32,
}
impl Actor for Accumulator {}
// A message to increase the count of the accumulator by one
struct Increment;
impl Message for Increment {
type Return = ();
}
// A message to get the current count of the accumulator
struct GetCount;
impl Message for GetCount {
type Return = i32;
}
// Handle `Increment` messages for the accumulator
#[async_trait]
impl Handler<Increment> for Accumulator {
async fn handle(
&mut self,
_msg: Increment,
_context: &mut Context<Self>,
) -> <Increment as Message>::Return {
println!("Adding 1");
self.count += 1
}
}
// Handle `GetCount`
#[async_trait]
impl Handler<GetCount> for Accumulator {
async fn handle(
&mut self,
_msg: GetCount,
_context: &mut Context<Self>,
) -> <GetCount as Message>::Return {
self.count
}
}
#[tokio::main]
async fn main() {
let mut acc = Accumulator::default().spawn().await;
assert_eq!(0, acc.send(GetCount).await.unwrap());
acc.send(Increment).await.unwrap(); // Output: `Adding 1`
acc.send(Increment).await.unwrap(); // Output: `Adding 1`
acc.send(Increment).await.unwrap(); // Output: `Adding 1`
acc.send(Increment).await.unwrap(); // Output: `Adding 1`
println!("Value: {}", acc.send(GetCount).await.unwrap()); // Output: Value: 4
assert_eq!(4, acc.send(GetCount).await.unwrap());
}Modules§
- actors
- Provided utility actors
- mailbox
- Structures related to the actor mailbox
- prelude
- Convenience module for common actor types
Structs§
- Actor
Handle - ActorHandle is a method of
awaiting the actor’s task. Dropping the ActorHandle will not shutdown the actor. - Config
- Context
- Context is the execution context of the actor. It allows for the actor to dynamically retrieve
the address of the actor’s
Mailbox.
Enums§
- Theatre
Error - Possible error variants.
Traits§
- Actor
- Actor is one of the core traits of theatre.
- Actor
Ext - Extensions for Actors. This is blanket impl’d on any type that implements
Actor. - Handler
- The
Handlertrait allows for an actor to have a per-message type handle implementation.
Functions§
- start
- Start an actor in a dedicated task and return its address and handle.