//! Service traits. use http::Response; use http_body::Body; use std::{ future::Future, task::{Context, Poll}, }; use tower_service::Service; /// Trait alias for [`Service`] with bounds required for [`serve`](crate::server::Server::serve). /// /// This trait is sealed and cannot be implemented for types outside this crate. #[allow(missing_docs)] pub trait SendService: send_service::Sealed { type Service: Service< Request, Response = Response, Error = Self::Error, Future = Self::Future, > + Send + Clone + 'static; type Body: Body + Send + 'static; type BodyData: bytes::Buf + Send + 'static; type BodyError: Into>; type Error: Into>; type Future: Future, Self::Error>> + Send + 'static; fn into_service(self) -> Self::Service; } impl send_service::Sealed for T where T: Service>, T::Error: Into>, T::Future: Send + 'static, B: Body + Send + 'static, B::Data: Send + 'static, B::Error: Into>, { } impl SendService for T where T: Service> + Send + Clone + 'static, T::Error: Into>, T::Future: Send + 'static, B: Body + Send + 'static, B::Data: Send + 'static, B::Error: Into>, { type Service = T; type Body = B; type BodyData = B::Data; type BodyError = B::Error; type Error = T::Error; type Future = T::Future; fn into_service(self) -> Self::Service { self } } /// Modified version of [`MakeService`] that takes a `&Target` and has required trait bounds for /// [`serve`](crate::server::Server::serve). /// /// This trait is sealed and cannot be implemented for types outside this crate. /// /// [`MakeService`]: https://docs.rs/tower/0.4/tower/make/trait.MakeService.html #[allow(missing_docs)] pub trait MakeService: make_service_ref::Sealed<(Target, Request)> { type Service: Service< Request, Response = Response, Error = Self::Error, Future = Self::Future, > + Send + 'static + Clone; type Body: Body + Send + 'static; type BodyData: Send + 'static; type BodyError: Into>; type Error: Into>; type Future: Future, Self::Error>> + Send + 'static; type MakeError: Into>; type MakeFuture: Future>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll>; fn make_service(&mut self, target: Target) -> Self::MakeFuture; } impl make_service_ref::Sealed<(Target, Request)> for T where T: Service, S: Service> + Send + 'static, S::Error: Into>, S::Future: Send + 'static, B: Body + Send + 'static, B::Data: Send + 'static, B::Error: Into>, E: Into>, F: Future>, { } impl MakeService for T where T: Service, S: Service> + Send + Clone + 'static, S::Error: Into>, S::Future: Send + 'static, B: Body + Send + 'static, B::Data: Send + 'static, B::Error: Into>, E: Into>, F: Future>, { type Service = S; type Body = B; type BodyData = B::Data; type BodyError = B::Error; type Error = S::Error; type Future = S::Future; type MakeError = E; type MakeFuture = F; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.poll_ready(cx) } fn make_service(&mut self, target: Target) -> Self::MakeFuture { self.call(target) } } mod send_service { pub trait Sealed {} } mod make_service_ref { pub trait Sealed {} }