pub struct Ctx<P>where
P: Actor,{ /* private fields */ }Expand description
The context surrounding the current Actor.
Provides a collection of methods that allow you to:
- spawn other actors as children of the current actor
- access the
Handle<_>for the currrent actor - access this actor’s props
- clear this actor’s mailbox
Implementations§
Source§impl<P: Actor> Ctx<P>
impl<P: Actor> Ctx<P>
Sourcepub fn subscribe<T>(&mut self, topic: &str) -> Result<(), PubSubError>
pub fn subscribe<T>(&mut self, topic: &str) -> Result<(), PubSubError>
Subscribes this actor to a topic. Messages published to the topic will be
cloned, converted via From<T> into this actor’s Msg type, and delivered
to this actor’s mailbox.
Returns PubSubError::TypeMismatch if the topic was already created with
a different message type.
Sourcepub fn publish<T: Send + 'static>(
&self,
topic: &str,
msg: T,
) -> Result<(), PubSubError>
pub fn publish<T: Send + 'static>( &self, topic: &str, msg: T, ) -> Result<(), PubSubError>
Publishes a message to all subscribers of a topic. The message is cloned for each subscriber.
Publishing to a topic with no subscribers is a no-op and returns Ok(()).
Returns PubSubError::TypeMismatch if the topic exists with a different type.
Source§impl<P> Ctx<P>where
P: Actor,
impl<P> Ctx<P>where
P: Actor,
Sourcepub fn clear_mailbox(&self)
pub fn clear_mailbox(&self)
Drains all pending messages from this Actor’s mailbox. Useful during
restarts to discard stale messages via Actor::init.
§Example
async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
ctx.clear_mailbox();
Ok(MyActor::default())
}Sourcepub fn actor<'a, Child>(
&'a mut self,
props: Child::Props,
) -> SpawnBuilder<'a, P, Child>where
Child: Actor,
pub fn actor<'a, Child>(
&'a mut self,
props: Child::Props,
) -> SpawnBuilder<'a, P, Child>where
Child: Actor,
Creates a SpawnBuilder for spawning a child Actor. The actor type is passed
as a generic parameter and its props as the argument. The child is supervised
by the current actor and will be stopped when the parent stops.
§Example
let handle = ctx.actor::<Worker>(WorkerProps { id: 1 })
.supervision(Supervision::Restart {
max: Limit::Amount(3),
backoff: Backoff::None,
})
.spawn();Sourcepub fn restart_children(&self)
pub fn restart_children(&self)
Restarts all child actors immediately, bypassing their supervision strategy.
Each child will re-run its Actor::init with the same props.
This is fire-and-forget: it does not wait for children to finish restarting.
Sourcepub async fn stop_children(&mut self)
pub async fn stop_children(&mut self)
Stops all child actors and waits for each to fully terminate before returning.
Sourcepub fn task<F>(&mut self, f: F)
pub fn task<F>(&mut self, f: F)
Spawns a background async task. On completion, its Ok value is delivered
as a message to this Actor; its Err triggers the supervision strategy
that this actor’s parent has set for it.
Tasks are aborted when the actor stops, but survive restarts. If the
actor is restarted (via supervision or Ctx::restart_children), in-flight
tasks from the previous incarnation will continue running and their results
will still be delivered to the restarted actor’s handle().
§Example
ctx.task(async {
let data = reqwest::get("https://example.com").await?.text().await?;
Ok(Msg::Fetched(data))
});Sourcepub fn get_handle_for<A: Actor>(&self) -> Result<Handle<A::Msg>, RegistryError>
pub fn get_handle_for<A: Actor>(&self) -> Result<Handle<A::Msg>, RegistryError>
Looks up a registered Actor’s Handle by its type. The actor must have been
spawned with SpawnBuilder::spawn_registered.
§Example
let logger = ctx.get_handle_for::<Logger>()?;
logger.send(LogMsg::Info("hello".into()));Sourcepub fn get_handle<Msg: Send + 'static>(
&self,
name: &str,
) -> Result<Handle<Msg>, RegistryError>
pub fn get_handle<Msg: Send + 'static>( &self, name: &str, ) -> Result<Handle<Msg>, RegistryError>
Looks up a registered Actor’s Handle by name. The actor must have been
spawned with SpawnBuilder::spawn_named.
§Example
let worker = ctx.get_handle::<WorkerMsg>("worker-1")?;
worker.send(WorkerMsg::Start);Sourcepub fn send<A: Actor>(
&self,
msg: impl Into<A::Msg>,
) -> Result<(), RegistryError>
pub fn send<A: Actor>( &self, msg: impl Into<A::Msg>, ) -> Result<(), RegistryError>
Sends a message to a registered Actor looked up by type.
§Example
// Assuming MetricsCollector was spawned with spawn_registered():
// ctx.actor::<MetricsCollector>(props).spawn_registered()?;
// Any actor in the system can then send to it by type:
ctx.send::<MetricsCollector>(MetricsMsg::RecordLatency(42))?;