[][src]Struct xtra::Context

pub struct Context<A: Actor> { /* fields omitted */ }

Context is used to control how the actor is managed and to get the actor's address from inside of a message handler.

Implementations

impl<A: Actor> Context<A>[src]

pub fn stop(&mut self)[src]

Stop the actor as soon as it has finished processing current message. This will mean that the Actor::stopping method will be called. If that returns KeepRunning::No, any subsequent attempts to send messages to this actor will return the Disconnected error.

pub fn address(&self) -> Option<Address<A>>[src]

Get an address to the current actor if the actor is still running.

pub async fn yield_once<'_, '_>(&'_ mut self, act: &'_ mut A)[src]

Yields to the manager to handle one message.

pub async fn handle_while<'_, '_, F, R>(
    &'_ mut self,
    act: &'_ mut A,
    __arg2: F
) -> R where
    F: Future<Output = R> + Unpin
[src]

Handle any incoming messages for the actor while running a given future.

Example

use async_trait::async_trait;
use xtra::prelude::*;
use smol::Timer;
use std::time::Duration;

struct Initialized(Address<ActorA>);
impl Message for Initialized {
    type Result = ();
}

struct Hello;
impl Message for Hello {
    type Result = ();
}

struct ActorA {
    actor_b: Address<ActorB>,
}
impl Actor for ActorA {}

#[async_trait]
impl Handler<Hello> for ActorA {
    async fn handle(&mut self, _: Hello, ctx: &mut Context<Self>) {
        println!("ActorA: Hello");
        ctx.handle_while(self, self.actor_b.send(Hello))
            .await
            .unwrap();
    }
}

struct ActorB;
impl Actor for ActorB {}

#[async_trait]
impl Handler<Initialized> for ActorB {
    async fn handle(&mut self, m: Initialized, ctx: &mut Context<Self>) {
        println!("ActorB: Initialized");
        let actor_a = m.0;
        ctx.handle_while(self, actor_a.send(Hello)).await.unwrap();
    }
}

#[async_trait]
impl Handler<Hello> for ActorB {
    async fn handle(&mut self, _: Hello, _: &mut Context<Self>) {
        println!("ActorB: Hello");
    }
}

#[smol_potat::main]
async fn main() {
    let actor_b = ActorB {}.spawn();
    let actor_a = ActorA {
        actor_b: actor_b.clone(),
    }
    .spawn();
    actor_b.send(Initialized(actor_a.clone())).await.unwrap();
}

pub fn notify_immediately<M>(&mut self, msg: M) where
    M: Message,
    A: Handler<M> + Send
[src]

Notify this actor with a message that is handled synchronously before any other messages from the general queue are processed (therefore, immediately). If multiple notify_immediately messages are queued, they will still be processed in the order that they are queued (i.e the immediate priority is only over other messages).

pub fn notify_later<M>(&mut self, msg: M) where
    M: Message,
    A: Handler<M> + Send
[src]

Notify this actor with a message that is handled after any other messages from the general queue are processed. This is almost equivalent to calling send on Context::address(), but will never fail to send the message.

pub fn notify_interval<F, M>(&mut self, duration: Duration, constructor: F) where
    F: Send + 'static + Fn() -> M,
    M: Message,
    A: Handler<M> + Send
[src]

This is supported on feature="with-tokio-0_2" and feature="with-async_std-1" and feature="with-wasm_bindgen-0_2" and feature="with-smol-0_1" only.

Notify the actor with a synchronously handled message every interval until it is stopped (either directly with Context::stop, or for a lack of strong Addresses). This does not take priority over other messages.

pub fn notify_after<M>(&mut self, duration: Duration, notification: M) where
    M: Message,
    A: Handler<M> + Send
[src]

This is supported on feature="with-tokio-0_2" and feature="with-async_std-1" and feature="with-wasm_bindgen-0_2" and feature="with-smol-0_1" only.

Notify the actor with a synchronously handled message after a certain duration has elapsed. This does not take priority over other messages.

Auto Trait Implementations

impl<A> !RefUnwindSafe for Context<A>

impl<A> Send for Context<A>

impl<A> !Sync for Context<A>

impl<A> Unpin for Context<A>

impl<A> !UnwindSafe for Context<A>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.