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§
Sourcefn ask_with_timeout(
&self,
msg: M,
timeout: Duration,
) -> BoxFuture<'_, Result<R>>
fn ask_with_timeout( &self, msg: M, timeout: Duration, ) -> BoxFuture<'_, Result<R>>
Sends a message and awaits a reply with timeout.
Sourcefn blocking_ask(&self, msg: M, timeout: Option<Duration>) -> Result<R>
fn blocking_ask(&self, msg: M, timeout: Option<Duration>) -> Result<R>
Blocking version of ask.
§Timeout Behavior
timeout: None: Uses Tokio’sblocking_send/blocking_recvdirectly. 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.
Sourcefn clone_boxed(&self) -> Box<dyn AskHandler<M, R>>
fn clone_boxed(&self) -> Box<dyn AskHandler<M, R>>
Clone this handler into a new boxed instance.
Sourcefn downgrade(&self) -> Box<dyn WeakAskHandler<M, R>>
fn downgrade(&self) -> Box<dyn WeakAskHandler<M, R>>
Downgrade to a weak handler.
Sourcefn as_control(&self) -> &dyn ActorControl
fn as_control(&self) -> &dyn ActorControl
Returns a reference to ActorControl for lifecycle management.
Use this to access identity(), is_alive(), stop(), kill(), etc.