26 releases (16 stable)
1.2.4 | Mar 14, 2024 |
---|---|
1.2.2 | Feb 20, 2024 |
1.2.1 | Nov 13, 2023 |
0.3.0 | Jul 12, 2023 |
#409 in Asynchronous
162 downloads per month
285KB
5.5K
SLoC
interthread
The actor
macro provided by this crate automates the implementation of an Actor Model for a given struct or enum. It handles the intricacies of message routing and synchronization, empowering developers to swiftly prototype the core functionality of their applications.
This fast sketching capability is
particularly useful when exploring different design options,
experimenting with concurrency models, or implementing
proof-of-concept systems. Not to mention, the cases where
the importance of the program lies in the result of its work
rather than its execution.
Examples
Filename: Cargo.toml
[dependencies]
interthread = "1.2.4"
oneshot = "0.1.6"
Filename: main.rs
pub struct MyActor {
value: i8,
}
#[interthread::actor] // <- this is it
impl MyActor {
pub fn new( v: i8 ) -> Self {
Self { value: v }
}
pub fn increment(&mut self) {
self.value += 1;
}
pub fn add_number(&mut self, num: i8) -> i8 {
self.value += num;
self.value
}
pub fn get_value(&self) -> i8 {
self.value
}
}
// uncomment to see the generated code
//#[interthread::example(path="src/main.rs")]
fn main() {
let actor = MyActorLive::new(5);
let mut actor_a = actor.clone();
let mut actor_b = actor.clone();
let handle_a = std::thread::spawn( move || {
actor_a.increment();
});
let handle_b = std::thread::spawn( move || {
actor_b.add_number(5)
});
let _ = handle_a.join();
let hb = handle_b.join().unwrap();
// we never know which thread will
// be first to call the actor so
// hb = 10 or 11
assert!(hb >= 10);
assert_eq!(actor.get_value(), 11);
}
Be sure to explore the example
macro provided by this crate, as it proves to be an invaluable tool for debugging and visualizing the code generated by the actor macro. Additionally, the edit
option, when combined with the file
option, facilitates writing the requested part of the generated code to the file. To substitute the macro with code on file, utilize edit(file)
within the macro.
The same example can be run in
with the only difference being that the methods will
be marked as async
and need to be await
ed for
asynchronous execution.
Dependencies
~3MB
~64K SLoC