use super::Handler; use crate::body::BoxBody; use http::{Request, Response}; use std::{ convert::Infallible, fmt, marker::PhantomData, task::{Context, Poll}, }; use tower_service::Service; /// An adapter that makes a [`Handler`] into a [`Service`]. /// /// Created with [`Handler::into_service`]. pub struct IntoService { handler: H, _marker: PhantomData (B, T)>, } #[test] fn traits() { use crate::tests::*; assert_send::>(); assert_sync::>(); } impl IntoService { pub(super) fn new(handler: H) -> Self { Self { handler, _marker: PhantomData, } } } impl fmt::Debug for IntoService { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("IntoService") .field(&format_args!("...")) .finish() } } impl Clone for IntoService where H: Clone, { fn clone(&self) -> Self { Self { handler: self.handler.clone(), _marker: PhantomData, } } } impl Service> for IntoService where H: Handler + Clone + Send + 'static, B: Send + 'static, { type Response = Response; type Error = Infallible; type Future = super::future::IntoServiceFuture; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { // `IntoService` can only be constructed from async functions which are always ready, or from // `Layered` which bufferes in `::call` and is therefore also always // ready. Poll::Ready(Ok(())) } fn call(&mut self, req: Request) -> Self::Future { use futures_util::future::FutureExt; let handler = self.handler.clone(); let future = Handler::call(handler, req).map(Ok::<_, Infallible> as _); super::future::IntoServiceFuture { future } } }