rsActor
A Simple and Efficient In-Process Actor Model Implementation for Rust.
rsActor is a lightweight, Tokio-based actor framework in Rust focused on providing a simple and efficient actor model for local, in-process systems. It emphasizes clean message-passing semantics and straightforward actor lifecycle management while maintaining high performance for Rust applications.
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.
- Actor Derive Macro:
#[derive(Actor)]for simple actors that don't need complex initialization. - 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.
- Straightforward Actor Lifecycle:
on_start,on_run, andon_stophooks provide a clean and intuitive actor lifecycle management system. The framework manages the execution flow while giving developers full control over actor behavior. - Graceful & Immediate Termination: Actors can be stopped gracefully or killed.
ActorResult: Enum representing the outcome of an actor's lifecycle (e.g., completed, failed).- Macro-Assisted Message Handling:
impl_message_handler!macro simplifies routing messages. - Tokio-Native: Built for the
tokioasynchronous runtime. - Strong Type Safety: Provides both compile-time (
ActorRef<T>) and runtime (UntypedActorRef) type safety options, ensuring message handling consistency while supporting flexible actor management patterns. - 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. TheActortrait andMessageHandlertrait (viaimpl_message_handler!macro) are also required, but they don't add any additional constraints on the actor's fields.
Getting Started
1. Add Dependency
[]
= "0.8" # Check crates.io for the latest version
2. Choose Your Implementation Approach
Option A: Using the Actor Derive Macro (Recommended for Simple Cases)
For simple actors that don't need complex initialization logic:
use ;
// 1. Define your actor struct and derive Actor
// 2. Define message types
;
;
// 3. Implement message handlers
// 4. Wire up message handlers
impl_message_handler!;
// 5. Usage
async
Option B: Manual Implementation (For Complex Initialization)
For actors that need complex initialization logic:
use ;
use Result;
use info;
// Define actor struct
// Added Debug for printing the actor in ActorResult
// Implement Actor trait
// Define message types
;
// Implement Message<T> for IncrementMsg
// Use macro for message handling
impl_message_handler!;
async
Examples
rsActor comes with several examples that demonstrate various features and use cases:
- basic - Simple counter actor demonstrating core actor model concepts
- actor_with_timeout - Using timeouts for actor communication
- actor_async_worker - Inter-actor communication with async tasks
- actor_task - Background task communication with actors
- actor_blocking_task - Using blocking APIs with actors
- dining_philosophers - Classic concurrency problem implementation
- unified_macro_demo - Message handling with macros
Run any example with:
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.