hyper_util/service/
glue.rs1use pin_project_lite::pin_project;
2use std::{
3 future::Future,
4 pin::Pin,
5 task::{Context, Poll},
6};
7
8use super::Oneshot;
9
10#[derive(Debug, Copy, Clone)]
20pub struct TowerToHyperService<S> {
21 service: S,
22}
23
24impl<S> TowerToHyperService<S> {
25 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 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}