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_start,on_stop, andrun_loophooks. Therun_loopmethod is called afteron_startand contains the main execution logic of the actor, running for its lifetime. All lifecycle hooks are optional and have default implementations. - Built-in Task Management: The
run_loopfeature eliminates the need for separatetokio::spawncalls for actor tasks, as the framework handles spawning and managing actor tasks internally. - 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.
Version Differences
Key Changes in v0.4.0 (compared to v0.3.0 and below)
- Optimized
ActorRefPassing (Reference-Based): In v0.4.0,ActorRefis passed by reference (&ActorRef) to lifecycle methods likeon_startandon_stop. This change focuses on optimization by reducingActorRefcloning in these common method calls, rather than being a substantial performance gain across the board.ActorRefremains clonable for explicit duplication if needed. run_loopIntroduction for Optimized Task Management: v0.4.0 adds therun_loopmethod. This provides a dedicated, framework-managed execution context for an actor's primary logic. This optimizes actor implementation by centralizing its core task management within the framework, removing the need for developers to manuallytokio::spawnthe actor's main processing loop.
Getting Started
1. Add Dependency
[]
= "0.4" # 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.