pub trait MessageDispatcher:
Send
+ Sync
+ 'static {
type Workload: Workload<Dispatcher = Self>;
// Required method
fn dispatch<'life0, 'life1, 'async_trait, C>(
workload: &'life0 Self::Workload,
envelope: RpcEnvelope,
ctx: &'life1 C,
) -> Pin<Box<dyn Future<Output = ActorResult<Bytes>> + Send + 'async_trait>>
where C: 'async_trait + Context,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
MessageDispatcher - Message dispatcher interface
Responsible for routing incoming message envelopes to corresponding handler methods.
§Design Characteristics
- Static routing: Implemented via compile-time match statements, no runtime lookup overhead
- Zero-sized type (ZST): Router itself occupies no memory
- Code generation: Implementation is auto-generated by
actr-clicodegen
§Code Generation Example
ⓘ
// Generated by code generator
pub struct EchoServiceRouter<T: EchoServiceHandler>(PhantomData<T>);
#[async_trait]
impl<T: EchoServiceHandler> MessageDispatcher for EchoServiceRouter<T> {
type Workload = EchoServiceWorkload<T>;
async fn dispatch<C: Context>(
workload: &Self::Workload,
envelope: RpcEnvelope,
ctx: &C,
) -> ActorResult<Bytes> {
match envelope.route_key.as_str() {
"echo.EchoService.Echo" => {
let req = EchoRequest::decode(&*envelope.payload)?;
let resp = workload.0.echo(req, ctx).await?;
Ok(resp.encode_to_vec().into())
}
_ => Err(ProtocolError::Actr(ActrError::UnknownRoute {
route_key: envelope.route_key.to_string()
}))
}
}
}Required Associated Types§
Required Methods§
Sourcefn dispatch<'life0, 'life1, 'async_trait, C>(
workload: &'life0 Self::Workload,
envelope: RpcEnvelope,
ctx: &'life1 C,
) -> Pin<Box<dyn Future<Output = ActorResult<Bytes>> + Send + 'async_trait>>where
C: 'async_trait + Context,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn dispatch<'life0, 'life1, 'async_trait, C>(
workload: &'life0 Self::Workload,
envelope: RpcEnvelope,
ctx: &'life1 C,
) -> Pin<Box<dyn Future<Output = ActorResult<Bytes>> + Send + 'async_trait>>where
C: 'async_trait + Context,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Dispatch message to corresponding handler method and execute full request-response cycle
§Responsibilities
- Routing: Select handler method based on
envelope.route_key - Decoding: Deserialize payload into concrete message type
- Dispatching: Invoke workload’s business logic method
- Encoding: Serialize response into bytes
§Parameters
workload: Workload instance (contains user business logic)envelope: Message envelope (contains route_key and payload)ctx: Execution context (generic, supports any Context implementation)
§Returns
Returns serialized response bytes
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.