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 23, 2024
1 parent 6a213e9 commit 58ca572
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 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, transport::AxumBody};
use tower::Service;
use tonic::body::empty_body;
use tower::{Service, ServiceExt};

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

impl<S> Service<Request<Incoming>> for H2c<S>
where
S: Service<Request<Incoming>, Response = Response<AxumBody>> + Clone + Send + 'static,
S: Service<Request<tonic::body::BoxBody>, Response = Response<tonic::body::BoxBody>>
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Sync + Send + 'static,
S::Response: Send + 'static,
Expand All @@ -100,8 +101,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 58ca572

Please sign in to comment.