Skip to main content

Handle

Struct Handle 

Source
pub struct Handle<Msg> { /* private fields */ }
Expand description

A handle to send messages to or stop an Actor.

Implementations§

Source§

impl<Msg> Handle<Msg>

Source

pub fn stop(&self)

Stops the Actor associated with this handle. Does not wait for the actor to finish.

§Example
handle.stop();
Source

pub fn restart(&self)

Restarts the Actor by re-running Actor::init and Actor::sources. Does not wait for the actor to finish.

§Example
handle.restart();
Source

pub fn is_alive(&self) -> bool

Returns true if the Actor is still running.

§Example
if handle.is_alive() {
    handle.send(Msg::Ping);
}
Source

pub fn send<M: Into<Msg>>(&self, msg: M) -> bool

Sends a message to the Actor, returning true if the message was delivered or false if the actor is no longer running. Takes advantage of From<_> implementations on the message type.

§Example
// Given `#[derive(From)] enum Msg { Inc(u32) }`:
handle.send(Msg::Inc(1));
handle.send(1u32); // works via From<u32>
Source

pub fn send_in<M>(&self, msg: M, duration: Duration)
where Msg: 'static + Send, M: 'static + Send + Into<Msg>,

Sends a message to the Actor after the given duration, failing silently if it is no longer running.

§Example
handle.send_in(Msg::Timeout, Duration::from_secs(5));
Source

pub async fn req<Req, Res>(&self, req: Req) -> Result<Res, ReqErr>
where Msg: From<Request<Req, Res>>,

Sends a request and awaits a response. Requires Msg: From<Request<Req, Res>>.

§Example
#[derive(From)]
enum Msg {
    GetCount(Request<(), u32>),
}

// sender side:
let count: u32 = handle.req(()).await?;

// receiver side, inside handle():
Msg::GetCount(req) => req.reply(self.count),
Source

pub async fn reqw<F, Req, Res>( &self, to_req: F, req: Req, ) -> Result<Res, ReqErr>
where F: Fn(Request<Req, Res>) -> Msg,

Like Handle::req, but uses a wrapper function to convert the Request into the message type. Useful when the message variant can’t implement From<Request<Req, Res>>.

§Example
enum Msg {
    GetCount(Request<(), u32>),
}

let count: u32 = handle.reqw(Msg::GetCount, ()).await?;
Source

pub async fn req_timeout<Req, Res>( &self, req: Req, timeout: Duration, ) -> Result<Res, ReqErr>
where Msg: From<Request<Req, Res>>,

Like Handle::req, but fails with ReqErr::Timeout if no response within the given Duration.

§Example
let count: u32 = handle.req_timeout((), Duration::from_secs(1)).await?;
Source

pub async fn reqw_timeout<F, Req, Res>( &self, to_req: F, req: Req, timeout: Duration, ) -> Result<Res, ReqErr>
where F: Fn(Request<Req, Res>) -> Msg,

Like Handle::reqw, but fails with ReqErr::Timeout if no response within the given Duration.

§Example
let count: u32 = handle.reqw_timeout(Msg::GetCount, (), Duration::from_secs(1)).await?;

Trait Implementations§

Source§

impl<Msg> Clone for Handle<Msg>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<Msg> Freeze for Handle<Msg>

§

impl<Msg> RefUnwindSafe for Handle<Msg>

§

impl<Msg> Send for Handle<Msg>
where Msg: Send,

§

impl<Msg> Sync for Handle<Msg>
where Msg: Send,

§

impl<Msg> Unpin for Handle<Msg>

§

impl<Msg> UnsafeUnpin for Handle<Msg>

§

impl<Msg> UnwindSafe for Handle<Msg>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.