1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-30 20:42:52 +01:00

add helpers

This commit is contained in:
Nikolay Kim 2019-03-12 12:53:08 -07:00
parent bef199f831
commit ef9bfb8981
7 changed files with 314 additions and 123 deletions

View File

@ -1,5 +1,16 @@
# Changes
## [0.3.4] - 2019-03-12
### Added
* Add `Transform::from_err()` combinator
* Add `apply_fn` and `apply_fn_factory` helpers
* Add `apply_transform` helper
## [0.3.3] - 2019-03-09
### Added

View File

@ -4,6 +4,34 @@ use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
/// Apply tranform function to a service
pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out>
where
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
U: IntoService<T>,
{
Apply::new(service.into_service(), f)
}
/// Create fractory for `apply_fn` service.
pub fn apply_fn_factory<T, F, In, Out, Cfg, U>(
service: U,
f: F,
) -> ApplyNewService<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
U: IntoNewService<T, Cfg>,
{
ApplyNewService::new(service.into_new_service(), f)
}
#[doc(hidden)]
/// `Apply` service combinator
pub struct Apply<T, F, In, Out>
where

View File

@ -5,6 +5,19 @@ use futures::IntoFuture;
use crate::{Apply, IntoTransform, Service, Transform};
/// Use function as transform service
pub fn fn_transform<F, S, In, Out, Err>(
f: F,
) -> impl Transform<S, Request = In, Response = Out::Item, Error = Out::Error, InitError = Err>
where
S: Service,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
FnTransform::new(f)
}
pub struct FnTransform<F, S, In, Out, Err>
where
F: FnMut(In, &mut S) -> Out + Clone,

View File

