Skip to content

Commit

Permalink
feat: add TowerToHyperService
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn authored and seanmonstar committed Nov 28, 2023
1 parent 72c1c67 commit b6bffe2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ mod common;
pub mod rt;
#[cfg(feature = "server")]
pub mod server;
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "server", feature = "client")
))]
pub mod service;

mod error;
70 changes: 70 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Service utilities.

use pin_project_lite::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::{util::Oneshot, ServiceExt};

/// A tower service converted into a hyper service.
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "server", feature = "client")
))]
#[derive(Debug, Copy, Clone)]
pub struct TowerToHyperService<S> {
service: S,
}

impl<S> TowerToHyperService<S> {
/// Create a new `TowerToHyperService` from a tower service.
pub fn new(tower_service: S) -> Self {
Self {
service: tower_service,
}
}
}

impl<S, R> hyper::service::Service<R> for TowerToHyperService<S>
where
S: tower_service::Service<R> + Clone,
{
type Response = S::Response;
type Error = S::Error;
type Future = TowerToHyperServiceFuture<S, R>;

fn call(&self, req: R) -> Self::Future {
TowerToHyperServiceFuture {
future: self.service.clone().oneshot(req),
}
}
}

pin_project! {
/// Response future for [`TowerToHyperService`].
#[cfg(all(
any(feature = "http1", feature = "http2"),
any(feature = "server", feature = "client")
))]
pub struct TowerToHyperServiceFuture<S, R>
where
S: tower_service::Service<R>,
{
#[pin]
future: Oneshot<S, R>,
}
}

impl<S, R> Future for TowerToHyperServiceFuture<S, R>
where
S: tower_service::Service<R>,
{
type Output = Result<S::Response, S::Error>;

#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.project().future.poll(cx)
}
}

0 comments on commit b6bffe2

Please sign in to comment.