1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 18:02:58 +01:00

update utils; add NewTransform::map_init_err

This commit is contained in:
Nikolay Kim 2019-03-02 13:18:01 -08:00
parent d0b8b6940c
commit 668e4f9ac4
10 changed files with 127 additions and 23 deletions

View File

@ -24,3 +24,4 @@ path = "src/lib.rs"
[dependencies]
futures = "0.1.24"
void = "1.0.2"

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use crate::Never;
use crate::Void;
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
/// Create `NewService` for function that can act as Service
@ -125,7 +125,7 @@ where
type Error = Out::Error;
type Service = FnService<F, Req, Out>;
type InitError = Never;
type InitError = Void;
type Future = FutureResult<Self::Service, Self::InitError>;
fn new_service(&self, _: &Cfg) -> Self::Future {

View File

@ -3,6 +3,8 @@ use std::sync::Arc;
use futures::{Future, IntoFuture, Poll};
pub use void::Void;
mod and_then;
mod and_then_apply;
mod and_then_apply_fn;
@ -19,6 +21,7 @@ mod map_init_err;
mod then;
mod transform;
mod transform_map_err;
mod transform_map_init_err;
pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::{AndThenTransform, AndThenTransformNewService};
@ -33,9 +36,6 @@ pub use self::map_init_err::MapInitErr;
pub use self::then::{Then, ThenNewService};
pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform};
#[derive(Copy, Clone, Debug)]
pub enum Never {}
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
/// Requests handled by the service.

View File

@ -4,6 +4,7 @@ use std::sync::Arc;
use futures::{Future, Poll};
use crate::transform_map_err::{TransformMapErr, TransformMapErrNewTransform};
use crate::transform_map_init_err::TransformMapInitErr;
use crate::Service;
/// An asynchronous function for transforming service call result.
@ -85,6 +86,16 @@ pub trait NewTransform<Service, Config = ()> {
{
TransformMapErrNewTransform::new(self, f)
}
/// Map this service's factory init error to a different error,
/// returning a new transform service factory.
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, Service, Config, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E,
{
TransformMapInitErr::new(self, f)
}
}
impl<'a, T, S> Transform<S> for &'a mut T

View File

@ -0,0 +1,94 @@
use std::marker::PhantomData;
use futures::{Future, Poll};
use super::NewTransform;
/// 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, C, F, E> {
t: T,
f: F,
e: PhantomData<(S, C, E)>,
}
impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> {
/// Create new `MapInitErr` new transform instance
pub fn new(t: T, f: F) -> Self
where
T: NewTransform<S, C>,
F: Fn(T::InitError) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, C, F, E> Clone for TransformMapInitErr<T, S, C, 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, C, F, E> NewTransform<S, C> for TransformMapInitErr<T, S, C, F, E>
where
T: NewTransform<S, C>,
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, C, F, E>;
fn new_transform(&self, cfg: &C) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(cfg), self.f.clone())
}
}
pub struct TransformMapInitErrFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, C, F, E> TransformMapInitErrFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::InitError) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapInitErrFuture { f, fut }
}
}
impl<T, S, C, F, E> Future for TransformMapInitErrFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
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(|e| (self.f)(e))
}
}

View File

@ -1,4 +1,4 @@
use actix_service::{NewTransform, Service, Transform};
use actix_service::{NewTransform, Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
@ -24,15 +24,15 @@ impl Default for InFlight {
}
}
impl<T: Service> NewTransform<T> for InFlight {
impl<T: Service, C> NewTransform<T, C> for InFlight {
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type InitError = ();
type InitError = Void;
type Transform = InFlightService;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self) -> Self::Future {
fn new_transform(&self, _: &C) -> Self::Future {
ok(InFlightService::new(self.max_inflight))
}
}

View File

@ -1,13 +1,12 @@
use std::marker::PhantomData;
use std::time::{Duration, Instant};
use actix_service::{NewService, Service};
use actix_service::{NewService, Service, Void};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use tokio_timer::Delay;
use super::time::{LowResTime, LowResTimeService};
use super::Never;
pub struct KeepAlive<R, E, F> {
f: F,
@ -51,7 +50,7 @@ where
type Request = R;
type Response = R;
type Error = E;
type InitError = Never;
type InitError = Void;
type Service = KeepAliveService<R, E, F>;
type Future = FutureResult<Self::Service, Self::InitError>;

View File

@ -3,7 +3,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
use actix_service::{NewTransform, Service, Transform};
use actix_service::{NewTransform, Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::task::AtomicTask;
use futures::unsync::oneshot;
@ -85,7 +85,7 @@ where
}
}
impl<S> NewTransform<S> for InOrder<S>
impl<S, C> NewTransform<S, C> for InOrder<S>
where
S: Service,
S::Response: 'static,
@ -95,11 +95,11 @@ where
type Request = S::Request;
type Response = S::Response;
type Error = InOrderError<S::Error>;
type InitError = ();
type InitError = Void;
type Transform = InOrderService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self) -> Self::Future {
fn new_transform(&self, _: &C) -> Self::Future {
ok(InOrderService::new())
}
}

View File

@ -1,12 +1,11 @@
use std::time::{self, Duration, Instant};
use actix_service::{NewService, Service};
use actix_service::{NewService, Service, Void};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use tokio_timer::sleep;
use super::cell::Cell;
use super::Never;
#[derive(Clone, Debug)]
pub struct LowResTime(Cell<Inner>);
@ -45,8 +44,8 @@ impl Default for LowResTime {
impl NewService<()> for LowResTime {
type Request = ();
type Response = Instant;
type Error = Never;
type InitError = Never;
type Error = Void;
type InitError = Void;
type Service = LowResTimeService;
type Future = FutureResult<Self::Service, Self::InitError>;
@ -92,7 +91,7 @@ impl LowResTimeService {
impl Service for LowResTimeService {
type Request = ();
type Response = Instant;
type Error = Never;
type Error = Void;
type Future = FutureResult<Self::Response, Self::Error>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {

View File

@ -80,7 +80,7 @@ impl<E> Clone for Timeout<E> {
}
}
impl<S, E> NewTransform<S> for Timeout<E>
impl<S, C, E> NewTransform<S, C> for Timeout<E>
where
S: Service,
{
@ -91,7 +91,7 @@ where
type Transform = TimeoutService;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self) -> Self::Future {
fn new_transform(&self, _: &C) -> Self::Future {
ok(TimeoutService {
timeout: self.timeout,
})