Skip to main content

AskHandler

Trait AskHandler 

Source
pub trait AskHandler<M: Send + 'static, R: Send + 'static>: Send + Sync {
    // Required methods
    fn ask(&self, msg: M) -> BoxFuture<'_, Result<R>>;
    fn ask_with_timeout(
        &self,
        msg: M,
        timeout: Duration,
    ) -> BoxFuture<'_, Result<R>>;
    fn blocking_ask(&self, msg: M, timeout: Option<Duration>) -> Result<R>;
    fn clone_boxed(&self) -> Box<dyn AskHandler<M, R>>;
    fn downgrade(&self) -> Box<dyn WeakAskHandler<M, R>>;
    fn as_control(&self) -> &dyn ActorControl;
    fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result;
}
Expand description

Request-response message handler for strong references (object-safe).

This trait allows storing different actor types that handle the same message type and return the same reply type in a unified collection. The handlers maintain strong references to actors, keeping them alive.

§Example

let handlers: Vec<Box<dyn AskHandler<GetStatus, Status>>> = vec![
    (&actor_a).into(),
    (&actor_b).into(),
];

for handler in &handlers {
    let status = handler.ask(GetStatus).await?;
    println!("Status: {:?}", status);
}

Required Methods§

Source

fn ask(&self, msg: M) -> BoxFuture<'_, Result<R>>

Sends a message and awaits a reply.

Source

fn ask_with_timeout( &self, msg: M, timeout: Duration, ) -> BoxFuture<'_, Result<R>>

Sends a message and awaits a reply with timeout.

Source

fn blocking_ask(&self, msg: M, timeout: Option<Duration>) -> Result<R>

Blocking version of ask.

§Timeout Behavior
  • timeout: None: Uses Tokio’s blocking_send/blocking_recv directly. Most efficient but blocks indefinitely.
  • timeout: Some(duration): Spawns a separate thread with a temporary runtime. Has overhead (~50-200μs for thread + ~1-10μs for runtime) but guarantees bounded waiting.
Source

fn clone_boxed(&self) -> Box<dyn AskHandler<M, R>>

Clone this handler into a new boxed instance.

Source

fn downgrade(&self) -> Box<dyn WeakAskHandler<M, R>>

Downgrade to a weak handler.

Source

fn as_control(&self) -> &dyn ActorControl

Returns a reference to ActorControl for lifecycle management.

Use this to access identity(), is_alive(), stop(), kill(), etc.

Source

fn debug_fmt(&self, f: &mut Formatter<'_>) -> Result

Debug formatting support for trait objects.

Trait Implementations§

Source§

impl<M: Send + 'static, R: Send + 'static> Clone for Box<dyn AskHandler<M, R>>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Send + 'static, R: Send + 'static> Debug for Box<dyn AskHandler<M, R>>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, M> From<&ActorRef<T>> for Box<dyn AskHandler<M, <T as Message<M>>::Reply>>
where T: Actor + Message<M> + 'static, M: Send + 'static, <T as Message<M>>::Reply: Send + 'static,

Source§

fn from(actor_ref: &ActorRef<T>) -> Self

Converts to this type from the input type.
Source§

impl<T, M> From<ActorRef<T>> for Box<dyn AskHandler<M, <T as Message<M>>::Reply>>
where T: Actor + Message<M> + 'static, M: Send + 'static, <T as Message<M>>::Reply: Send + 'static,

Source§

fn from(actor_ref: ActorRef<T>) -> Self

Converts to this type from the input type.

Implementors§

Source§

impl<T, M> AskHandler<M, <T as Message<M>>::Reply> for ActorRef<T>
where T: Actor + Message<M> + 'static, M: Send + 'static, <T as Message<M>>::Reply: Send + 'static,