-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow futures on Serve and Stub to be Send #448
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: typo
Suggested change
|
||||||
pub struct ServeStub<S> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe keep this type private for now and return an imp Stub? Can always make it public later if needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea |
||||||
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 { | ||||||
|
@@ -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> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remind me why the blanket impl for Serve impls had to be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the compiler was complaining about conflicting trait implementations. |
||||||
where | ||||||
Self: Clone, | ||||||
{ | ||||||
ServeStub { | ||||||
serve: self.clone(), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/// A Serve wrapper around a Fn. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: maybe just call it
SendStub
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!