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>
§Child Actors
impl<A: Actor> Context<A>
§Child Actors
Sourcepub fn add_child(&mut self, child: impl Into<Sender<()>>)
pub fn add_child(&mut self, child: impl Into<Sender<()>>)
Add a child actor.
This child actor is held until this context is stopped.
Sourcepub fn register_child<M: Message<Response = ()>>(
&mut self,
child: impl Into<Sender<M>>,
)
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.
Sourcepub fn send_to_children<M>(&mut self, message: M)
pub fn send_to_children<M>(&mut self, message: M)
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
impl<A: Actor> Context<A>
§Creating Addrs, Callers and Senders to yourself
Sourcepub fn weak_address(&self) -> WeakAddr<A>
pub fn weak_address(&self) -> WeakAddr<A>
Create an weak address to the actor.
Sourcepub fn weak_sender<M: Message<Response = ()>>(&self) -> WeakSender<M>where
A: Handler<M>,
pub fn weak_sender<M: Message<Response = ()>>(&self) -> WeakSender<M>where
A: Handler<M>,
Create a weak sender to the actor.
Sourcepub fn weak_caller<M: Message<Response = R>, R>(&self) -> WeakCaller<M>where
A: Handler<M>,
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
impl<A: Actor> Context<A>
§Broker Interaction
Source§impl<A: Actor> Context<A>
§Task Handling
impl<A: Actor> Context<A>
§Task Handling
Sourcepub fn spawn_task(
&mut self,
task: impl Future<Output = ()> + Send + 'static,
) -> TaskHandle
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.
Sourcepub fn interval<M: Message<Response = ()> + Clone + Send + 'static>(
&mut self,
message: M,
duration: Duration,
) -> TaskHandle
pub fn interval<M: Message<Response = ()> + Clone + Send + 'static>( &mut self, message: M, duration: Duration, ) -> TaskHandle
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.
Sourcepub fn interval_with<M: Message<Response = ()>>(
&mut self,
message_fn: impl Fn() -> M + Send + Sync + 'static,
duration: Duration,
) -> TaskHandlewhere
A: Handler<M>,
pub fn interval_with<M: Message<Response = ()>>(
&mut self,
message_fn: impl Fn() -> M + Send + Sync + 'static,
duration: Duration,
) -> TaskHandlewhere
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.
Sourcepub fn delayed_send<M: Message<Response = ()>>(
&mut self,
message_fn: impl Fn() -> M + Send + Sync + 'static,
duration: Duration,
) -> TaskHandlewhere
A: Handler<M>,
pub fn delayed_send<M: Message<Response = ()>>(
&mut self,
message_fn: impl Fn() -> M + Send + Sync + 'static,
duration: Duration,
) -> TaskHandlewhere
A: Handler<M>,
Send yourself a message after a delay.
Sourcepub fn delayed_exec<F: Future<Output = ()> + Send + 'static>(
&mut self,
task: F,
duration: Duration,
) -> TaskHandle
pub fn delayed_exec<F: Future<Output = ()> + Send + 'static>( &mut self, task: F, duration: Duration, ) -> TaskHandle
Execute a task after a delay.
Sourcepub fn stop_task(&mut self, handle: TaskHandle)
pub fn stop_task(&mut self, handle: TaskHandle)
Stop a very specific task.
Source§impl<A: RestartableActor> Context<A>
Life-cycle
impl<A: RestartableActor> Context<A>
Life-cycle
Sourcepub fn restart(&self) -> Result<()>
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.