@ -21,21 +21,34 @@ mod map_err;
mod map_init_err;
mod then;
mod transform;
mod transform_map_init_err;
mod transform_err;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn` instead")]
pub use self::apply::Apply;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_fn_factory` instead")]
pub use self::apply::ApplyNewService;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `fn_transform` instead")]
pub use self::fn_transform::FnTransform;
#[doc(hidden)]
#[deprecated(since = "0.3.4", note = "please use `apply_transform` instead")]
pub use self::transform::ApplyTransform;
pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::apply_cfg::ApplyConfig;
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::FnTransform;
pub use self::fn_transform::fn_transform;
pub use self::from_err::{FromErr, FromErrNewService};
pub use self::map::{Map, MapNewService};
pub use self::map_err::{MapErr, MapErrNewService};
pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{ApplyTransform, IntoTransform, Transform};
pub use self::transform::{apply_transform, IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {

View File

@ -3,8 +3,8 @@ use std::sync::Arc;
use futures::{Async, Future, IntoFuture, Poll};
use crate::transform_map_init_err::TransformMapInitErr;
use crate::{NewService, Service};
use crate::transform_err::{TransformFromErr, TransformMapInitErr};
use crate::{IntoNewService, NewService, Service};
/// `Transform` service factory.
///
@ -36,7 +36,7 @@ pub trait Transform<S> {
/// Create and return a new service value asynchronously.
fn new_transform(&self, service: S) -> Self::Future;
/// Map this service's factory init error to a different error,
/// Map this service's factory error to a different error,
/// returning a new transform service factory.
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where
@ -45,6 +45,32 @@ pub trait Transform<S> {
{
TransformMapInitErr::new(self, f)
}
/// Map this service's init error to any error implementing `From` for
/// this service`s `Error`.
///
/// Note that this function consumes the receiving transform and returns a
/// wrapped version of it.
fn from_err<E>(self) -> TransformFromErr<Self, S, E>
where
Self: Sized,
E: From<Self::InitError>,
{
TransformFromErr::new(self)
}
// /// Map this service's init error to service's init error
// /// if it is implementing `Into` to this service`s `InitError`.
// ///
// /// Note that this function consumes the receiving transform and returns a
// /// wrapped version of it.
// fn into_err<E>(self) -> TransformIntoErr<Self, S>
// where
// Self: Sized,
// Self::InitError: From<Self::InitError>,
// {
// TransformFromErr::new(self)
// }
}
impl<T, S> Transform<S> for Rc<T>
@ -97,33 +123,65 @@ where
}
}
/// `Apply` transform new service
#[derive(Clone)]
pub struct ApplyTransform<T, A, C> {
a: A,
/// Apply transform to service factory. Function returns
/// services factory that in initialization creates
/// service and applies transform to this service.
pub fn apply_transform<T, S, C, F, U>(
t: F,
service: U,
) -> impl NewService<
C,
Request = T::Request,
Response = T::Response,
Error = T::Error,
Service = T::Transform,
InitError = S::InitError,
> + Clone
where
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
F: IntoTransform<T, S::Service>,
U: IntoNewService<S, C>,
{
ApplyTransform::new(t.into_transform(), service.into_new_service())
}
/// `Apply` transform to new service
pub struct ApplyTransform<T, S, C> {
s: Rc<S>,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, A, C> ApplyTransform<T, A, C>
impl<T, S, C> ApplyTransform<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<F: IntoTransform<T, A::Service>>(t: F, a: A) -> Self {
/// Create new `ApplyTransform` new service instance
pub fn new<F: IntoTransform<T, S::Service>>(t: F, service: S) -> Self {
Self {
a,
s: Rc::new(service),
t: Rc::new(t.into_transform()),
_t: std::marker::PhantomData,
}
}
}
impl<T, A, C> NewService<C> for ApplyTransform<T, A, C>
impl<T, S, C> Clone for ApplyTransform<T, S, C> {
fn clone(&self) -> Self {
ApplyTransform {
s: self.s.clone(),
t: self.t.clone(),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> NewService<C> for ApplyTransform<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
type Request = T::Request;
type Response = T::Response;
@ -131,31 +189,31 @@ where
type Service = T::Transform;
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, A, C>;
type Future = ApplyTransformFuture<T, S, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg).into_future(),
fut_a: self.s.new_service(cfg).into_future(),
fut_t: None,
}
}
}
pub struct ApplyTransformFuture<T, A, C>
pub struct ApplyTransformFuture<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
fut_a: A::Future,
fut_a: S::Future,
fut_t: Option<<T::Future as IntoFuture>::Future>,
t_cell: Rc<T>,
}
impl<T, A, C> Future for ApplyTransformFuture<T, A, C>
impl<T, S, C> Future for ApplyTransformFuture<T, S, C>
where
A: NewService<C>,
T: Transform<A::Service, Error = A::Error, InitError = A::InitError>,
S: NewService<C>,
T: Transform<S::Service, InitError = S::InitError>,
{
type Item = T::Transform;
type Error = T::InitError;

View File

@ -0,0 +1,162 @@
use std::marker::PhantomData;
use futures::{Future, Poll};
use super::Transform;
/// Transform for the `map_err` combinator, changing the type of a new
/// transform's init error.
///
/// This is created by the `Transform::map_err` method.
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `TransformMapErr` new transform instance
pub fn new(t: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture {
fut: self.t.new_transform(service),
f: self.f.clone(),
}
}
}
pub struct TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(&self.f)
}
}
/// Transform for the `from_err` combinator, changing the type of a new
/// transform's init error.
///
/// This is created by the `Transform::from_err` method.
pub struct TransformFromErr<T, S, E> {
t: T,
e: PhantomData<(S, E)>,
}
impl<T, S, E> TransformFromErr<T, S, E>
where
T: Transform<S>,
E: From<T::InitError>,
{
/// Create new `TransformFromErr` new transform instance
pub fn new(t: T) -> Self {
Self { t, e: PhantomData }
}
}
impl<T, S, E> Clone for TransformFromErr<T, S, E>
where
T: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
e: PhantomData,
}
}
}
impl<T, S, E> Transform<S> for TransformFromErr<T, S, E>
where
T: Transform<S>,
E: From<T::InitError>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformFromErrFuture<T, S, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformFromErrFuture {
fut: self.t.new_transform(service),
_t: PhantomData,
}
}
}
pub struct TransformFromErrFuture<T, S, E>
where
T: Transform<S>,
E: From<T::InitError>,
{
fut: T::Future,
_t: PhantomData<E>,
}
impl<T, S, E> Future for TransformFromErrFuture<T, S, E>
where
T: Transform<S>,
E: From<T::InitError>,
{
type Item = T::Transform;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(E::from)
}
}

View File

@ -1,94 +0,0 @@
use std::marker::PhantomData;
use futures::{Future, Poll};
use super::Transform;
/// NewTransform for the `map_init_err` combinator, changing the type of a new
/// transform's error.
///
/// This is created by the `NewTransform::map_init_err` method.
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapInitErr<T, S, F, E> {
/// Create new `MapInitErr` new transform instance
pub fn new(t: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
t: self.t.clone(),
f: self.f.clone(),
e: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
}
}
pub struct TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapInitErrFuture { f, fut }
}
}
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Item = T::Transform;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.fut.poll().map_err(&self.f)
}
}