1use crate::ServiceExt;
2use tower_layer::{layer_fn, LayerFn};
3use tower_service::Service;
4
5use std::fmt;
6use std::{
7 future::Future,
8 pin::Pin,
9 task::{Context, Poll},
10};
11
12pub struct BoxService<T, U, E> {
23 inner: Box<dyn Service<T, Response = U, Error = E, Future = BoxFuture<U, E>> + Send>,
24}
25
26type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
31
32impl<T, U, E> BoxService<T, U, E> {
33 #[allow(missing_docs)]
34 pub fn new<S>(inner: S) -> Self
35 where
36 S: Service<T, Response = U, Error = E> + Send + 'static,
37 S::Future: Send + 'static,
38 {
39 let inner = Box::new(inner.map_future(|f: S::Future| Box::pin(f) as _));
40 BoxService { inner }
41 }
42
43 pub fn layer<S>() -> LayerFn<fn(S) -> Self>
48 where
49 S: Service<T, Response = U, Error = E> + Send + 'static,
50 S::Future: Send + 'static,
51 {
52 layer_fn(Self::new)
53 }
54}
55
56impl<T, U, E> Service<T> for BoxService<T, U, E> {
57 type Response = U;
58 type Error = E;
59 type Future = BoxFuture<U, E>;
60
61 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
62 self.inner.poll_ready(cx)
63 }
64
65 fn call(&mut self, request: T) -> BoxFuture<U, E> {
66 self.inner.call(request)
67 }
68}
69
70impl<T, U, E> fmt::Debug for BoxService<T, U, E> {
71 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
72 fmt.debug_struct("BoxService").finish()
73 }
74}