1use super::Handler;
2use crate::body::{Body, Bytes, HttpBody};
3#[cfg(feature = "tokio")]
4use crate::extract::connect_info::IntoMakeServiceWithConnectInfo;
5use crate::response::Response;
6use crate::routing::IntoMakeService;
7use crate::BoxError;
8use http::Request;
9use std::{
10 convert::Infallible,
11 fmt,
12 marker::PhantomData,
13 task::{Context, Poll},
14};
15use tower_service::Service;
16
17pub struct HandlerService<H, T, S> {
23 handler: H,
24 state: S,
25 _marker: PhantomData<fn() -> T>,
26}
27
28impl<H, T, S> HandlerService<H, T, S> {
29 pub fn state(&self) -> &S {
31 &self.state
32 }
33
34 pub fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, S>> {
64 IntoMakeService::new(self)
65 }
66
67 #[cfg(feature = "tokio")]
104 pub fn into_make_service_with_connect_info<C>(
105 self,
106 ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, S>, C> {
107 IntoMakeServiceWithConnectInfo::new(self)
108 }
109}
110
111#[test]
112fn traits() {
113 use crate::test_helpers::*;
114 assert_send::<HandlerService<(), NotSendSync, ()>>();
115 assert_sync::<HandlerService<(), NotSendSync, ()>>();
116}
117
118impl<H, T, S> HandlerService<H, T, S> {
119 pub(super) fn new(handler: H, state: S) -> Self {
120 Self {
121 handler,
122 state,
123 _marker: PhantomData,
124 }
125 }
126}
127
128impl<H, T, S> fmt::Debug for HandlerService<H, T, S> {
129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130 f.debug_struct("IntoService").finish_non_exhaustive()
131 }
132}
133
134impl<H, T, S> Clone for HandlerService<H, T, S>
135where
136 H: Clone,
137 S: Clone,
138{
139 fn clone(&self) -> Self {
140 Self {
141 handler: self.handler.clone(),
142 state: self.state.clone(),
143 _marker: PhantomData,
144 }
145 }
146}
147
148impl<H, T, S, B> Service<Request<B>> for HandlerService<H, T, S>
149where
150 H: Handler<T, S> + Clone + Send + 'static,
151 B: HttpBody<Data = Bytes> + Send + 'static,
152 B::Error: Into<BoxError>,
153 S: Clone + Send + Sync,
154{
155 type Response = Response;
156 type Error = Infallible;
157 type Future = super::future::IntoServiceFuture<H::Future>;
158
159 #[inline]
160 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
161 Poll::Ready(Ok(()))
165 }
166
167 fn call(&mut self, req: Request<B>) -> Self::Future {
168 use futures_util::future::FutureExt;
169
170 let req = req.map(Body::new);
171
172 let handler = self.handler.clone();
173 let future = Handler::call(handler, req, self.state.clone());
174 let future = future.map(Ok as _);
175
176 super::future::IntoServiceFuture::new(future)
177 }
178}
179
180#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
182const _: () = {
183 use crate::serve::IncomingStream;
184
185 impl<H, T, S> Service<IncomingStream<'_>> for HandlerService<H, T, S>
186 where
187 H: Clone,
188 S: Clone,
189 {
190 type Response = Self;
191 type Error = Infallible;
192 type Future = std::future::Ready<Result<Self::Response, Self::Error>>;
193
194 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
195 Poll::Ready(Ok(()))
196 }
197
198 fn call(&mut self, _req: IncomingStream<'_>) -> Self::Future {
199 std::future::ready(Ok(self.clone()))
200 }
201 }
202};