From 117018f3199a458c0f40270d5c20260c33841ea4 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 29 Mar 2019 19:17:02 -0700 Subject: [PATCH] Rename `EitherService` -> `Either` (#218) --- tower-util/Cargo.toml | 1 - tower-util/src/either.rs | 68 +++++++++++++++++++++++++++++++++ tower-util/src/either/future.rs | 42 -------------------- tower-util/src/either/mod.rs | 63 ------------------------------ tower-util/src/lib.rs | 6 +-- tower/Cargo.toml | 2 +- tower/src/util.rs | 2 +- 7 files changed, 71 insertions(+), 113 deletions(-) create mode 100644 tower-util/src/either.rs delete mode 100644 tower-util/src/either/future.rs delete mode 100644 tower-util/src/either/mod.rs diff --git a/tower-util/Cargo.toml b/tower-util/Cargo.toml index c020eab..c6ac944 100644 --- a/tower-util/Cargo.toml +++ b/tower-util/Cargo.toml @@ -22,7 +22,6 @@ categories = ["asynchronous", "network-programming"] io = ["tokio-io"] [dependencies] -either = { version = "1.5.1", optional = true } futures = "0.1.23" tokio-io = { version = "0.1.12", optional = true } tower-service = "0.2.0" diff --git a/tower-util/src/either.rs b/tower-util/src/either.rs new file mode 100644 index 0000000..0a035fa --- /dev/null +++ b/tower-util/src/either.rs @@ -0,0 +1,68 @@ +//! Contains `Either` and related types and functions. +//! +//! See `Either` documentation for more details. + +use futures::{Future, Poll}; +use tower_service::Service; + +/// Combine two different service types into a single type. +/// +/// Both services must be of the same request, response, and error types. +/// `Either` is useful for handling conditional branching in service middleware +/// to different inner service types. +pub enum Either { + A(A), + B(B), +} + +type Error = Box<::std::error::Error + Send + Sync>; + +impl Service for Either +where + A: Service, + A::Error: Into, + B: Service, + B::Error: Into, +{ + type Response = A::Response; + type Error = Error; + type Future = Either; + + fn poll_ready(&mut self) -> Poll<(), Self::Error> { + use self::Either::*; + + match self { + A(service) => service.poll_ready().map_err(Into::into), + B(service) => service.poll_ready().map_err(Into::into), + } + } + + fn call(&mut self, request: Request) -> Self::Future { + use self::Either::*; + + match self { + A(service) => A(service.call(request)), + B(service) => B(service.call(request)), + } + } +} + +impl Future for Either +where + A: Future, + A::Error: Into, + B: Future, + B::Error: Into, +{ + type Item = A::Item; + type Error = Error; + + fn poll(&mut self) -> Poll { + use self::Either::*; + + match self { + A(fut) => fut.poll().map_err(Into::into), + B(fut) => fut.poll().map_err(Into::into), + } + } +} diff --git a/tower-util/src/either/future.rs b/tower-util/src/either/future.rs deleted file mode 100644 index 1b5457c..0000000 --- a/tower-util/src/either/future.rs +++ /dev/null @@ -1,42 +0,0 @@ -use super::Error; -use _either::Either; -use futures::{Future, Poll}; - -#[derive(Debug)] -pub struct ResponseFuture { - inner: Either, -} - -impl ResponseFuture { - pub(crate) fn new_left(left: A) -> ResponseFuture { - ResponseFuture { - inner: Either::Left(left), - } - } - - pub(crate) fn new_right(right: B) -> ResponseFuture { - ResponseFuture { - inner: Either::Right(right), - } - } -} - -impl Future for ResponseFuture -where - A: Future, - A::Error: Into, - B: Future, - B::Error: Into, -{ - type Item = A::Item; - type Error = Error; - - fn poll(&mut self) -> Poll { - use self::Either::*; - - match &mut self.inner { - Left(fut) => fut.poll().map_err(Into::into), - Right(fut) => fut.poll().map_err(Into::into), - } - } -} diff --git a/tower-util/src/either/mod.rs b/tower-util/src/either/mod.rs deleted file mode 100644 index 32acea0..0000000 --- a/tower-util/src/either/mod.rs +++ /dev/null @@ -1,63 +0,0 @@ -//! Contains `EitherService` and related types and functions. -//! -//! See `EitherService` documentation for more details. - -pub mod future; - -use self::future::ResponseFuture; -use _either::Either; -use futures::Poll; -use tower_service::Service; - -/// Combine two different service types into a single type. -/// -/// Both services must be of the same request, response, and error types. -/// `EitherService` is useful for handling conditional branching in service -/// middleware to different inner service types. -pub struct EitherService { - inner: Either, -} - -type Error = Box<::std::error::Error + Send + Sync>; - -impl EitherService { - pub fn new(inner: Either) -> EitherService - where - A: Service, - A::Error: Into, - B: Service, - B::Error: Into, - { - EitherService { inner } - } -} - -impl Service for EitherService -where - A: Service, - A::Error: Into, - B: Service, - B::Error: Into, -{ - type Response = A::Response; - type Error = Error; - type Future = ResponseFuture; - - fn poll_ready(&mut self) -> Poll<(), Self::Error> { - use self::Either::*; - - match &mut self.inner { - Left(service) => service.poll_ready().map_err(Into::into), - Right(service) => service.poll_ready().map_err(Into::into), - } - } - - fn call(&mut self, request: Request) -> Self::Future { - use self::Either::*; - - match &mut self.inner { - Left(service) => ResponseFuture::new_left(service.call(request)), - Right(service) => ResponseFuture::new_right(service.call(request)), - } - } -} diff --git a/tower-util/src/lib.rs b/tower-util/src/lib.rs index 7512d22..2dcac53 100644 --- a/tower-util/src/lib.rs +++ b/tower-util/src/lib.rs @@ -1,7 +1,5 @@ //! Various utility types and functions that are generally with Tower. -#[cfg(feature = "either")] -extern crate either as _either; #[macro_use] extern crate futures; #[cfg(feature = "io")] @@ -11,7 +9,6 @@ extern crate tower_service; mod boxed; mod call_all; -#[cfg(feature = "either")] mod either; pub mod layer; #[cfg(feature = "io")] @@ -25,8 +22,7 @@ mod service_fn; pub use crate::boxed::{BoxService, UnsyncBoxService}; pub use crate::call_all::{CallAll, CallAllUnordered}; -#[cfg(feature = "either")] -pub use crate::either::EitherService; +pub use crate::either::Either; #[cfg(feature = "io")] pub use crate::make_connection::MakeConnection; pub use crate::make_service::MakeService; diff --git a/tower/Cargo.toml b/tower/Cargo.toml index 91c5256..27e863e 100644 --- a/tower/Cargo.toml +++ b/tower/Cargo.toml @@ -33,7 +33,7 @@ make_service = [ [dependencies] futures = "0.1" tower-service = "0.2" -tower-util = { version = "0.1.0", path = "../tower-util", features = ["io", "either"] } +tower-util = { version = "0.1.0", path = "../tower-util", features = ["io"] } tower-layer = { version = "0.1", path = "../tower-layer" } tower-in-flight-limit = { version = "0.1", path = "../tower-in-flight-limit", optional = true } tower-rate-limit = { version = "0.1", path = "../tower-rate-limit", optional = true } diff --git a/tower/src/util.rs b/tower/src/util.rs index a4c5ba0..827629b 100644 --- a/tower/src/util.rs +++ b/tower/src/util.rs @@ -3,7 +3,7 @@ pub use tower_util::BoxService; pub use tower_util::CallAll; pub use tower_util::CallAllUnordered; -pub use tower_util::EitherService; +pub use tower_util::Either; pub use tower_util::Oneshot; pub use tower_util::OptionService; pub use tower_util::Ready;