Expand description
§Act.rs Tokio
X | Twitch | Youtube | Mastodon | GitHub | GitHub Sponsors
Act.rs Tokio is a minimal Tokio oriented actor framework.
§What Is An Actor?
An actor is an object that runs in its own thread or task. You would usually communicate with it via channels.
§Related Actor Crates
You can also create task based actors:
This crate uses types from Act.rs.
See also: Act.rs smol
§An Example
Here’s the first example in the Act.rs readme adapted to implement a macro generated actor:
//Adapted from the std TwoPlusTwoActorState ThreadActor test (in Act.rs (act_rs)).
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
use act_rs_tokio::impl_task_actor;
//Need for impl_task_actor:
use act_rs::impl_pre_and_post_run_async;
use pastey::paste;
use tokio::task::JoinHandle;
struct TwoPlusTwoActorState
{
number: u32,
client_sender: UnboundedSender<String>
}
impl TwoPlusTwoActorState
{
pub fn new(client_sender: UnboundedSender<String>) -> Self
{
Self
{
number: 2,
client_sender
}
}
impl_pre_and_post_run_async!();
async fn run_async(&mut self) -> bool
{
if self.number < 4
{
self.number += 2;
let message = format!("two plus two is: {}", self.number);
if let Err(_) = self.client_sender.send(message)
{
return false;
}
return true;
}
false
}
}
impl_task_actor!(TwoPlusTwoActor);
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
async fn main()
{
let (sender, mut receiver) = unbounded_channel();
TwoPlusTwoActor::spawn(TwoPlusTwoActorState::new(sender));
let res = receiver.recv().await.expect("Error: Message not delivered");
println!("{}", res);
}
§Examples
§Todo
- Add more documentation
- Add examples
- Add more tests
- Cleanup the code
§Coding Style
This project uses a coding style that emphasises the use of white space over keeping the line and column counts as low as possible.
So this:
fn bar() {}
fn foo()
{
bar();
}
Not this:
fn bar() {}
fn foo()
{
bar();
}
§License
Licensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0 (see also: https://www.tldrlegal.com/license/apache-license-2-0-apache-2-0))
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT (see also: https://www.tldrlegal.com/license/mit-license))
at your discretion
§Contributing
Please clone the repository and create an issue explaining what feature or features you’d like to add or bug or bugs you’d like to fix and perhaps how you intend to implement these additions or fixes. Try to include details though it doesn’t need to be exhaustive and we’ll take it from there (dependant on availability).
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Macros§
- enter
- Calls “enter()” on the provided “$to_enter” parameter in a block, storing the result in an invariant. Then the provided “$expr” expression parameter is then run in this block.
- impl_
task_ actor - Generates an async oriented actor to be instantiated within an async runtime context.
- impl_
task_ actor_ build_ state - Similar to impl_task_actor, but it also generates a spawn_and_build_state method for building the actor state in another task.
- impl_
task_ actor_ build_ state_ and_ catch_ unwind - impl_
task_ actor_ build_ state_ and_ catch_ unwind_ flexible - impl_
task_ actor_ build_ state_ flexible - impl_
task_ actor_ build_ state_ with_ spawn - impl_
task_ actor_ build_ state_ with_ spawn_ catch_ unwind - impl_
task_ actor_ build_ state_ with_ spawn_ catch_ unwind_ flexible - impl_
task_ actor_ build_ state_ with_ spawn_ flexible - impl_
task_ actor_ catch_ unwind - impl_
task_ actor_ catch_ unwind_ flexible - impl_
task_ actor_ flexible
Structs§
- Blocking
Actor - A blocking thread based actor.
- Task
Actor async-trait - A task based actor.