| 1 | use std::{ |
| 2 | fmt, |
| 3 | future::Future, |
| 4 | task::{Context, Poll}, |
| 5 | }; |
| 6 | use tower_layer::Layer; |
| 7 | use tower_service::Service; |
| 8 | |
| 9 | /// [`Service`] returned by the [`map_future`] combinator. |
| 10 | /// |
| 11 | /// [`map_future`]: crate::util::ServiceExt::map_future |
| 12 | #[derive (Clone)] |
| 13 | pub struct MapFuture<S, F> { |
| 14 | inner: S, |
| 15 | f: F, |
| 16 | } |
| 17 | |
| 18 | impl<S, F> MapFuture<S, F> { |
| 19 | /// Creates a new [`MapFuture`] service. |
| 20 | pub const fn new(inner: S, f: F) -> Self { |
| 21 | Self { inner, f } |
| 22 | } |
| 23 | |
| 24 | /// Returns a new [`Layer`] that produces [`MapFuture`] services. |
| 25 | /// |
| 26 | /// This is a convenience function that simply calls [`MapFutureLayer::new`]. |
| 27 | /// |
| 28 | /// [`Layer`]: tower_layer::Layer |
| 29 | pub fn layer(f: F) -> MapFutureLayer<F> { |
| 30 | MapFutureLayer::new(f) |
| 31 | } |
| 32 | |
| 33 | /// Get a reference to the inner service |
| 34 | pub fn get_ref(&self) -> &S { |
| 35 | &self.inner |
| 36 | } |
| 37 | |
| 38 | /// Get a mutable reference to the inner service |
| 39 | pub fn get_mut(&mut self) -> &mut S { |
| 40 | &mut self.inner |
| 41 | } |
| 42 | |
| 43 | /// Consume `self`, returning the inner service |
| 44 | pub fn into_inner(self) -> S { |
| 45 | self.inner |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl<R, S, F, T, E, Fut> Service<R> for MapFuture<S, F> |
| 50 | where |
| 51 | S: Service<R>, |
| 52 | F: FnMut(S::Future) -> Fut, |
| 53 | E: From<S::Error>, |
| 54 | Fut: Future<Output = Result<T, E>>, |
| 55 | { |
| 56 | type Response = T; |
| 57 | type Error = E; |
| 58 | type Future = Fut; |
| 59 | |
| 60 | fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 61 | self.inner.poll_ready(cx).map_err(From::from) |
| 62 | } |
| 63 | |
| 64 | fn call(&mut self, req: R) -> Self::Future { |
| 65 | (self.f)(self.inner.call(req)) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl<S, F> fmt::Debug for MapFuture<S, F> |
| 70 | where |
| 71 | S: fmt::Debug, |
| 72 | { |
| 73 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 74 | f&mut DebugStruct<'_, '_>.debug_struct("MapFuture" ) |
| 75 | .field("inner" , &self.inner) |
| 76 | .field(name:"f" , &format_args!(" {}" , std::any::type_name::<F>())) |
| 77 | .finish() |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// A [`Layer`] that produces a [`MapFuture`] service. |
| 82 | /// |
| 83 | /// [`Layer`]: tower_layer::Layer |
| 84 | #[derive (Clone)] |
| 85 | pub struct MapFutureLayer<F> { |
| 86 | f: F, |
| 87 | } |
| 88 | |
| 89 | impl<F> MapFutureLayer<F> { |
| 90 | /// Creates a new [`MapFutureLayer`] layer. |
| 91 | pub const fn new(f: F) -> Self { |
| 92 | Self { f } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl<S, F> Layer<S> for MapFutureLayer<F> |
| 97 | where |
| 98 | F: Clone, |
| 99 | { |
| 100 | type Service = MapFuture<S, F>; |
| 101 | |
| 102 | fn layer(&self, inner: S) -> Self::Service { |
| 103 | MapFuture::new(inner, self.f.clone()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl<F> fmt::Debug for MapFutureLayer<F> { |
| 108 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 109 | f&mut DebugStruct<'_, '_>.debug_struct("MapFutureLayer" ) |
| 110 | .field(name:"f" , &format_args!(" {}" , std::any::type_name::<F>())) |
| 111 | .finish() |
| 112 | } |
| 113 | } |
| 114 | |