Skip to content

Commit

Permalink
Replace blanket impl with Serve::to_stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneMurphy2 committed Mar 27, 2024
1 parent 05c6cee commit 7f6f351
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
15 changes: 1 addition & 14 deletions tarpc/src/client/stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use crate::{
client::{Channel, RpcError},
context,
server::Serve,
RequestName,
context, RequestName,
};

pub mod load_balance;
Expand Down Expand Up @@ -40,14 +38,3 @@ where
Self::call(self, ctx, request).await
}
}

// impl<S> Stub for S
// where
// S: Serve + Clone,
// {
// type Req = S::Req;
// type Resp = S::Resp;
// async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
// self.clone().serve(ctx, req).await.map_err(RpcError::Server)
// }
// }
33 changes: 33 additions & 0 deletions tarpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

//! Provides a server that concurrently handles many connections sending multiplexed requests.

use crate::client::stub::Stub;
use crate::client::RpcError;
use crate::{
cancellations::{cancellations, CanceledRequests, RequestCancellation},
context::{self, SpanExt},
Expand Down Expand Up @@ -66,6 +68,27 @@ impl Config {
}
}

/// A [`Stub`] implementation that simply warps a `Serve`.
pub struct ServeStub<S> {
serve: S,
}

impl<S> Stub for ServeStub<S>
where
S: Serve + Clone,
{
type Req = <S as Serve>::Req;
type Resp = <S as Serve>::Resp;

async fn call(&self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, RpcError> {
self.serve
.clone()
.serve(ctx, req)
.await
.map_err(RpcError::Server)
}
}

/// Equivalent to a `FnOnce(Req) -> impl Future<Output = Resp>`.
#[allow(async_fn_in_trait)]
pub trait Serve {
Expand All @@ -77,6 +100,16 @@ pub trait Serve {

/// Responds to a single request.
async fn serve(self, ctx: context::Context, req: Self::Req) -> Result<Self::Resp, ServerError>;

/// Wrap this `Serve` in a type that implements [`Stub`].
async fn into_stub(self) -> ServeStub<Self>
where
Self: Clone,
{
ServeStub {
serve: self.clone(),
}
}
}

/// A Serve wrapper around a Fn.
Expand Down

0 comments on commit 7f6f351

Please sign in to comment.