Skip to content

Commit

Permalink
util: Refactor BoxService (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored Aug 26, 2021
1 parent ee131aa commit 3a134ba
Showing 1 changed file with 2 additions and 24 deletions.
26 changes: 2 additions & 24 deletions tower/src/util/boxed/sync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ServiceExt;
use tower_layer::{layer_fn, LayerFn};
use tower_service::Service;

Expand Down Expand Up @@ -25,19 +26,14 @@ pub struct BoxService<T, U, E> {
/// across threads.
type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;

#[derive(Debug)]
struct Boxed<S> {
inner: S,
}

impl<T, U, E> BoxService<T, U, E> {
#[allow(missing_docs)]
pub fn new<S>(inner: S) -> Self
where
S: Service<T, Response = U, Error = E> + Send + 'static,
S::Future: Send + 'static,
{
let inner = Box::new(Boxed { inner });
let inner = Box::new(inner.map_future(|f: S::Future| Box::pin(f) as _));
BoxService { inner }
}

Expand Down Expand Up @@ -78,21 +74,3 @@ where
fmt.debug_struct("BoxService").finish()
}
}

impl<S, Request> Service<Request> for Boxed<S>
where
S: Service<Request> + 'static,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future = Pin<Box<dyn Future<Output = Result<S::Response, S::Error>> + Send>>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, request: Request) -> Self::Future {
Box::pin(self.inner.call(request))
}
}

0 comments on commit 3a134ba

Please sign in to comment.