Skip to content

Commit

Permalink
feat: Make boxed function public
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Jun 24, 2024
1 parent f679dcf commit 21c781a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions examples/src/h2c/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::server::conn::auto::Builder;
use hyper_util::service::TowerToHyperService;
use tokio::net::TcpListener;
use tonic::{transport::Server, Request, Response, Status};
use tonic::{service::Routes, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
Expand Down Expand Up @@ -39,9 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("GreeterServer listening on {}", addr);

let incoming = TcpListener::bind(addr).await?;
let svc = Server::builder()
.add_service(GreeterServer::new(greeter))
.into_router();
let svc = Routes::new(GreeterServer::new(greeter));

let h2c = h2c::H2c { s: svc };

Expand Down Expand Up @@ -71,8 +69,8 @@ mod h2c {
use http::{Request, Response};
use hyper::body::Incoming;
use hyper_util::{rt::TokioExecutor, service::TowerToHyperService};
use tonic::{body::empty_body, service::AxumBody};
use tower::Service;
use tonic::body::{empty_body, BoxBody};
use tower::{Service, ServiceExt};

#[derive(Clone)]
pub struct H2c<S> {
Expand All @@ -83,12 +81,12 @@ mod h2c {

impl<S> Service<Request<Incoming>> for H2c<S>
where
S: Service<Request<Incoming>, Response = Response<AxumBody>> + Clone + Send + 'static,
S: Service<Request<BoxBody>, Response = Response<BoxBody>> + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Sync + Send + 'static,
S::Response: Send + 'static,
{
type Response = hyper::Response<tonic::body::BoxBody>;
type Response = hyper::Response<BoxBody>;
type Error = hyper::Error;
type Future =
Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
Expand All @@ -100,8 +98,12 @@ mod h2c {
std::task::Poll::Ready(Ok(()))
}

fn call(&mut self, mut req: hyper::Request<Incoming>) -> Self::Future {
let svc = self.s.clone();
fn call(&mut self, req: hyper::Request<Incoming>) -> Self::Future {
let mut req = req.map(tonic::body::boxed);
let svc = self
.s
.clone()
.map_request(|req: Request<Incoming>| req.map(tonic::body::boxed));
Box::pin(async move {
tokio::spawn(async move {
let upgraded_io = hyper::upgrade::on(&mut req).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tonic/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use http_body_util::BodyExt;
pub type BoxBody = http_body_util::combinators::UnsyncBoxBody<bytes::Bytes, crate::Status>;

/// Convert a [`http_body::Body`] into a [`BoxBody`].
pub(crate) fn boxed<B>(body: B) -> BoxBody
pub fn boxed<B>(body: B) -> BoxBody
where
B: http_body::Body<Data = bytes::Bytes> + Send + 'static,
B::Error: Into<crate::Error>,
Expand Down

0 comments on commit 21c781a

Please sign in to comment.