Expand description
This crate provides a simple, runtime-agnostic, actor framework, aimed to be a minimal framework that gets out of your way.
use async_oneshot_channel::oneshot;
use async_actor::{Actor, ActorRef, WeakActorRef};
struct CounterActor(usize);
impl Actor for CounterActor {
type Error = ();
type Message = usize;
async fn on_msg(
&mut self,
_: &WeakActorRef<Self>,
msg: Self::Message,
) -> Result<(), Self::Error> {
self.0 += msg;
println!("Received message: {}. Current state: {}", msg, self.0);
Ok(())
}
}
#[tokio::main]
async fn main() {
let actor = CounterActor(0);
let (actor_ref, fut) = actor.into_future(None);
let handle = tokio::spawn(fut);
actor_ref.send(3).await.unwrap();
actor_ref.send(7).await.unwrap();
actor_ref.stop(0).unwrap();
let res = handle.await;
assert!(res.is_ok());
}
Structs§
- Actor
Ref - A handle to an actor, that allows messages to be sent to the actor.
- Actor
Run - A future that drives an actor from start to completion. Once awaited, it will run the actor, process all messages, and eventually resolve with either the actor (on success) or an error.
- Mailbox
- A mailbox for an actor, containing a receiver for messages, a receiver for stop messages, and a weak reference to the actor.
- Weak
Actor Ref - A reference to an actor that allows messages to be sent to the actor.