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.
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