Skip to content

Commit 7f6f351

Browse files
committed
Replace blanket impl with Serve::to_stub
1 parent 05c6cee commit 7f6f351

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

tarpc/src/client/stub.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
33
use crate::{
44
client::{Channel, RpcError},
5-
context,
6-
server::Serve,
7-
RequestName,
5+
context, RequestName,
86
};
97

108
pub mod load_balance;
@@ -40,14 +38,3 @@ where
4038
Self::call(self, ctx, request).await
4139
}
4240
}
43-
44-
// impl<S> Stub for S
45-
// where
46-
// S: Serve + Clone,
47-
// {
48-
// type Req = S::Req;
49-
// type Resp = S::Resp;
50-
// async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
51-
// self.clone().serve(ctx, req).await.map_err(RpcError::Server)
52-
// }
53-
// }

tarpc/src/server.rs

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
//! Provides a server that concurrently handles many connections sending multiplexed requests.
88
9+
use crate::client::stub::Stub;
10+
use crate::client::RpcError;
911
use crate::{
1012
cancellations::{cancellations, CanceledRequests, RequestCancellation},
1113
context::{self, SpanExt},
@@ -66,6 +68,27 @@ impl Config {
6668
}
6769
}
6870

71+
/// A [`Stub`] implementation that simply warps a `Serve`.
72+
pub struct ServeStub<S> {
73+
serve: S,
74+
}
75+
76+
impl<S> Stub for ServeStub<S>
77+
where
78+
S: Serve + Clone,
79+
{
80+
type Req = <S as Serve>::Req;
81+
type Resp = <S as Serve>::Resp;
82+
83+
async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
84+
self.serve
85+
.clone()
86+
.serve(ctx, req)
87+
.await
88+
.map_err(RpcError::Server)
89+
}
90+
}
91+
6992
/// Equivalent to a `FnOnce(Req) -> impl Future<Output = Resp>`.
7093
#[allow(async_fn_in_trait)]
7194
pub trait Serve {
@@ -77,6 +100,16 @@ pub trait Serve {
77100

78101
/// Responds to a single request.
79102
async fn serve(self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, ServerError>;
103+
104+
/// Wrap this `Serve` in a type that implements [`Stub`].
105+
async fn into_stub(self) -> ServeStub<Self>
106+
where
107+
Self: Clone,
108+
{
109+
ServeStub {
110+
serve: self.clone(),
111+
}
112+
}
80113
}
81114

82115
/// A Serve wrapper around a Fn.

0 commit comments

Comments
 (0)