Trait datacake_rpc::Handler

source ·
pub trait Handler<Msg>: RpcServicewhere
    Msg: Archive,
    Msg::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
{ type Reply: Archive + Serialize<AllocSerializer<SCRATCH_SPACE>>; 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
; fn path() -> &'static str { ... } }
Expand description

A generic RPC message handler.

use bytecheck::CheckBytes;
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_attr(derive(CheckBytes, 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§

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.

Required Methods§

Process a message. 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.

Provided Methods§

The path of the message, this is similar to the service name which can be used to avoid conflicts, by default this uses the name of the message type.

Implementors§