ActorRef

Trait ActorRef 

Source
pub trait ActorRef: Clone {
    // Required methods
    fn id(&self) -> u64;
    fn is_alive(&self) -> bool;
    fn current() -> Option<Self>;
    fn link_child<R: ActorRef>(&self, child: &R);
    fn unlink_child<R: ActorRef>(&self, child: &R);
    fn link_together<R: ActorRef>(&self, actor_ref: &R);
    fn unlink_together<R: ActorRef>(&self, actor_ref: &R);
    fn notify_link_died(
        &self,
        id: u64,
        reason: ActorStopReason,
    ) -> Result<(), SendError>;
    fn stop_gracefully(&self) -> Result<(), SendError>;
    fn kill(&self);
    async fn wait_for_stop(&self);
}
Expand description

Provides functionality to stop and wait for an actor to complete based on an actor ref.

This trait is automatically implemented by the #[actor] macro.

Required Methods§

Source

fn id(&self) -> u64

Returns the actor identifier.

Source

fn is_alive(&self) -> bool

Returns whether the actor is currently alive.

Source

fn current() -> Option<Self>

Returns the current actor ref, if called from the running actor.

Links this actor with a child, notifying the child actor if the parent dies through Actor::on_link_died, but not visa versa.

Unlinks the actor with a previously linked actor.

Links two actors with one another, notifying eachother if either actor dies through Actor::on_link_died.

This operation is atomic.

Unlinks two previously linked actors.

This operation is atomic.

Notifies the actor that one of its links died.

This is called automatically when an actor dies.

Source

fn stop_gracefully(&self) -> Result<(), SendError>

Signals the actor to stop after processing all messages currently in its mailbox.

This method sends a special stop message to the end of the actor’s mailbox, ensuring that the actor will process all preceding messages before stopping. Any messages sent after this stop signal will be ignored and dropped. This approach allows for a graceful shutdown of the actor, ensuring all pending work is completed before termination.

Source

fn kill(&self)

Kills the actor immediately.

This method aborts the actor immediately. Messages in the mailbox will be ignored and dropped.

The actors on_stop hook will still be called.

Note: If the actor is in the middle of processing a message, it will abort processing of that message.

Source

async fn wait_for_stop(&self)

Waits for the actor to finish processing and stop.

This method suspends execution until the actor has stopped, ensuring that any ongoing processing is completed and the actor has fully terminated. This is particularly useful in scenarios where it’s necessary to wait for an actor to clean up its resources or complete its final tasks before proceeding.

Note: This method does not initiate the stop process; it only waits for the actor to stop. You should signal the actor to stop using stop_gracefully or kill before calling this method.

§Examples
// Assuming `actor.stop_gracefully().await` has been called earlier
actor.wait_for_stop().await;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§