pub struct Handle<A> { /* private fields */ }Expand description
A clonable handle to schedule actions onto an actor of type A.
Use Handle::channel to create a (Handle<A>, mpsc::Receiver<Action<A>>)
pair, then build your actor with the receiver and spawn its run loop. The
handle may be cloned and used concurrently from many tasks/threads.
Implementations§
Source§impl<A> Handle<A>where
A: Send + 'static,
impl<A> Handle<A>where
A: Send + 'static,
Sourcepub fn channel(capacity: usize) -> (Self, Receiver<Action<A>>)
pub fn channel(capacity: usize) -> (Self, Receiver<Action<A>>)
Create a new actor handle and mailbox receiver with the given capacity.
Returns the (Handle<A>, mpsc::Receiver<Action<A>>) pair. Pass the
receiver to your actor and spawn its run loop like this:
ⓘ
let (handle, rx) = Handle::<MyActor>::channel(128);
let actor = MyActor { /* ... */ rx };
tokio::spawn(async move { let _ = actor.run().await; });Sourcepub async fn call<R, F>(&self, f: F) -> Result<R>
pub async fn call<R, F>(&self, f: F) -> Result<R>
Schedule an action to run on the actor and await its result.
fis a closure that receives&mut Aand returns a boxed future yieldinganyhow::Result<R>. Use theact!andact_ok!macros to write these concisely.- If the actor panics while processing the action, the panic is caught
and returned as an
anyhow::Errorwith the call site location. - If the actor task has stopped, an error is returned.
Examples found in repository?
examples/readme.rs (lines 24-26)
23 pub async fn increment(&self, by: i32) -> Result<()> {
24 self.handle.call(act_ok!(actor => async move {
25 actor.value += by;
26 })).await
27 }
28
29 pub async fn get(&self) -> Result<i32> {
30 self.handle.call(act_ok!(actor => async move {
31 actor.value
32 })).await
33 }
34
35 pub async fn set_positive(&self, value: i32) -> Result<()> {
36 self.handle.call(act!(actor => async move {
37 if value <= 0 {
38 Err(anyhow!("Value must be positive"))
39 } else {
40 actor.value = value;
41 Ok(())
42 }
43 })).await
44 }Trait Implementations§
Auto Trait Implementations§
impl<A> Freeze for Handle<A>
impl<A> RefUnwindSafe for Handle<A>
impl<A> Send for Handle<A>
impl<A> Sync for Handle<A>
impl<A> Unpin for Handle<A>
impl<A> UnwindSafe for Handle<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more