hyper_util/service/
glue.rs

1use pin_project_lite::pin_project;
2use std::{
3    future::Future,
4    pin::Pin,
5    task::{Context, Poll},
6};
7
8use 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)]
20pub struct TowerToHyperService<S> {
21    service: S,
22}
23
24impl<S> TowerToHyperService<S> {
25    /// Create a new [`TowerToHyperService`] from a tower service.
26    pub fn new(tower_service: S) -> Self {
27        Self {
28            service: tower_service,
29        }
30    }
31}
32
33impl<S, R> hyper::service::Service<R> for TowerToHyperService<S>
34where
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    fn call(&self, req: R) -> Self::Future {
42        TowerToHyperServiceFuture {
43            future: Oneshot::new(self.service.clone(), req),
44        }
45    }
46}
47
48pin_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
62impl<S, R> Future for TowerToHyperServiceFuture<S, R>
63where
64    S: tower_service::Service<R>,
65{
66    type Output = Result<S::Response, S::Error>;
67
68    #[inline]
69    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
70        self.project().future.poll(cx)
71    }
72}