Expand description
Simple Async Message Passing, sampr, is a message passing framework based on actors and inspired by actix
The crate provides an abstraction to pass Messages between Actors. In contrast to actix sampr builds upon the rust language’s async/await feature.
Overview
An actor is an arbitrary type implementing the Actor trait. Each actor runs, when started, in its own Context, which is spawned in the tokio runtime as tokio::task.
struct MyActor;
impl sampr::Actor for MyActor {}
After an actor has started, it can receive messages from any other actor (or other components of the program if the user whishes to do so). A messages is an arbitrary type implementing the Message trait.
struct MyMessage(u8);
impl sampr::Message for MyMessage {
type Result = bool;
}
To receive a message, the actor type has to implement the Handler trait for this specific message.
#[sampr::async_trait]
impl sampr::Handler<MyMessage> for MyActor {
async fn handle(&mut self, msg: MyMessage, _ctx: &mut sampr::Context<Self>) -> bool {
msg.0 == 0
}
}
Apart from messages, the actor’s task can, through Context, be augmented with streams and futures to do work asynchronously and callback the Actor once work is done or new elements are yielded from the stream.
The lifetime of an actor could look like the following:
async fn main() {
let actor = MyActor;
let addr = actor.start();
assert!(addr.send(MyMessage(0)).unwrap());
let actor = addr.stop();
}
Re-exports
pub use async_trait::async_trait;
Structs
- Address of an actor.
- Context for interacting with an Actor’s task.
Enums
- Error type for the sampr crate.
Traits
- An asynchronous actor.
- A trait implemented by an actor to handle a specific message.
- A message sent to an actor.