axum_server/
service.rs

1//! Service traits.
2
3use http::Response;
4use http_body::Body;
5use std::{
6    future::Future,
7    task::{Context, Poll},
8};
9use tower_service::Service;
10
11/// Trait alias for [`Service`] with bounds required for [`serve`](crate::server::Server::serve).
12///
13/// This trait is sealed and cannot be implemented for types outside this crate.
14#[allow(missing_docs)]
15pub trait SendService<Request>: send_service::Sealed<Request> {
16    type Service: Service<
17            Request,
18            Response = Response<Self::Body>,
19            Error = Self::Error,
20            Future = Self::Future,
21        > + Send
22        + Clone
23        + 'static;
24
25    type Body: Body<Data = Self::BodyData, Error = Self::BodyError> + Send + 'static;
26    type BodyData: bytes::Buf + Send + 'static;
27    type BodyError: Into<Box<dyn std::error::Error + Send + Sync>>;
28
29    type Error: Into<Box<dyn std::error::Error + Send + Sync>>;
30
31    type Future: Future<Output = Result<Response<Self::Body>, Self::Error>> + Send + 'static;
32
33    fn into_service(self) -> Self::Service;
34}
35
36impl<T, B, Request> send_service::Sealed<Request> for T
37where
38    T: Service<Request, Response = Response<B>>,
39    T::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
40    T::Future: Send + 'static,
41    B: Body + Send + 'static,
42    B::Data: Send + 'static,
43    B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
44{
45}
46
47impl<T, B, Request> SendService<Request> for T
48where
49    T: Service<Request, Response = Response<B>> + Send + Clone + 'static,
50    T::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
51    T::Future: Send + 'static,
52    B: Body + Send + 'static,
53    B::Data: Send + 'static,
54    B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
55{
56    type Service = T;
57
58    type Body = B;
59    type BodyData = B::Data;
60    type BodyError = B::Error;
61
62    type Error = T::Error;
63
64    type Future = T::Future;
65
66    fn into_service(self) -> Self::Service {
67        self
68    }
69}
70
71/// Modified version of [`MakeService`] that takes a `&Target` and has required trait bounds for
72/// [`serve`](crate::server::Server::serve).
73///
74/// This trait is sealed and cannot be implemented for types outside this crate.
75///
76/// [`MakeService`]: https://docs.rs/tower/0.4/tower/make/trait.MakeService.html
77#[allow(missing_docs)]
78pub trait MakeService<Target, Request>: make_service_ref::Sealed<(Target, Request)> {
79    type Service: Service<
80            Request,
81            Response = Response<Self::Body>,
82            Error = Self::Error,
83            Future = Self::Future,
84        >
85        + Send
86        + 'static
87        + Clone;
88
89    type Body: Body<Data = Self::BodyData, Error = Self::BodyError> + Send + 'static;
90    type BodyData: Send + 'static;
91    type BodyError: Into<Box<dyn std::error::Error + Send + Sync>>;
92
93    type Error: Into<Box<dyn std::error::Error + Send + Sync>>;
94
95    type Future: Future<Output = Result<Response<Self::Body>, Self::Error>> + Send + 'static;
96
97    type MakeError: Into<Box<dyn std::error::Error + Send + Sync>>;
98    type MakeFuture: Future<Output = Result<Self::Service, Self::MakeError>>;
99
100    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::MakeError>>;
101
102    fn make_service(&mut self, target: Target) -> Self::MakeFuture;
103}
104
105impl<T, S, B, E, F, Target, Request> make_service_ref::Sealed<(Target, Request)> for T
106where
107    T: Service<Target, Response = S, Error = E, Future = F>,
108    S: Service<Request, Response = Response<B>> + Send + 'static,
109    S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
110    S::Future: Send + 'static,
111    B: Body + Send + 'static,
112    B::Data: Send + 'static,
113    B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
114    E: Into<Box<dyn std::error::Error + Send + Sync>>,
115    F: Future<Output = Result<S, E>>,
116{
117}
118
119impl<T, S, B, E, F, Target, Request> MakeService<Target, Request> for T
120where
121    T: Service<Target, Response = S, Error = E, Future = F>,
122    S: Service<Request, Response = Response<B>> + Send + Clone + 'static,
123    S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
124    S::Future: Send + 'static,
125    B: Body + Send + 'static,
126    B::Data: Send + 'static,
127    B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
128    E: Into<Box<dyn std::error::Error + Send + Sync>>,
129    F: Future<Output = Result<S, E>>,
130{
131    type Service = S;
132
133    type Body = B;
134    type BodyData = B::Data;
135    type BodyError = B::Error;
136
137    type Error = S::Error;
138
139    type Future = S::Future;
140
141    type MakeError = E;
142    type MakeFuture = F;
143
144    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::MakeError>> {
145        self.poll_ready(cx)
146    }
147
148    fn make_service(&mut self, target: Target) -> Self::MakeFuture {
149        self.call(target)
150    }
151}
152
153mod send_service {
154    pub trait Sealed<T> {}
155}
156
157mod make_service_ref {
158    pub trait Sealed<T> {}
159}