-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change extends e62cc28 by boxing the controller clients so their inner types signatures don't leak into the stacks that use them. This change adds a new BoxServiceClone type to replace Tower's BoxCloneService. Our new type implements Sync so the Service can be used across threads.
- Loading branch information
Showing
11 changed files
with
116 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,94 @@ | ||
use std::marker::PhantomData; | ||
use futures::Future; | ||
use std::pin::Pin; | ||
pub use tower::util::BoxService; | ||
|
||
#[derive(Copy, Debug)] | ||
pub struct BoxServiceLayer<R> { | ||
_p: PhantomData<fn(R)>, | ||
pub struct BoxServiceClone<Req, Rsp, E> { | ||
inner: Box< | ||
dyn sealed::CloneService< | ||
Req, | ||
Response = Rsp, | ||
Error = E, | ||
Future = Pin<Box<dyn Future<Output = Result<Rsp, E>> + Send + 'static>>, | ||
>, | ||
>, | ||
} | ||
|
||
impl<S, R> tower::Layer<S> for BoxServiceLayer<R> | ||
where | ||
S: tower::Service<R> + Send + 'static, | ||
S::Future: Send + 'static, | ||
S::Error: Send + 'static, | ||
{ | ||
type Service = BoxService<R, S::Response, S::Error>; | ||
fn layer(&self, s: S) -> Self::Service { | ||
BoxService::new(s) | ||
impl<Req: 'static, Rsp: 'static, E: 'static> BoxServiceClone<Req, Rsp, E> { | ||
pub fn new<S>(inner: S) -> Self | ||
where | ||
S: crate::Service<Req, Response = Rsp, Error = E>, | ||
S: Clone + Send + Sync + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
Self { | ||
inner: Box::new(crate::BoxFuture::new(inner)), | ||
} | ||
} | ||
} | ||
|
||
impl<R> BoxServiceLayer<R> { | ||
pub fn new() -> Self { | ||
Self { _p: PhantomData } | ||
pub fn layer<S>() -> impl crate::layer::Layer<S, Service = Self> + Clone + Copy | ||
where | ||
S: crate::Service<Req, Response = Rsp, Error = E>, | ||
S: Clone + Send + Sync + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
crate::layer::mk(Self::new) | ||
} | ||
} | ||
|
||
impl<R> Clone for BoxServiceLayer<R> { | ||
impl<Req: 'static, Rsp: 'static, E: 'static> Clone for BoxServiceClone<Req, Rsp, E> { | ||
fn clone(&self) -> Self { | ||
Self::new() | ||
BoxServiceClone { | ||
inner: self.inner.clone_boxed(), | ||
} | ||
} | ||
} | ||
|
||
impl<Req, Rsp, E> crate::Service<Req> for BoxServiceClone<Req, Rsp, E> | ||
where | ||
Req: 'static, | ||
Rsp: 'static, | ||
E: 'static, | ||
{ | ||
type Response = Rsp; | ||
type Error = E; | ||
type Future = Pin<Box<dyn Future<Output = Result<Rsp, E>> + Send + 'static>>; | ||
|
||
#[inline] | ||
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Result<(), E>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
#[inline] | ||
fn call(&mut self, req: Req) -> Self::Future { | ||
self.inner.call(req) | ||
} | ||
} | ||
|
||
impl<R> Default for BoxServiceLayer<R> { | ||
fn default() -> Self { | ||
Self::new() | ||
mod sealed { | ||
use crate::Service; | ||
|
||
pub trait CloneService<Req>: Service<Req> + Send + Sync + 'static { | ||
fn clone_boxed( | ||
&self, | ||
) -> Box< | ||
dyn CloneService< | ||
Req, | ||
Response = Self::Response, | ||
Error = Self::Error, | ||
Future = Self::Future, | ||
>, | ||
>; | ||
} | ||
|
||
impl<Req, S> CloneService<Req> for S | ||
where | ||
S: Service<Req> + Send + Sync + Clone + 'static, | ||
{ | ||
fn clone_boxed( | ||
&self, | ||
) -> Box<dyn CloneService<Req, Response = S::Response, Error = S::Error, Future = S::Future>> | ||
{ | ||
Box::new(self.clone()) | ||
} | ||
} | ||
} |
Oops, something went wrong.