Struct datacake_rpc::RpcClient
source · pub struct RpcClient<Svc>where
Svc: RpcService,{ /* private fields */ }Expand description
A RPC client handle for a given service.
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, PartialEq, Debug)]
#[archive(compare(PartialEq))]
#[archive_attr(derive(CheckBytes, PartialEq, 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>();
}
}
#[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())
}
}
let connect = "127.0.0.1:8000".parse::<SocketAddr>()?;
let client = Channel::connect(connect);
let rpc_client = RpcClient::<EchoService>::new(client);
let msg = MyMessage {
name: "Bobby".to_string(),
age: 12,
};
let resp = rpc_client.send(&msg).await?;
assert_eq!(resp, msg);Implementations§
source§impl<Svc> RpcClient<Svc>where
Svc: RpcService,
impl<Svc> RpcClient<Svc>where
Svc: RpcService,
sourcepub fn new(channel: Channel) -> Self
pub fn new(channel: Channel) -> Self
Creates a new RPC client which can handle a new service type.
RpcClient’s are cheap to create and should be preferred over locking or other synchronization primitives.
sourcepub fn new_client<Svc2>(&self) -> RpcClient<Svc2>where
Svc2: RpcService,
pub fn new_client<Svc2>(&self) -> RpcClient<Svc2>where
Svc2: RpcService,
Creates a new RPC client which can handle a new service type.
RpcClient’s are cheap to create and should be preferred over locking or other synchronization primitives.
sourcepub async fn send<Msg>(
&self,
msg: &Msg
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: Archive + Serialize<AllocSerializer<SCRATCH_SPACE>>,
Msg::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: Archive + Serialize<AllocSerializer<SCRATCH_SPACE>>,
<<Svc as Handler<Msg>>::Reply as Archive>::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
pub async fn send<Msg>(
&self,
msg: &Msg
) -> Result<MessageReply<Svc, Msg>, Status>where
Msg: Archive + Serialize<AllocSerializer<SCRATCH_SPACE>>,
Msg::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
Svc: Handler<Msg>,
<Svc as Handler<Msg>>::Reply: Archive + Serialize<AllocSerializer<SCRATCH_SPACE>>,
<<Svc as Handler<Msg>>::Reply as Archive>::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
Sends a message to the server and wait for a reply.
Trait Implementations§
Auto Trait Implementations§
impl<Svc> !RefUnwindSafe for RpcClient<Svc>
impl<Svc> Send for RpcClient<Svc>where
Svc: Send,
impl<Svc> Sync for RpcClient<Svc>where
Svc: Sync,
impl<Svc> Unpin for RpcClient<Svc>where
Svc: Unpin,
impl<Svc> !UnwindSafe for RpcClient<Svc>
Blanket Implementations§
source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata
) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
source§impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere
W: DeserializeWith<F, T, D>,
D: Fallible + ?Sized,
F: ?Sized,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
source§fn layout_raw(_: *const T) -> Layout
fn layout_raw(_: *const T) -> Layout
Gets the layout of the type.