rsActor: A Simplified Actor Framework for Rust
rsActor is a lightweight, Tokio-based actor framework in Rust. It prioritizes simplicity for local, in-process actor systems.
Note: This project is in early development. APIs may change.
Core Features
- Minimalist Actor System: Focuses on core actor model primitives.
- Message Passing:
ask: Send a message and asynchronously await a reply.tell: Send a message without waiting for a reply.ask_blocking/tell_blocking: Blocking versions fortokio::task::spawn_blockingcontexts.
- Actor Lifecycle:
on_startandon_stophooks. - Graceful & Immediate Termination: Actors can be stopped gracefully or killed.
- Macro-Assisted Message Handling:
impl_message_handler!macro simplifies routing messages. - Tokio-Native: Built for the
tokioasynchronous runtime.
Comparison with Kameo
rsActor differs from Kameo by:
- Supporting local actors only (no remote actor support).
- Using a concrete
ActorRefwith runtime type checking for replies. - Not including built-in actor linking or supervision.
- Being tightly coupled with Tokio.
- Using the
impl_message_handler!macro for message handling.
Getting Started
1. Add Dependency
[]
= "0.3" # Check crates.io for the latest version
2. Basic Usage Example
A simple counter actor:
use ;
use Result;
use info;
// Define actor struct
// Implement Actor trait
// Define message types
;
;
// Implement Message<T> for IncrementMsg
// Implement Message<T> for GetCountMsg
// Use macro for message handling
impl_message_handler!;
async
Running the Example
Run the example from examples/basic.rs:
Using Blocking Functions with Tokio Tasks
ask_blocking and tell_blocking are for use within Tokio's blocking tasks (tokio::task::spawn_blocking).
When to Use
- Inside a
tokio::task::spawn_blockingtask.
Example
use ; // Assuming Actor is also in scope
use task;
use Duration;
use Result;
// Dummy message and actor for context
;
;
;
impl_message_handler!;
async
// To make this runnable, you'd need to spawn an actor and pass its ActorRef
// For example:
// #[tokio::main]
// async fn main() -> Result<()> {
// let (actor_ref, _join_handle) = rsactor::spawn(MyActor);
// demonstrate_blocking_calls(actor_ref).await?;
// Ok(())
// }
Important: These functions require an active Tokio runtime.
License
This project is licensed under the Apache License 2.0. See the LICENSE-APACHE file for details.