1use tower_service::Service;
2
3use std::fmt;
4use std::{
5 future::Future,
6 pin::Pin,
7 task::{Context, Poll},
8};
9
10pub struct BoxService<T, U, E> {
18 inner: Box<dyn Service<T, Response = U, Error = E, Future = BoxFuture<U, E>> + Send>,
19}
20
21type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
26
27#[derive(Debug)]
28struct Boxed<S> {
29 inner: S,
30}
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(Boxed { inner });
40 BoxService { inner }
41 }
42}
43
44impl<T, U, E> Service<T> for BoxService<T, U, E> {
45 type Response = U;
46 type Error = E;
47 type Future = BoxFuture<U, E>;
48
49 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
50 self.inner.poll_ready(cx)
51 }
52
53 fn call(&mut self, request: T) -> BoxFuture<U, E> {
54 self.inner.call(request)
55 }
56}
57
58impl<T, U, E> fmt::Debug for BoxService<T, U, E>
59where
60 T: fmt::Debug,
61 U: fmt::Debug,
62 E: fmt::Debug,
63{
64 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
65 fmt.debug_struct("BoxService").finish()
66 }
67}
68
69impl<S, Request> Service<Request> for Boxed<S>
70where
71 S: Service<Request> + 'static,
72 S::Future: Send + 'static,
73{
74 type Response = S::Response;
75 type Error = S::Error;
76 type Future = Pin<Box<dyn Future<Output = Result<S::Response, S::Error>> + Send>>;
77
78 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
79 self.inner.poll_ready(cx)
80 }
81
82 fn call(&mut self, request: Request) -> Self::Future {
83 Box::pin(self.inner.call(request))
84 }
85}