tarpc_copy/client/
stub.rs

1//! Provides a Stub trait, implemented by types that can call remote services.
2
3use crate::{
4    client::{Channel, RpcError},
5    context,
6    server::Serve,
7    RequestName,
8};
9
10pub mod load_balance;
11pub mod retry;
12
13#[cfg(test)]
14mod mock;
15
16/// A connection to a remote service.
17/// Calls the service with requests of type `Req` and receives responses of type `Resp`.
18#[allow(async_fn_in_trait)]
19pub trait Stub {
20    /// The service request type.
21    type Req: RequestName;
22
23    /// The service response type.
24    type Resp;
25
26    /// Calls a remote service.
27    async fn call(&self, ctx: context::Context, request: Self::Req)
28        -> Result<Self::Resp, RpcError>;
29}
30
31impl<Req, Resp> Stub for Channel<Req, Resp>
32where
33    Req: RequestName,
34{
35    type Req = Req;
36    type Resp = Resp;
37
38    async fn call(&self, ctx: context::Context, request: Req) -> Result<Self::Resp, RpcError> {
39        Self::call(self, ctx, request).await
40    }
41}
42
43impl<S> Stub for S
44where
45    S: Serve + Clone,
46{
47    type Req = S::Req;
48    type Resp = S::Resp;
49    async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
50        self.clone().serve(ctx, req).await.map_err(RpcError::Server)
51    }
52}