diff --git a/examples/src/tls_rustls/server.rs b/examples/src/tls_rustls/server.rs index 0fb31f8fa..23f894966 100644 --- a/examples/src/tls_rustls/server.rs +++ b/examples/src/tls_rustls/server.rs @@ -9,52 +9,35 @@ use pb::{EchoRequest, EchoResponse}; use std::sync::Arc; use tokio::net::TcpListener; use tokio_rustls::{ - rustls::{ - pki_types::{CertificateDer, PrivatePkcs8KeyDer}, - ServerConfig, - }, + rustls::{pki_types::CertificateDer, ServerConfig}, TlsAcceptor, }; -use tonic::{body::BoxBody, transport::Server, Request, Response, Status}; +use tonic::{body::BoxBody, service::Routes, Request, Response, Status}; use tower::{BoxError, ServiceExt}; use tower_http::ServiceBuilderExt; #[tokio::main] async fn main() -> Result<(), Box> { let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]); - let certs: Vec> = { + let certs = { let fd = std::fs::File::open(data_dir.join("tls/server.pem"))?; let mut buf = std::io::BufReader::new(&fd); - rustls_pemfile::certs(&mut buf) - .into_iter() - .map(|res| res.map(|cert| cert.to_owned())) - .collect::, _>>()? + rustls_pemfile::certs(&mut buf).collect::, _>>()? }; - let key: PrivatePkcs8KeyDer<'static> = { + let key = { let fd = std::fs::File::open(data_dir.join("tls/server.key"))?; let mut buf = std::io::BufReader::new(&fd); - let key = rustls_pemfile::pkcs8_private_keys(&mut buf) - .into_iter() - .next() - .unwrap()? - .clone_key(); - - key - - // let key = std::fs::read(data_dir.join("tls/server.key"))?; - // PrivateKey(key) + rustls_pemfile::private_key(&mut buf)?.unwrap() }; let mut tls = ServerConfig::builder() .with_no_client_auth() - .with_single_cert(certs, key.into())?; + .with_single_cert(certs, key)?; tls.alpn_protocols = vec![b"h2".to_vec()]; let server = EchoServer::default(); - let svc = Server::builder() - .add_service(pb::echo_server::EchoServer::new(server)) - .into_service(); + let svc = Routes::new(pb::echo_server::EchoServer::new(server)); let http = Builder::new(TokioExecutor::new()); diff --git a/tonic/src/codec/decode.rs b/tonic/src/codec/decode.rs index 95bf90d05..742660e31 100644 --- a/tonic/src/codec/decode.rs +++ b/tonic/src/codec/decode.rs @@ -291,13 +291,11 @@ impl StreamingInner { fn response(&mut self) -> Result<(), Status> { if let Direction::Response(status) = self.direction { - if let Err(e) = crate::status::infer_grpc_status(self.trailers.as_ref(), status) { - if let Some(e) = e { - // If the trailers contain a grpc-status, then we should return that as the error - // and otherwise stop the stream (by taking the error state) - self.trailers.take(); - return Err(e); - } + if let Err(Some(e)) = crate::status::infer_grpc_status(self.trailers.as_ref(), status) { + // If the trailers contain a grpc-status, then we should return that as the error + // and otherwise stop the stream (by taking the error state) + self.trailers.take(); + return Err(e); } } Ok(()) diff --git a/tonic/src/service/router.rs b/tonic/src/service/router.rs index 690d28fc2..e7f864f8d 100644 --- a/tonic/src/service/router.rs +++ b/tonic/src/service/router.rs @@ -168,10 +168,7 @@ where } fn call(&mut self, req: Request) -> Self::Future { - let fut = self.service.call(req.map(|body| boxed(body))); - Box::pin(async move { - fut.await - .map(|res| res.map(|body| axum::body::Body::new(body))) - }) + let fut = self.service.call(req.map(boxed)); + Box::pin(async move { fut.await.map(|res| res.map(axum::body::Body::new)) }) } } diff --git a/tonic/src/transport/channel/service/connection.rs b/tonic/src/transport/channel/service/connection.rs index 3d8ce0d1e..e2a40fbea 100644 --- a/tonic/src/transport/channel/service/connection.rs +++ b/tonic/src/transport/channel/service/connection.rs @@ -147,11 +147,7 @@ impl tower::Service> for SendRequest { fn call(&mut self, req: Request) -> Self::Future { let fut = self.inner.send_request(req); - Box::pin(async move { - fut.await - .map_err(Into::into) - .map(|res| res.map(|body| boxed(body))) - }) + Box::pin(async move { fut.await.map_err(Into::into).map(|res| res.map(boxed)) }) } } diff --git a/tonic/src/transport/channel/service/connector.rs b/tonic/src/transport/channel/service/connector.rs index 47628daf6..2e8291c04 100644 --- a/tonic/src/transport/channel/service/connector.rs +++ b/tonic/src/transport/channel/service/connector.rs @@ -118,7 +118,7 @@ where Ok::<_, crate::Error>(BoxedIo::new(io)) } .await - .map_err(|err| ConnectError(From::from(err))) + .map_err(ConnectError) }) } }