rsActor: A Simplified Actor Framework for Rust
rsActor is a lightweight, Tokio-based actor framework in Rust, inspired by Kameo. It prioritizes simplicity and ease of use for local, in-process actor systems by stripping away more complex features like remote actors and supervision trees.
Note: This project is in a very early stage of development. APIs are subject to change, and features are still being actively developed.
Core Features
- Minimalist Actor System: Focuses on the core actor model primitives.
- Asynchronous Message Passing:
ask: Send a message and asynchronously await a reply.tell: Send a message without waiting for a reply (fire-and-forget).
- Actor Lifecycle: Actors implement
on_startandon_stophooks. - Graceful & Immediate Termination: Actors can be stopped gracefully (processing remaining messages) or killed immediately.
- Macro-Assisted Message Handling: The
impl_message_handler!macro simplifies routing different message types to their respective handlers within an actor. - Tokio-Native: Built exclusively for the
tokioasynchronous runtime.
Comparison with Kameo
While rsActor shares the goal of providing an actor system, it makes different design choices compared to Kameo:
- No Remote Actor Support:
rsActoris for local actors only. - Non-Generic
ActorRef:rsActor'sActorRefis a concrete type. Messages are dynamically typed (Box<dyn Any + Send>), with runtime type checking for replies inaskcalls. Kameo uses a genericActorRef<A: Actor>. - No Actor Linking or Supervision:
rsActordoes not include built-in support for linking actor lifecycles or supervision strategies. - Tokio-Specific:
rsActoris tightly coupled with Tokio. Kameo is designed for broader async runtime compatibility. impl_message_handler!Macro:rsActoruses a macro to generate the boilerplate for handling multiple message types, whereas Kameo might use generic trait implementations per message.
Getting Started
1. Add Dependency
Add rsActor to your Cargo.toml:
[]
= { = "https://your-repo-url/rsActor.git" } # Or path, or crates.io version when published
= { = "1", = ["full"] }
= "1.0"
= "0.4"
= "0.11" # Optional, for logging examples
(Note: Update the dependency source once rsActor is published or if you're using a local path.)
2. Basic Usage Example
Here's a simple counter actor:
use ; // Updated import
use Result;
use info;
// Define your actor struct
// Implement the Actor trait
// Define message types
; // Message to increment the counter by a value
; // Message to get the current count
// Implement Message<T> for CounterActor to handle IncrementMsg
// Implement Message<T> for CounterActor to handle GetCountMsg
// Use the rsactor::impl_message_handler! macro to generate boilerplate
// for routing IncrementMsg and GetCountMsg to their respective handlers.
impl_message_handler!;
async
Running the Example
The project includes a basic example in examples/basic.rs. You can run it using:
This will demonstrate actor creation, message passing, and lifecycle logging.
Motivation
The primary goal of this project is to provide a streamlined and efficient actor-based framework by focusing on core functionalities while reducing complexity. This makes it suitable for scenarios where a full-featured actor system like actix might be overkill, but the actor model's benefits (concurrency, state encapsulation) are still desired.
License
This project is licensed under the Apache License 2.0. You can find a copy of the license in the LICENSE-APACHE file.
Contribution
Contributions are welcome! Feel free to open issues and submit pull requests to improve the project.