pub struct ServiceRegistry<Svc> { /* private fields */ }
Expand description

A registry system used for linking a service’s message handlers with the RPC system at runtime.

Since the RPC system cannot determine what message payload matches with which handler at compile time, it must dynamically link them at runtime.

Not registering a handler will cause the handler to not be triggered even if a valid message comes through.

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,
}

#[repr(C)]
#[derive(Serialize, Deserialize, Archive, Debug)]
#[archive_attr(derive(CheckBytes, Debug))]
pub struct MyOtherMessage {
    age: u32,
}

pub struct EchoService;

impl RpcService for EchoService {
    fn register_handlers(registry: &mut ServiceRegistry<Self>) {
        // Since we've registered the `MyMessage` handler, the RPC system
        // will dispatch the messages to out handler.
        //
        // But because we *haven't* registered our `MyOtherMessage` handler,
        // even though our service implements the handler, no messages will
        // be dispatched.
        registry.add_handler::<MyMessage>();

    }
}

#[datacake_rpc::async_trait]
impl Handler<MyMessage> for EchoService {
    type Reply = MyMessage;

    async fn on_message(&self, msg: Request<MyMessage>) -> Result<Self::Reply, Status> {
        Ok(msg.to_owned().unwrap())
    }
}

#[datacake_rpc::async_trait]
impl Handler<MyOtherMessage> for EchoService {
    type Reply = MyOtherMessage;

    async fn on_message(&self, msg: Request<MyOtherMessage>) -> Result<Self::Reply, Status> {
        Ok(msg.to_owned().unwrap())
    }
}

Implementations§

Adds a new handler to the registry.

This is done in the form of specifying what message types are handled by the service via the generic.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
The archived version of the pointer metadata for this type.
Converts some archived metadata to the pointer metadata for itself.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Deserializes using the given deserializer

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Gets the layout of the type.
The type for metadata in pointers and references to Self.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more