From bc95f797ec5c7b0f8150fb672669813b9547af80 Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 28 Jul 2023 22:05:42 +0900 Subject: [PATCH] chore(examples): Remove futures-util --- examples/Cargo.toml | 4 +-- examples/src/hyper_warp/server.rs | 31 +++++++++++---------- examples/src/hyper_warp_multiplex/server.rs | 31 +++++++++++---------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 2c9e3975e..f32939ed1 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -261,7 +261,7 @@ autoreload = ["tokio-stream/net", "dep:listenfd"] health = ["dep:tonic-health"] grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:tracing-subscriber"] tracing = ["dep:tracing", "dep:tracing-subscriber"] -hyper-warp = ["dep:futures-util", "dep:tower", "dep:hyper", "dep:http", "dep:http-body", "dep:warp"] +hyper-warp = ["dep:either", "dep:tower", "dep:hyper", "dep:http", "dep:http-body", "dep:warp"] hyper-warp-multiplex = ["hyper-warp"] uds = ["tokio-stream/net", "dep:tower", "dep:hyper"] streaming = ["tokio-stream", "dep:h2"] @@ -289,8 +289,8 @@ tonic-web = { path = "../tonic-web", optional = true } tonic-health = { path = "../tonic-health", optional = true } tonic-reflection = { path = "../tonic-reflection", optional = true } tonic-types = { path = "../tonic-types", optional = true } +either = { version = "1.9", optional = true } async-stream = { version = "0.3", optional = true } -futures-util = { version = "0.3", optional = true } tokio-stream = { version = "0.1", optional = true } tower = { version = "0.4", optional = true } rand = { version = "0.8", optional = true } diff --git a/examples/src/hyper_warp/server.rs b/examples/src/hyper_warp/server.rs index c46298688..a79caf401 100644 --- a/examples/src/hyper_warp/server.rs +++ b/examples/src/hyper_warp/server.rs @@ -3,7 +3,7 @@ //! To hit the warp server you can run this command: //! `curl localhost:50051/hello` -use futures_util::future::{self, Either, TryFutureExt}; +use either::Either; use http::version::Version; use hyper::{service::make_service_fn, Server}; use std::convert::Infallible; @@ -53,22 +53,25 @@ async fn main() -> Result<(), Box> { Server::bind(&addr) .serve(make_service_fn(move |_| { let mut tonic = tonic.clone(); - future::ok::<_, Infallible>(tower::service_fn( + std::future::ready(Ok::<_, Infallible>(tower::service_fn( move |req: hyper::Request| match req.version() { - Version::HTTP_11 | Version::HTTP_10 => Either::Left( - warp.call(req) - .map_ok(|res| res.map(EitherBody::Left)) - .map_err(Error::from), - ), - Version::HTTP_2 => Either::Right( - tonic - .call(req) - .map_ok(|res| res.map(EitherBody::Right)) - .map_err(Error::from), - ), + Version::HTTP_11 | Version::HTTP_10 => Either::Left({ + let res = warp.call(req); + Box::pin(async move { + let res = res.await.map(|res| res.map(EitherBody::Left))?; + Ok::<_, Error>(res) + }) + }), + Version::HTTP_2 => Either::Right({ + let res = tonic.call(req); + Box::pin(async move { + let res = res.await.map(|res| res.map(EitherBody::Right))?; + Ok::<_, Error>(res) + }) + }), _ => unimplemented!(), }, - )) + ))) })) .await?; diff --git a/examples/src/hyper_warp_multiplex/server.rs b/examples/src/hyper_warp_multiplex/server.rs index 6bfc33dfc..deea8bea5 100644 --- a/examples/src/hyper_warp_multiplex/server.rs +++ b/examples/src/hyper_warp_multiplex/server.rs @@ -3,7 +3,7 @@ //! To hit the warp server you can run this command: //! `curl localhost:50051/hello` -use futures_util::future::{self, Either, TryFutureExt}; +use either::Either; use http::version::Version; use hyper::{service::make_service_fn, Server}; use std::convert::Infallible; @@ -80,22 +80,25 @@ async fn main() -> Result<(), Box> { .add_service(echo) .into_service(); - future::ok::<_, Infallible>(tower::service_fn( + std::future::ready(Ok::<_, Infallible>(tower::service_fn( move |req: hyper::Request| match req.version() { - Version::HTTP_11 | Version::HTTP_10 => Either::Left( - warp.call(req) - .map_ok(|res| res.map(EitherBody::Left)) - .map_err(Error::from), - ), - Version::HTTP_2 => Either::Right( - tonic - .call(req) - .map_ok(|res| res.map(EitherBody::Right)) - .map_err(Error::from), - ), + Version::HTTP_11 | Version::HTTP_10 => Either::Left({ + let res = warp.call(req); + Box::pin(async move { + let res = res.await.map(|res| res.map(EitherBody::Left))?; + Ok::<_, Error>(res) + }) + }), + Version::HTTP_2 => Either::Right({ + let res = tonic.call(req); + Box::pin(async move { + let res = res.await.map(|res| res.map(EitherBody::Right))?; + Ok::<_, Error>(res) + }) + }), _ => unimplemented!(), }, - )) + ))) })) .await?;