layer: Add an identity layer (#195)

Tied to #175
This commit is contained in:
Lucio Franco 2019-03-24 16:53:09 -04:00 committed by GitHub
parent db2f0ecfb3
commit 0e70f1320e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,47 @@
use std::error::Error;
use std::fmt;
use tower_service::Service;
use Layer;
/// A no-op middleware.
///
/// When wrapping a `Service`, the `Identity` layer returns the provided
/// service without modifying it.
#[derive(Debug, Default, Clone)]
pub struct Identity {
_p: (),
}
impl Identity {
/// Create a new `Identity` value
pub fn new() -> Identity {
Identity { _p: () }
}
}
/// Decorates a `Service`, transforming either the request or the response.
impl<S, Request> Layer<S, Request> for Identity
where
S: Service<Request>,
{
type Response = S::Response;
type Error = S::Error;
type LayerError = Never;
type Service = S;
fn layer(&self, inner: S) -> Result<Self::Service, Self::LayerError> {
Ok(inner)
}
}
/// An error that can never occur.
#[derive(Debug)]
pub enum Never {}
impl fmt::Display for Never {
fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
match *self {}
}
}
impl ::std::error::Error for Never {}

View File

@ -3,8 +3,10 @@
use Layer;
mod chain;
mod identity;
pub use self::chain::{Chain, ChainError};
pub use self::identity::Identity;
/// An extension trait for `Layer`'s that provides a variety of convenient
/// adapters.