pub trait Handler<Msg>: RpcServicewhere
Msg: RequestContents,{
type Reply: TryIntoBody;
// Required method
fn on_message<'life0, 'async_trait>(
&'life0 self,
msg: Request<Msg>,
) -> Pin<Box<dyn Future<Output = Result<Self::Reply, Status>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided method
fn path() -> &'static str { ... }
}
Expand description
A generic RPC message handler.
use rkyv::{Archive, Deserialize, Serialize};
use datacake_rpc::{Handler, Request, RpcService, ServiceRegistry, Status, RpcClient, Channel};
use std::net::SocketAddr;
#[repr(C)]
#[derive(Serialize, Deserialize, Archive, Debug)]
#[archive(check_bytes)]
#[archive_attr(derive(Debug))]
pub struct MyMessage {
name: String,
age: u32,
}
pub struct EchoService;
impl RpcService for EchoService {
fn register_handlers(registry: &mut ServiceRegistry<Self>) {
registry.add_handler::<MyMessage>();
}
}
// Our message must implement `Archive` and have it's archived value
// implement check bytes, this is used to provide the zero-copy functionality.
#[datacake_rpc::async_trait]
impl Handler<MyMessage> for EchoService {
// Our reply can be any type that implements `Archive` and `Serialize` as part
// of the rkyv package. Here we're just echoing the message back.
type Reply = MyMessage;
// We get passed a `Request` which is a thin wrapper around the `DataView` type.
// This means we are simply being given a zero-copy view of the message rather
// than a owned value. If you need a owned version which is not tied ot the
// request buffer, you can use the `to_owned` method which will attempt to
// deserialize the inner message/view.
async fn on_message(&self, msg: Request<MyMessage>) -> Result<Self::Reply, Status> {
Ok(msg.to_owned().unwrap())
}
}
Required Associated Types§
Required Methods§
Sourcefn on_message<'life0, 'async_trait>(
&'life0 self,
msg: Request<Msg>,
) -> Pin<Box<dyn Future<Output = Result<Self::Reply, Status>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn on_message<'life0, 'async_trait>(
&'life0 self,
msg: Request<Msg>,
) -> Pin<Box<dyn Future<Output = Result<Self::Reply, Status>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Process a message. We get passed a Request which is a thin wrapper around the inner content of the specified type as defined by RequestContents::Content
This means we are simply being given a zero-copy view of the message rather
than a owned value. If you need a owned version which is not tied ot the
request buffer, you can use the to_owned
method which will attempt to
deserialize the inner message/view.
Provided Methods§
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.