hyper_util_wasm/
service.rs

1//! Service utilities.
2
3use pin_project_lite::pin_project;
4use std::{
5    future::Future,
6    pin::Pin,
7    task::{Context, Poll},
8};
9use tower::{util::Oneshot, ServiceExt};
10
11/// A tower service converted into a hyper service.
12#[derive(Debug, Copy, Clone)]
13pub struct TowerToHyperService<S> {
14    service: S,
15}
16
17impl<S> TowerToHyperService<S> {
18    /// Create a new `TowerToHyperService` from a tower service.
19    pub fn new(tower_service: S) -> Self {
20        Self {
21            service: tower_service,
22        }
23    }
24}
25
26impl<S, R> hyper::service::Service<R> for TowerToHyperService<S>
27where
28    S: tower_service::Service<R> + Clone,
29{
30    type Response = S::Response;
31    type Error = S::Error;
32    type Future = TowerToHyperServiceFuture<S, R>;
33
34    fn call(&self, req: R) -> Self::Future {
35        TowerToHyperServiceFuture {
36            future: self.service.clone().oneshot(req),
37        }
38    }
39}
40
41pin_project! {
42    /// Response future for [`TowerToHyperService`].
43    pub struct TowerToHyperServiceFuture<S, R>
44    where
45        S: tower_service::Service<R>,
46    {
47        #[pin]
48        future: Oneshot<S, R>,
49    }
50}
51
52impl<S, R> Future for TowerToHyperServiceFuture<S, R>
53where
54    S: tower_service::Service<R>,
55{
56    type Output = Result<S::Response, S::Error>;
57
58    #[inline]
59    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
60        self.project().future.poll(cx)
61    }
62}