diff --git a/tower-layer/src/util/identity.rs b/tower-layer/src/util/identity.rs new file mode 100644 index 0000000..072dba0 --- /dev/null +++ b/tower-layer/src/util/identity.rs @@ -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 Layer for Identity +where + S: Service, +{ + type Response = S::Response; + type Error = S::Error; + type LayerError = Never; + type Service = S; + + fn layer(&self, inner: S) -> Result { + 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 {} diff --git a/tower-layer/src/util/mod.rs b/tower-layer/src/util/mod.rs index a9fc1c1..ceafb65 100644 --- a/tower-layer/src/util/mod.rs +++ b/tower-layer/src/util/mod.rs @@ -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.