Context

Struct Context 

Source
pub struct Context<A> { /* private fields */ }
Expand description

Available to the actor in every execution call.

The context is used to interact with the actor system. You can start intervals, send messages to yourself, and stop the actor.

Implementations§

Source§

impl<A: Actor> Context<A>

§Life-cycle
Source

pub fn stop(&self) -> Result<()>

Stop the actor.

Source§

impl<A: Actor> Context<A>

§Child Actors
Source

pub fn add_child(&mut self, child: impl Into<Sender<()>>)

Add a child actor.

This child actor is held until this context is stopped.

Source

pub fn register_child<M: Message<Response = ()>>( &mut self, child: impl Into<Sender<M>>, )

Register a child actor by Message type.

This actor will be held until this actor is stopped via a Sender.

Source

pub fn send_to_children<M>(&mut self, message: M)
where M: Message<Response = ()> + Clone,

Send a message to all child actors registered with this message type.

Source§

impl<A: Actor> Context<A>

§Creating Addrs, Callers and Senders to yourself
Source

pub fn weak_address(&self) -> WeakAddr<A>

Create an weak address to the actor.

Source

pub fn weak_sender<M: Message<Response = ()>>(&self) -> WeakSender<M>
where A: Handler<M>,

Create a weak sender to the actor.

Source

pub fn weak_caller<M: Message<Response = R>, R>(&self) -> WeakCaller<M>
where A: Handler<M>,

Create a weak caller to the actor.

Source§

impl<A: Actor> Context<A>

§Broker Interaction
Source

pub async fn publish<M: Message<Response = ()> + Clone>( &self, message: M, ) -> Result<()>
where A: Handler<M>,

Publish to the broker.

Every actor can publish messages to the broker which will be delivered to all actors that subscribe to the message.

Source

pub async fn subscribe<M: Message<Response = ()> + Clone>( &mut self, ) -> Result<()>
where A: Handler<M>,

Subscribe to a message.

The actor will receive all messages of this type.

Source§

impl<A: Actor> Context<A>

§Task Handling
Source

pub fn spawn_task( &mut self, task: impl Future<Output = ()> + Send + 'static, ) -> TaskHandle

Spawn a task that will be executed in the background.

The task will be aborted when the actor is stopped.

Source

pub fn interval<M: Message<Response = ()> + Clone + Send + 'static>( &mut self, message: M, duration: Duration, ) -> TaskHandle
where A: Handler<M> + Send + 'static,

Send yourself a message at a regular interval.

§Backpressure

Ticks that take longer than 50% of the interval duration to send are skipped to prevent stale message buildup when the actor cannot keep pace with the interval.

Source

pub fn interval_with<M: Message<Response = ()>>( &mut self, message_fn: impl Fn() -> M + Send + Sync + 'static, duration: Duration, ) -> TaskHandle
where A: Handler<M>,

Send yourself a message at a regular interval.

§Backpressure

Ticks that take longer than 50% of the interval duration to send are skipped to prevent stale message buildup when the actor cannot keep pace with the interval.

Warning: don’t do anything expensive in the message function, as it will block the interval.

Source

pub fn delayed_send<M: Message<Response = ()>>( &mut self, message_fn: impl Fn() -> M + Send + Sync + 'static, duration: Duration, ) -> TaskHandle
where A: Handler<M>,

Send yourself a message after a delay.

Source

pub fn delayed_exec<F: Future<Output = ()> + Send + 'static>( &mut self, task: F, duration: Duration, ) -> TaskHandle

Execute a task after a delay.

Source

pub fn stop_task(&mut self, handle: TaskHandle)

Stop a very specific task.

Source§

impl<A: RestartableActor> Context<A>

Life-cycle

Source

pub fn restart(&self) -> Result<()>

Restart the actor.

Depending on the RestartStrategy of the particular Actor this will do either of two things:

RestartOnly: call the Actor::stopped() and Actor::started() methods in order RecreateFromDefault: create a new instance of the actor and start it.

Trait Implementations§

Source§

impl<A> Drop for Context<A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<A> Freeze for Context<A>

§

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§

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