/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-1.6.0/src/service/util.rs
Line | Count | Source |
1 | | use std::error::Error as StdError; |
2 | | use std::fmt; |
3 | | use std::future::Future; |
4 | | use std::marker::PhantomData; |
5 | | |
6 | | use crate::body::Body; |
7 | | use crate::service::service::Service; |
8 | | use crate::{Request, Response}; |
9 | | |
10 | | /// Create a `Service` from a function. |
11 | | /// |
12 | | /// # Example |
13 | | /// |
14 | | /// ``` |
15 | | /// use bytes::Bytes; |
16 | | /// use hyper::{body, Request, Response, Version}; |
17 | | /// use http_body_util::Full; |
18 | | /// use hyper::service::service_fn; |
19 | | /// |
20 | | /// let service = service_fn(|req: Request<body::Incoming>| async move { |
21 | | /// if req.version() == Version::HTTP_11 { |
22 | | /// Ok(Response::new(Full::<Bytes>::from("Hello World"))) |
23 | | /// } else { |
24 | | /// // Note: it's usually better to return a Response |
25 | | /// // with an appropriate StatusCode instead of an Err. |
26 | | /// Err("not HTTP/1.1, abort connection") |
27 | | /// } |
28 | | /// }); |
29 | | /// ``` |
30 | 0 | pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R> |
31 | 0 | where |
32 | 0 | F: Fn(Request<R>) -> S, |
33 | 0 | S: Future, |
34 | | { |
35 | 0 | ServiceFn { |
36 | 0 | f, |
37 | 0 | _req: PhantomData, |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | | /// Service returned by [`service_fn`] |
42 | | pub struct ServiceFn<F, R> { |
43 | | f: F, |
44 | | _req: PhantomData<fn(R)>, |
45 | | } |
46 | | |
47 | | impl<F, ReqBody, Ret, ResBody, E> Service<Request<ReqBody>> for ServiceFn<F, ReqBody> |
48 | | where |
49 | | F: Fn(Request<ReqBody>) -> Ret, |
50 | | ReqBody: Body, |
51 | | Ret: Future<Output = Result<Response<ResBody>, E>>, |
52 | | E: Into<Box<dyn StdError + Send + Sync>>, |
53 | | ResBody: Body, |
54 | | { |
55 | | type Response = crate::Response<ResBody>; |
56 | | type Error = E; |
57 | | type Future = Ret; |
58 | | |
59 | 0 | fn call(&self, req: Request<ReqBody>) -> Self::Future { |
60 | 0 | (self.f)(req) |
61 | 0 | } |
62 | | } |
63 | | |
64 | | impl<F, R> fmt::Debug for ServiceFn<F, R> { |
65 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
66 | 0 | f.debug_struct("impl Service").finish() |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl<F, R> Clone for ServiceFn<F, R> |
71 | | where |
72 | | F: Clone, |
73 | | { |
74 | 0 | fn clone(&self) -> Self { |
75 | 0 | ServiceFn { |
76 | 0 | f: self.f.clone(), |
77 | 0 | _req: PhantomData, |
78 | 0 | } |
79 | 0 | } |
80 | | } |
81 | | |
82 | | impl<F, R> Copy for ServiceFn<F, R> where F: Copy {} |