From 05c6cee0ec4f052b2cdc918d6d8993b8f1d96dd0 Mon Sep 17 00:00:00 2001 From: Shane Date: Fri, 15 Mar 2024 11:03:55 -0700 Subject: [PATCH 1/3] Add trait variant --- tarpc/Cargo.toml | 1 + tarpc/src/client/stub.rs | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/tarpc/Cargo.toml b/tarpc/Cargo.toml index fed99f1d..be1c9697 100644 --- a/tarpc/Cargo.toml +++ b/tarpc/Cargo.toml @@ -59,6 +59,7 @@ tracing = { version = "0.1", default-features = false, features = [ "log", ] } tracing-opentelemetry = { version = "0.22.0", default-features = false } +trait-variant = "0.1.1" opentelemetry = { version = "0.21.0", default-features = false } diff --git a/tarpc/src/client/stub.rs b/tarpc/src/client/stub.rs index e7c11aa0..8861e668 100644 --- a/tarpc/src/client/stub.rs +++ b/tarpc/src/client/stub.rs @@ -16,6 +16,7 @@ mod mock; /// A connection to a remote service. /// Calls the service with requests of type `Req` and receives responses of type `Resp`. #[allow(async_fn_in_trait)] +#[trait_variant::make(TokioStub: Send)] pub trait Stub { /// The service request type. type Req: RequestName; @@ -40,13 +41,13 @@ where } } -impl 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.clone().serve(ctx, req).await.map_err(RpcError::Server) - } -} +// impl 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.clone().serve(ctx, req).await.map_err(RpcError::Server) +// } +// } From 7f6f35186ed7cf63cdfaabad42656ed53c311c32 Mon Sep 17 00:00:00 2001 From: Shane Date: Wed, 27 Mar 2024 12:18:12 -0700 Subject: [PATCH 2/3] Replace blanket impl with Serve::to_stub --- tarpc/src/client/stub.rs | 15 +-------------- tarpc/src/server.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/tarpc/src/client/stub.rs b/tarpc/src/client/stub.rs index 8861e668..8240cead 100644 --- a/tarpc/src/client/stub.rs +++ b/tarpc/src/client/stub.rs @@ -2,9 +2,7 @@ use crate::{ client::{Channel, RpcError}, - context, - server::Serve, - RequestName, + context, RequestName, }; pub mod load_balance; @@ -40,14 +38,3 @@ where Self::call(self, ctx, request).await } } - -// impl 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.clone().serve(ctx, req).await.map_err(RpcError::Server) -// } -// } diff --git a/tarpc/src/server.rs b/tarpc/src/server.rs index d79d45c2..6c0cee92 100644 --- a/tarpc/src/server.rs +++ b/tarpc/src/server.rs @@ -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}, @@ -66,6 +68,27 @@ impl Config { } } +/// A [`Stub`] implementation that simply warps a `Serve`. +pub struct ServeStub { + serve: S, +} + +impl Stub for ServeStub +where + S: Serve + Clone, +{ + type Req = ::Req; + type Resp = ::Resp; + + async fn call(&self, ctx: context::Context, req: Self::Req) -> Result { + self.serve + .clone() + .serve(ctx, req) + .await + .map_err(RpcError::Server) + } +} + /// Equivalent to a `FnOnce(Req) -> impl Future`. #[allow(async_fn_in_trait)] pub trait Serve { @@ -77,6 +100,16 @@ pub trait Serve { /// Responds to a single request. async fn serve(self, ctx: context::Context, req: Self::Req) -> Result; + + /// Wrap this `Serve` in a type that implements [`Stub`]. + async fn into_stub(self) -> ServeStub + where + Self: Clone, + { + ServeStub { + serve: self.clone(), + } + } } /// A Serve wrapper around a Fn. From a272ab886608813d773e9c463f593119430bb1f4 Mon Sep 17 00:00:00 2001 From: Shane Date: Sat, 26 Oct 2024 21:44:28 -0700 Subject: [PATCH 3/3] Rename to SendStub --- tarpc/src/client/stub.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tarpc/src/client/stub.rs b/tarpc/src/client/stub.rs index 8240cead..6c7bfe5c 100644 --- a/tarpc/src/client/stub.rs +++ b/tarpc/src/client/stub.rs @@ -14,7 +14,7 @@ mod mock; /// A connection to a remote service. /// Calls the service with requests of type `Req` and receives responses of type `Resp`. #[allow(async_fn_in_trait)] -#[trait_variant::make(TokioStub: Send)] +#[trait_variant::make(SendStub: Send)] pub trait Stub { /// The service request type. type Req: RequestName;