Skip to content

Commit 2cc868f

Browse files
authoredJul 6, 2024··
feat: Make boxed function public (#1754)
1 parent cdfbf2a commit 2cc868f

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed
 

‎examples/src/h2c/server.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo};
44
use hyper_util::server::conn::auto::Builder;
55
use hyper_util::service::TowerToHyperService;
66
use tokio::net::TcpListener;
7-
use tonic::{transport::Server, Request, Response, Status};
7+
use tonic::{service::Routes, Request, Response, Status};
88

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

4141
let incoming = TcpListener::bind(addr).await?;
42-
let svc = Server::builder()
43-
.add_service(GreeterServer::new(greeter))
44-
.into_router();
42+
let svc = Routes::new(GreeterServer::new(greeter));
4543

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

@@ -71,8 +69,8 @@ mod h2c {
7169
use http::{Request, Response};
7270
use hyper::body::Incoming;
7371
use hyper_util::{rt::TokioExecutor, service::TowerToHyperService};
74-
use tonic::{body::empty_body, service::AxumBody};
75-
use tower::Service;
72+
use tonic::body::{empty_body, BoxBody};
73+
use tower::{Service, ServiceExt};
7674

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

8482
impl<S> Service<Request<Incoming>> for H2c<S>
8583
where
86-
S: Service<Request<Incoming>, Response = Response<AxumBody>> + Clone + Send + 'static,
84+
S: Service<Request<BoxBody>, Response = Response<BoxBody>> + Clone + Send + 'static,
8785
S::Future: Send + 'static,
8886
S::Error: Into<BoxError> + Sync + Send + 'static,
8987
S::Response: Send + 'static,
9088
{
91-
type Response = hyper::Response<tonic::body::BoxBody>;
89+
type Response = hyper::Response<BoxBody>;
9290
type Error = hyper::Error;
9391
type Future =
9492
Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
@@ -100,8 +98,12 @@ mod h2c {
10098
std::task::Poll::Ready(Ok(()))
10199
}
102100

103-
fn call(&mut self, mut req: hyper::Request<Incoming>) -> Self::Future {
104-
let svc = self.s.clone();
101+
fn call(&mut self, req: hyper::Request<Incoming>) -> Self::Future {
102+
let mut req = req.map(tonic::body::boxed);
103+
let svc = self
104+
.s
105+
.clone()
106+
.map_request(|req: Request<_>| req.map(tonic::body::boxed));
105107
Box::pin(async move {
106108
tokio::spawn(async move {
107109
let upgraded_io = hyper::upgrade::on(&mut req).await.unwrap();

‎tonic/src/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use http_body_util::BodyExt;
66
pub type BoxBody = http_body_util::combinators::UnsyncBoxBody<bytes::Bytes, crate::Status>;
77

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

0 commit comments

Comments
 (0)
Please sign in to comment.