Skip to main content

ActorRef

Trait ActorRef 

Source
pub trait ActorRef<A: Actor>:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn id(&self) -> ActorId;
    fn name(&self) -> String;
    fn is_alive(&self) -> bool;
    fn stop(&self);
    fn tell<M>(&self, msg: M) -> Result<(), ActorSendError>
       where A: Handler<M>,
             M: Message<Reply = ()>;
    fn ask<M>(
        &self,
        msg: M,
        cancel: Option<CancellationToken>,
    ) -> Result<AskReply<M::Reply>, ActorSendError>
       where A: Handler<M>,
             M: Message;
    fn expand<M, OutputItem>(
        &self,
        msg: M,
        buffer: usize,
        batch_config: Option<BatchConfig>,
        cancel: Option<CancellationToken>,
    ) -> Result<BoxStream<OutputItem>, ActorSendError>
       where A: ExpandHandler<M, OutputItem>,
             M: Send + 'static,
             OutputItem: Send + 'static;
    fn reduce<InputItem, Reply>(
        &self,
        input: BoxStream<InputItem>,
        buffer: usize,
        batch_config: Option<BatchConfig>,
        cancel: Option<CancellationToken>,
    ) -> Result<AskReply<Reply>, ActorSendError>
       where A: ReduceHandler<InputItem, Reply>,
             InputItem: Send + 'static,
             Reply: Send + 'static;
    fn transform<InputItem, OutputItem>(
        &self,
        input: BoxStream<InputItem>,
        buffer: usize,
        batch_config: Option<BatchConfig>,
        cancel: Option<CancellationToken>,
    ) -> Result<BoxStream<OutputItem>, ActorSendError>
       where A: TransformHandler<InputItem, OutputItem>,
             InputItem: Send + 'static,
             OutputItem: Send + 'static;

    // Provided method
    fn pending_messages(&self) -> usize { ... }
}
Expand description

A reference to a running actor of type A.

ActorRef<A> is typed to the actor struct. This enables sending any message type M where A: Handler<M>.

Required Methods§

Source

fn id(&self) -> ActorId

The actor’s unique identity.

Source

fn name(&self) -> String

The actor’s name (as given to spawn).

Source

fn is_alive(&self) -> bool

Check if the actor is still alive.

Source

fn stop(&self)

Gracefully stop the actor. Closes the mailbox and triggers on_stop.

Source

fn tell<M>(&self, msg: M) -> Result<(), ActorSendError>
where A: Handler<M>, M: Message<Reply = ()>,

Fire-and-forget: deliver a message to the actor. The message must have Reply = () (no reply expected).

Source

fn ask<M>( &self, msg: M, cancel: Option<CancellationToken>, ) -> Result<AskReply<M::Reply>, ActorSendError>
where A: Handler<M>, M: Message,

Request-reply: send a message and await the reply.

Returns an AskReply future that resolves to the handler’s reply. Usage: let reply = actor.ask(msg, None)?.await?;

Pass a CancellationToken to cooperatively cancel the operation.

Source

fn expand<M, OutputItem>( &self, msg: M, buffer: usize, batch_config: Option<BatchConfig>, cancel: Option<CancellationToken>, ) -> Result<BoxStream<OutputItem>, ActorSendError>
where A: ExpandHandler<M, OutputItem>, M: Send + 'static, OutputItem: Send + 'static,

Request-stream: send a request and receive a stream of responses. buffer controls the channel capacity (backpressure).

Pass batch_config to enable batching (reduces per-item overhead for remote actors). None means unbatched per-item delivery.

Pass a CancellationToken to cooperatively cancel the stream.

Source

fn reduce<InputItem, Reply>( &self, input: BoxStream<InputItem>, buffer: usize, batch_config: Option<BatchConfig>, cancel: Option<CancellationToken>, ) -> Result<AskReply<Reply>, ActorSendError>
where A: ReduceHandler<InputItem, Reply>, InputItem: Send + 'static, Reply: Send + 'static,

Client-streaming (feed): stream items to the actor and receive a reply.

The caller provides items via input. The actor consumes them via StreamReceiver and returns a single reply when the stream ends. buffer controls the internal channel capacity (backpressure).

Pass batch_config to enable batching (reduces per-item overhead for remote actors). None means unbatched per-item delivery.

Pass a CancellationToken to cooperatively cancel the feed.

Usage: let reply = actor.reduce::<u64, u64>(input, 8, None, None)?.await?;

Source

fn transform<InputItem, OutputItem>( &self, input: BoxStream<InputItem>, buffer: usize, batch_config: Option<BatchConfig>, cancel: Option<CancellationToken>, ) -> Result<BoxStream<OutputItem>, ActorSendError>
where A: TransformHandler<InputItem, OutputItem>, InputItem: Send + 'static, OutputItem: Send + 'static,

Transform: stream items to the actor and receive a stream of outputs.

The caller provides items via input. For each input item the actor’s TransformHandler::handle_transform may push zero or more output items. When the input stream ends, TransformHandler::on_transform_complete is called to allow final items to be emitted.

buffer controls the internal channel capacity (backpressure).

Pass batch_config to enable batching on the output stream. Batching groups output items into vectors by max_items or max_delay, amortizing serialization and network costs for remote actors. None means unbatched per-item delivery.

Pass a CancellationToken to cooperatively cancel the transform.

Usage:

let output: BoxStream<String> = actor.transform::<i32, String>(input, 8, None, None)?;

Provided Methods§

Source

fn pending_messages(&self) -> usize

Approximate number of messages pending in the actor’s mailbox.

This is a best-effort snapshot that may be stale immediately after reading. Used by PoolRouting::LeastLoaded to route to the least-busy worker. Returns 0 by default; adapters that can query their mailbox depth should override this.

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§

Source§

impl<A: Actor + Sync> ActorRef<A> for RemoteActorRef<A>

Source§

impl<A: Actor + Sync, L: ActorRef<A>> ActorRef<A> for WorkerRef<A, L>

Source§

impl<A: Actor, R: ActorRef<A>> ActorRef<A> for PoolRef<A, R>

Source§

impl<A: Actor, R: ActorRef<A>> ActorRef<A> for VirtualPoolRef<A, R>