Provides an Actor
type that wraps a state and allows mutating it
in turns using invoke
.
It is recommended to create a wrapper type around the Actor
,
and implement async functions that use invoke
to interact with
the inner private state.
```rust use simple_actor::Actor;
pub struct Adder(Actor
impl Adder { pub fn new(initialvalue: u32) -> Self { let (actor, driver) = Actor::new(initialvalue); tokio::spawn(driver); Self(actor) }
pub async fn add(&self, x: u32) {
let _ = self.0.invoke(move |state| *state += x).await;
}
pub async fn result(&self) -> Option<u32> {
self.0.invoke(move |state| *state).await
}
pub fn shutdown(&self) {
self.0.shutdown()
}
}
async fn main() { let adder = Adder::new(5);
adder.add(3).await;
assert_eq!(adder.result().await, Some(8));
adder.add(2).await;
assert_eq!(adder.result().await, Some(10));
adder.shutdown();
assert_eq!(adder.result().await, None);
} ```
This crate is inspired by [ghost_actor
], with a simpler implementation and
API. This crate invoke
function returns None
if the actor is down, which
avoids dealing with error type conversions.