/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-util-0.1.18/src/service/glue.rs
Line | Count | Source |
1 | | use pin_project_lite::pin_project; |
2 | | use std::{ |
3 | | future::Future, |
4 | | pin::Pin, |
5 | | task::{Context, Poll}, |
6 | | }; |
7 | | |
8 | | use super::Oneshot; |
9 | | |
10 | | /// A tower [`Service`][tower-svc] converted into a hyper [`Service`][hyper-svc]. |
11 | | /// |
12 | | /// This wraps an inner tower service `S` in a [`hyper::service::Service`] implementation. See |
13 | | /// the module-level documentation of [`service`][crate::service] for more information about using |
14 | | /// [`tower`][tower] services and middleware with [`hyper`]. |
15 | | /// |
16 | | /// [hyper-svc]: hyper::service::Service |
17 | | /// [tower]: https://docs.rs/tower/latest/tower/ |
18 | | /// [tower-svc]: https://docs.rs/tower/latest/tower/trait.Service.html |
19 | | #[derive(Debug, Copy, Clone)] |
20 | | pub struct TowerToHyperService<S> { |
21 | | service: S, |
22 | | } |
23 | | |
24 | | impl<S> TowerToHyperService<S> { |
25 | | /// Create a new [`TowerToHyperService`] from a tower service. |
26 | 0 | pub fn new(tower_service: S) -> Self { |
27 | 0 | Self { |
28 | 0 | service: tower_service, |
29 | 0 | } |
30 | 0 | } |
31 | | } |
32 | | |
33 | | impl<S, R> hyper::service::Service<R> for TowerToHyperService<S> |
34 | | where |
35 | | S: tower_service::Service<R> + Clone, |
36 | | { |
37 | | type Response = S::Response; |
38 | | type Error = S::Error; |
39 | | type Future = TowerToHyperServiceFuture<S, R>; |
40 | | |
41 | 0 | fn call(&self, req: R) -> Self::Future { |
42 | 0 | TowerToHyperServiceFuture { |
43 | 0 | future: Oneshot::new(self.service.clone(), req), |
44 | 0 | } |
45 | 0 | } |
46 | | } |
47 | | |
48 | | pin_project! { |
49 | | /// Response future for [`TowerToHyperService`]. |
50 | | /// |
51 | | /// This future is acquired by [`call`][hyper::service::Service::call]ing a |
52 | | /// [`TowerToHyperService`]. |
53 | | pub struct TowerToHyperServiceFuture<S, R> |
54 | | where |
55 | | S: tower_service::Service<R>, |
56 | | { |
57 | | #[pin] |
58 | | future: Oneshot<S, R>, |
59 | | } |
60 | | } |
61 | | |
62 | | impl<S, R> Future for TowerToHyperServiceFuture<S, R> |
63 | | where |
64 | | S: tower_service::Service<R>, |
65 | | { |
66 | | type Output = Result<S::Response, S::Error>; |
67 | | |
68 | | #[inline] |
69 | 0 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
70 | 0 | self.project().future.poll(cx) |
71 | 0 | } |
72 | | } |