rsActor
A Lightweight Rust Actor Framework with Simple Yet Powerful Task Control.
rsActor is a lightweight, Tokio-based actor framework in Rust focused on providing simple yet powerful task control. It prioritizes simplicity and efficiency for local, in-process actor systems while giving developers complete control over their actors' execution lifecycle — define your own run_loop, control execution, control the lifecycle.
Note: This project is actively evolving. While core APIs are stable, some features may be refined in future releases.
Core Features
- Minimalist Actor System: Focuses on core actor model primitives.
- Message Passing:
ask/ask_with_timeout: Send a message and asynchronously await a reply.tell/tell_with_timeout: Send a message without waiting for a reply.ask_blocking/tell_blocking: Blocking versions fortokio::task::spawn_blockingcontexts.
- Actor Lifecycle with Simple Yet Powerful Task Control:
on_start,on_stop, andrun_loophooks form the actor's lifecycle. The distinctiverun_loopfeature (added in v0.4.0) provides a dedicated task execution environment that users can control with simple yet powerful primitives, unlike other actor frameworks. This gives developers complete control over their actor's task logic while the framework manages the underlying execution, eliminating the need for separatetokio::spawncalls. All lifecycle hooks are optional and have default implementations. - 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. - Only
SendTrait Required: Actor structs only need to implement theSendtrait (notSync), enabling the use of interior mutability types likestd::cell::Cellfor internal state management without synchronization overhead.
Getting Started
1. Add Dependency
[]
= "0.5" # 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.
Further Information
For more detailed questions and answers, please see the FAQ.
License
This project is licensed under the Apache License 2.0. See the LICENSE-APACHE file for details.