Rename `EitherService` -> `Either` (#218)

This commit is contained in:
Carl Lerche 2019-03-29 19:17:02 -07:00 committed by GitHub
parent 3fd5b581cc
commit 117018f319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 113 deletions

View File

@ -22,7 +22,6 @@ categories = ["asynchronous", "network-programming"]
io = ["tokio-io"] io = ["tokio-io"]
[dependencies] [dependencies]
either = { version = "1.5.1", optional = true }
futures = "0.1.23" futures = "0.1.23"
tokio-io = { version = "0.1.12", optional = true } tokio-io = { version = "0.1.12", optional = true }
tower-service = "0.2.0" tower-service = "0.2.0"

68
tower-util/src/either.rs Normal file
View File

@ -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, B> {
A(A),
B(B),
}
type Error = Box<::std::error::Error + Send + Sync>;
impl<A, B, Request> Service<Request> for Either<A, B>
where
A: Service<Request>,
A::Error: Into<Error>,
B: Service<Request, Response = A::Response>,
B::Error: Into<Error>,
{
type Response = A::Response;
type Error = Error;
type Future = Either<A::Future, B::Future>;
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<A, B> Future for Either<A, B>
where
A: Future,
A::Error: Into<Error>,
B: Future<Item = A::Item>,
B::Error: Into<Error>,
{
type Item = A::Item;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
use self::Either::*;
match self {
A(fut) => fut.poll().map_err(Into::into),
B(fut) => fut.poll().map_err(Into::into),
}
}
}

View File

@ -1,42 +0,0 @@
use super::Error;
use _either::Either;
use futures::{Future, Poll};
#[derive(Debug)]
pub struct ResponseFuture<A, B> {
inner: Either<A, B>,
}
impl<A, B> ResponseFuture<A, B> {
pub(crate) fn new_left(left: A) -> ResponseFuture<A, B> {
ResponseFuture {
inner: Either::Left(left),
}
}
pub(crate) fn new_right(right: B) -> ResponseFuture<A, B> {
ResponseFuture {
inner: Either::Right(right),
}
}
}
impl<A, B> Future for ResponseFuture<A, B>
where
A: Future,
A::Error: Into<Error>,
B: Future<Item = A::Item>,
B::Error: Into<Error>,
{
type Item = A::Item;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
use self::Either::*;
match &mut self.inner {
Left(fut) => fut.poll().map_err(Into::into),
Right(fut) => fut.poll().map_err(Into::into),
}
}
}

View File

@ -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<A, B> {
inner: Either<A, B>,
}
type Error = Box<::std::error::Error + Send + Sync>;
impl<A, B> EitherService<A, B> {
pub fn new<Request>(inner: Either<A, B>) -> EitherService<A, B>
where
A: Service<Request>,
A::Error: Into<Error>,
B: Service<Request, Response = A::Response>,
B::Error: Into<Error>,
{
EitherService { inner }
}
}
impl<A, B, Request> Service<Request> for EitherService<A, B>
where
A: Service<Request>,
A::Error: Into<Error>,
B: Service<Request, Response = A::Response>,
B::Error: Into<Error>,
{
type Response = A::Response;
type Error = Error;
type Future = ResponseFuture<A::Future, B::Future>;
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)),
}
}
}

View File

@ -1,7 +1,5 @@
//! Various utility types and functions that are generally with Tower. //! Various utility types and functions that are generally with Tower.
#[cfg(feature = "either")]
extern crate either as _either;
#[macro_use] #[macro_use]
extern crate futures; extern crate futures;
#[cfg(feature = "io")] #[cfg(feature = "io")]
@ -11,7 +9,6 @@ extern crate tower_service;
mod boxed; mod boxed;
mod call_all; mod call_all;
#[cfg(feature = "either")]
mod either; mod either;
pub mod layer; pub mod layer;
#[cfg(feature = "io")] #[cfg(feature = "io")]
@ -25,8 +22,7 @@ mod service_fn;
pub use crate::boxed::{BoxService, UnsyncBoxService}; pub use crate::boxed::{BoxService, UnsyncBoxService};
pub use crate::call_all::{CallAll, CallAllUnordered}; pub use crate::call_all::{CallAll, CallAllUnordered};
#[cfg(feature = "either")] pub use crate::either::Either;
pub use crate::either::EitherService;
#[cfg(feature = "io")] #[cfg(feature = "io")]
pub use crate::make_connection::MakeConnection; pub use crate::make_connection::MakeConnection;
pub use crate::make_service::MakeService; pub use crate::make_service::MakeService;

View File

@ -33,7 +33,7 @@ make_service = [
[dependencies] [dependencies]
futures = "0.1" futures = "0.1"
tower-service = "0.2" 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-layer = { version = "0.1", path = "../tower-layer" }
tower-in-flight-limit = { version = "0.1", path = "../tower-in-flight-limit", optional = true } 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 } tower-rate-limit = { version = "0.1", path = "../tower-rate-limit", optional = true }

View File

@ -3,7 +3,7 @@
pub use tower_util::BoxService; pub use tower_util::BoxService;
pub use tower_util::CallAll; pub use tower_util::CallAll;
pub use tower_util::CallAllUnordered; pub use tower_util::CallAllUnordered;
pub use tower_util::EitherService; pub use tower_util::Either;
pub use tower_util::Oneshot; pub use tower_util::Oneshot;
pub use tower_util::OptionService; pub use tower_util::OptionService;
pub use tower_util::Ready; pub use tower_util::Ready;