From 7dca264546b8981c432c1ecf18195cc9b3cf97f5 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 4 Feb 2019 10:55:39 -0800 Subject: [PATCH] move transform map_err combinator to separate module --- actix-service/src/lib.rs | 1 + actix-service/src/transform.rs | 190 +------------------------ actix-service/src/transform_map_err.rs | 188 ++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 187 deletions(-) create mode 100644 actix-service/src/transform_map_err.rs diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 0672c324..fb2186b0 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -14,6 +14,7 @@ mod map_err; mod map_init_err; mod then; mod transform; +mod transform_map_err; pub use self::and_then::{AndThen, AndThenNewService}; use self::and_then_apply::{AndThenTransform, AndThenTransformNewService}; diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 96c553ba..836e0c83 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -1,8 +1,7 @@ -use std::marker::PhantomData; +use futures::{Future, Poll}; -use futures::{Async, Future, Poll}; - -use super::Service; +use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform}; +use crate::Service; /// An asynchronous function for transforming service call result. pub trait Transform { @@ -156,186 +155,3 @@ where self } } - -/// Service for the `map_err` combinator, changing the type of a transform's -/// error. -/// -/// This is created by the `Transform::map_err` method. -pub struct TransformMapErr { - transform: T, - f: F, - _t: PhantomData<(S, E)>, -} - -impl TransformMapErr { - /// Create new `MapErr` combinator - pub fn new(transform: T, f: F) -> Self - where - T: Transform, - F: Fn(T::Error) -> E, - { - Self { - transform, - f, - _t: PhantomData, - } - } -} - -impl Clone for TransformMapErr -where - T: Clone, - F: Clone, -{ - fn clone(&self) -> Self { - TransformMapErr { - transform: self.transform.clone(), - f: self.f.clone(), - _t: PhantomData, - } - } -} - -impl Transform for TransformMapErr -where - T: Transform, - F: Fn(T::Error) -> E + Clone, -{ - type Request = T::Request; - type Response = T::Response; - type Error = E; - type Future = TransformMapErrFuture; - - fn poll_ready(&mut self) -> Poll<(), Self::Error> { - self.transform.poll_ready().map_err(&self.f) - } - - fn call(&mut self, req: T::Request, service: &mut S) -> Self::Future { - TransformMapErrFuture::new(self.transform.call(req, service), self.f.clone()) - } -} - -pub struct TransformMapErrFuture -where - T: Transform, - F: Fn(T::Error) -> E, -{ - f: F, - fut: T::Future, -} - -impl TransformMapErrFuture -where - T: Transform, - F: Fn(T::Error) -> E, -{ - fn new(fut: T::Future, f: F) -> Self { - TransformMapErrFuture { f, fut } - } -} - -impl Future for TransformMapErrFuture -where - T: Transform, - F: Fn(T::Error) -> E, -{ - type Item = T::Response; - type Error = E; - - fn poll(&mut self) -> Poll { - self.fut.poll().map_err(&self.f) - } -} - -/// NewTransform for the `map_err` combinator, changing the type of a new -/// transform's error. -/// -/// This is created by the `NewTransform::map_err` method. -pub struct TransformMapErrNewTransform { - t: T, - f: F, - e: PhantomData<(S, E)>, -} - -impl TransformMapErrNewTransform { - /// Create new `MapErr` new service instance - pub fn new(t: T, f: F) -> Self - where - T: NewTransform, - F: Fn(T::Error) -> E, - { - Self { - t, - f, - e: PhantomData, - } - } -} - -impl Clone for TransformMapErrNewTransform -where - T: Clone, - F: Clone, -{ - fn clone(&self) -> Self { - Self { - t: self.t.clone(), - f: self.f.clone(), - e: PhantomData, - } - } -} - -impl NewTransform for TransformMapErrNewTransform -where - T: NewTransform, - F: Fn(T::Error) -> E + Clone, -{ - type Request = T::Request; - type Response = T::Response; - type Error = E; - type Transform = TransformMapErr; - - type InitError = T::InitError; - type Future = TransformMapErrNewTransformFuture; - - fn new_transform(&self) -> Self::Future { - TransformMapErrNewTransformFuture::new(self.t.new_transform(), self.f.clone()) - } -} - -pub struct TransformMapErrNewTransformFuture -where - T: NewTransform, - F: Fn(T::Error) -> E, -{ - fut: T::Future, - f: F, -} - -impl TransformMapErrNewTransformFuture -where - T: NewTransform, - F: Fn(T::Error) -> E, -{ - fn new(fut: T::Future, f: F) -> Self { - TransformMapErrNewTransformFuture { f, fut } - } -} - -impl Future for TransformMapErrNewTransformFuture -where - T: NewTransform, - F: Fn(T::Error) -> E + Clone, -{ - type Item = TransformMapErr; - type Error = T::InitError; - - fn poll(&mut self) -> Poll { - if let Async::Ready(tr) = self.fut.poll()? { - Ok(Async::Ready(TransformMapErr::new(tr, self.f.clone()))) - } else { - Ok(Async::NotReady) - } - } -} diff --git a/actix-service/src/transform_map_err.rs b/actix-service/src/transform_map_err.rs new file mode 100644 index 00000000..9ae9f65a --- /dev/null +++ b/actix-service/src/transform_map_err.rs @@ -0,0 +1,188 @@ +use std::marker::PhantomData; + +use futures::{Async, Future, Poll}; + +use super::{NewTransform, Transform}; + +/// Service for the `map_err` combinator, changing the type of a transform's +/// error. +/// +/// This is created by the `Transform::map_err` method. +pub struct TransformMapErr { + transform: T, + f: F, + _t: PhantomData<(S, E)>, +} + +impl TransformMapErr { + /// Create new `MapErr` combinator + pub fn new(transform: T, f: F) -> Self + where + T: Transform, + F: Fn(T::Error) -> E, + { + Self { + transform, + f, + _t: PhantomData, + } + } +} + +impl Clone for TransformMapErr +where + T: Clone, + F: Clone, +{ + fn clone(&self) -> Self { + TransformMapErr { + transform: self.transform.clone(), + f: self.f.clone(), + _t: PhantomData, + } + } +} + +impl Transform for TransformMapErr +where + T: Transform, + F: Fn(T::Error) -> E + Clone, +{ + type Request = T::Request; + type Response = T::Response; + type Error = E; + type Future = TransformMapErrFuture; + + fn poll_ready(&mut self) -> Poll<(), Self::Error> { + self.transform.poll_ready().map_err(&self.f) + } + + fn call(&mut self, req: T::Request, service: &mut S) -> Self::Future { + TransformMapErrFuture::new(self.transform.call(req, service), self.f.clone()) + } +} + +pub struct TransformMapErrFuture +where + T: Transform, + F: Fn(T::Error) -> E, +{ + f: F, + fut: T::Future, +} + +impl TransformMapErrFuture +where + T: Transform, + F: Fn(T::Error) -> E, +{ + fn new(fut: T::Future, f: F) -> Self { + TransformMapErrFuture { f, fut } + } +} + +impl Future for TransformMapErrFuture +where + T: Transform, + F: Fn(T::Error) -> E, +{ + type Item = T::Response; + type Error = E; + + fn poll(&mut self) -> Poll { + self.fut.poll().map_err(&self.f) + } +} + +/// NewTransform for the `map_err` combinator, changing the type of a new +/// transform's error. +/// +/// This is created by the `NewTransform::map_err` method. +pub struct TransformMapErrNewTransform { + t: T, + f: F, + e: PhantomData<(S, E)>, +} + +impl TransformMapErrNewTransform { + /// Create new `MapErr` new service instance + pub fn new(t: T, f: F) -> Self + where + T: NewTransform, + F: Fn(T::Error) -> E, + { + Self { + t, + f, + e: PhantomData, + } + } +} + +impl Clone for TransformMapErrNewTransform +where + T: Clone, + F: Clone, +{ + fn clone(&self) -> Self { + Self { + t: self.t.clone(), + f: self.f.clone(), + e: PhantomData, + } + } +} + +impl NewTransform for TransformMapErrNewTransform +where + T: NewTransform, + F: Fn(T::Error) -> E + Clone, +{ + type Request = T::Request; + type Response = T::Response; + type Error = E; + type Transform = TransformMapErr; + + type InitError = T::InitError; + type Future = TransformMapErrNewTransformFuture; + + fn new_transform(&self) -> Self::Future { + TransformMapErrNewTransformFuture::new(self.t.new_transform(), self.f.clone()) + } +} + +pub struct TransformMapErrNewTransformFuture +where + T: NewTransform, + F: Fn(T::Error) -> E, +{ + fut: T::Future, + f: F, +} + +impl TransformMapErrNewTransformFuture +where + T: NewTransform, + F: Fn(T::Error) -> E, +{ + fn new(fut: T::Future, f: F) -> Self { + TransformMapErrNewTransformFuture { f, fut } + } +} + +impl Future for TransformMapErrNewTransformFuture +where + T: NewTransform, + F: Fn(T::Error) -> E + Clone, +{ + type Item = TransformMapErr; + type Error = T::InitError; + + fn poll(&mut self) -> Poll { + if let Async::Ready(tr) = self.fut.poll()? { + Ok(Async::Ready(TransformMapErr::new(tr, self.f.clone()))) + } else { + Ok(Async::NotReady) + } + } +}