Skip to content

Commit

Permalink
Use upstream hyper_util::service::TowerToHyperService
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Aug 24, 2024
1 parent d34c43a commit 4ca7742
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 61 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ rust-version = "1.66"
[features]
default = []
tls-rustls = ["tls-rustls-no-provider", "rustls/aws-lc-rs"]
tls-rustls-no-provider = ["arc-swap", "rustls", "rustls-pemfile", "tokio/fs", "tokio/time", "tokio-rustls", "rustls-pki-types"]
tls-openssl = ["arc-swap", "openssl", "tokio-openssl"]
tls-rustls-no-provider = ["arc-swap", "rustls", "rustls-pemfile", "tokio/fs", "tokio/time", "tokio-rustls", "rustls-pki-types", "dep:pin-project-lite"]
tls-openssl = ["arc-swap", "openssl", "tokio-openssl", "dep:pin-project-lite"]

[dependencies]
bytes = "1"
Expand All @@ -29,8 +29,7 @@ hyper = { version = "1.4", features = ["http1", "http2", "server"] }
tokio = { version = "1", features = ["macros", "net", "sync"] }
tower-service = "0.3"
http-body-util = "0.1"
hyper-util = { version = "0.1.2", features = ["server-auto", "tokio"] }
pin-project-lite = "0.2"
hyper-util = { version = "0.1.2", features = ["server-auto", "service", "tokio"] }
tower = { version = "0.5", features = ["util"] }

# optional dependencies
Expand All @@ -45,6 +44,9 @@ tokio-rustls = { version = "0.26", default-features = false, optional = true }
openssl = { version = "0.10", optional = true }
tokio-openssl = { version = "0.6", optional = true }

## rustls or openssl
pin-project-lite = { version = "0.2", optional = true }

[dev-dependencies]
serial_test = "3.1"
axum = "0.7"
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
accept::{Accept, DefaultAcceptor},
handle::Handle,
service::TowerToHyperService,
service::{MakeService, SendService},
};
use futures_util::future::poll_fn;
Expand All @@ -10,6 +9,7 @@ use hyper::body::Incoming;
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto::Builder,
service::TowerToHyperService,
};
use std::{
fmt,
Expand Down
56 changes: 0 additions & 56 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

use http::Response;
use http_body::Body;
use pin_project_lite::pin_project;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
use tower::{util::Oneshot, ServiceExt};
use tower_service::Service;

/// Trait alias for [`Service`] with bounds required for [`serve`](crate::server::Server::serve).
Expand Down Expand Up @@ -160,56 +157,3 @@ mod send_service {
mod make_service_ref {
pub trait Sealed<T> {}
}

/// A tower service converted into a hyper service.
#[derive(Debug, Copy, Clone)]
pub(crate) struct TowerToHyperService<S> {
service: S,
}

impl<S> TowerToHyperService<S> {
/// Create a new `TowerToHyperService` from a tower service.
pub(crate) 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`].
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 4ca7742

Please sign in to comment.