1 #[cfg(feature = "tokio")] 2 use crate::extract::connect_info::IntoMakeServiceWithConnectInfo; 3 use crate::routing::IntoMakeService; 4 use tower_service::Service; 5 6 /// Extension trait that adds additional methods to any [`Service`]. 7 pub trait ServiceExt<R>: Service<R> + Sized { 8 /// Convert this service into a [`MakeService`], that is a [`Service`] whose 9 /// response is another service. 10 /// 11 /// This is commonly used when applying middleware around an entire [`Router`]. See ["Rewriting 12 /// request URI in middleware"] for more details. 13 /// 14 /// [`MakeService`]: tower::make::MakeService 15 /// ["Rewriting request URI in middleware"]: crate::middleware#rewriting-request-uri-in-middleware 16 /// [`Router`]: crate::Router into_make_service(self) -> IntoMakeService<Self>17 fn into_make_service(self) -> IntoMakeService<Self>; 18 19 /// Convert this service into a [`MakeService`], that will store `C`'s 20 /// associated `ConnectInfo` in a request extension such that [`ConnectInfo`] 21 /// can extract it. 22 /// 23 /// This enables extracting things like the client's remote address. 24 /// This is commonly used when applying middleware around an entire [`Router`]. See ["Rewriting 25 /// request URI in middleware"] for more details. 26 /// 27 /// [`MakeService`]: tower::make::MakeService 28 /// ["Rewriting request URI in middleware"]: crate::middleware#rewriting-request-uri-in-middleware 29 /// [`Router`]: crate::Router 30 /// [`ConnectInfo`]: crate::extract::connect_info::ConnectInfo 31 #[cfg(feature = "tokio")] into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C>32 fn into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C>; 33 } 34 35 impl<S, R> ServiceExt<R> for S 36 where 37 S: Service<R> + Sized, 38 { into_make_service(self) -> IntoMakeService<Self>39 fn into_make_service(self) -> IntoMakeService<Self> { 40 IntoMakeService::new(self) 41 } 42 43 #[cfg(feature = "tokio")] into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C>44 fn into_make_service_with_connect_info<C>(self) -> IntoMakeServiceWithConnectInfo<Self, C> { 45 IntoMakeServiceWithConnectInfo::new(self) 46 } 47 } 48