1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-30 19:32:53 +01:00

change to IntoFuture

This commit is contained in:
Nikolay Kim 2019-03-04 20:29:35 -08:00
parent 700abc997e
commit ed14e6b8ea
15 changed files with 89 additions and 64 deletions

View File

@ -1,5 +1,14 @@
# Changes # Changes
## [0.3.2] - 2019-03-04
### Changed
* Change `NewService::Future` and `Transform::Future` to the `IntoFuture` trait.
* Export `AndThenTransform` type
## [0.3.1] - 2019-03-04 ## [0.3.1] - 2019-03-04
### Changed ### Changed

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-service" name = "actix-service"
version = "0.3.1" version = "0.3.2"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix Service" description = "Actix Service"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, IntoFuture, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
@ -142,7 +142,10 @@ where
type Future = AndThenNewServiceFuture<A, B, C>; type Future = AndThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) AndThenNewServiceFuture::new(
self.a.new_service(cfg).into_future(),
self.b.new_service(cfg).into_future(),
)
} }
} }
@ -165,8 +168,8 @@ where
A: NewService<C>, A: NewService<C>,
B: NewService<C, Request = A::Response>, B: NewService<C, Request = A::Response>,
{ {
fut_b: B::Future, fut_b: <B::Future as IntoFuture>::Future,
fut_a: A::Future, fut_a: <A::Future as IntoFuture>::Future,
a: Option<A::Service>, a: Option<A::Service>,
b: Option<B::Service>, b: Option<B::Service>,
} }
@ -176,7 +179,10 @@ where
A: NewService<C>, A: NewService<C>,
B: NewService<C, Request = A::Response>, B: NewService<C, Request = A::Response>,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(
fut_a: <A::Future as IntoFuture>::Future,
fut_b: <B::Future as IntoFuture>::Future,
) -> Self {
AndThenNewServiceFuture { AndThenNewServiceFuture {
fut_a, fut_a,
fut_b, fut_b,

View File

@ -1,20 +1,20 @@
use std::rc::Rc; use std::rc::Rc;
use futures::{Async, Future, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use crate::and_then::AndThen; use crate::and_then::AndThen;
use crate::from_err::FromErr; use crate::from_err::FromErr;
use crate::{NewService, Transform}; use crate::{NewService, Transform};
/// `Apply` new service combinator /// `Apply` new service combinator
pub struct AndThenTransformNewService<T, A, B, C> { pub struct AndThenTransform<T, A, B, C> {
a: A, a: A,
b: B, b: B,
t: Rc<T>, t: Rc<T>,
_t: std::marker::PhantomData<C>, _t: std::marker::PhantomData<C>,
} }
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C> impl<T, A, B, C> AndThenTransform<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
@ -32,7 +32,7 @@ where
} }
} }
impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C> impl<T, A, B, C> Clone for AndThenTransform<T, A, B, C>
where where
A: Clone, A: Clone,
B: Clone, B: Clone,
@ -47,7 +47,7 @@ where
} }
} }
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C> impl<T, A, B, C> NewService<C> for AndThenTransform<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
@ -60,36 +60,36 @@ where
type InitError = T::InitError; type InitError = T::InitError;
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>; type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Future = AndThenTransformNewServiceFuture<T, A, B, C>; type Future = AndThenTransformFuture<T, A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformNewServiceFuture { AndThenTransformFuture {
a: None, a: None,
t: None, t: None,
t_cell: self.t.clone(), t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg), fut_a: self.a.new_service(cfg).into_future(),
fut_b: self.b.new_service(cfg), fut_b: self.b.new_service(cfg).into_future(),
fut_t: None, fut_t: None,
} }
} }
} }
pub struct AndThenTransformNewServiceFuture<T, A, B, C> pub struct AndThenTransformFuture<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>, T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>, T::Error: From<A::Error>,
{ {
fut_a: A::Future, fut_a: <A::Future as IntoFuture>::Future,
fut_b: B::Future, fut_b: <B::Future as IntoFuture>::Future,
fut_t: Option<T::Future>, fut_t: Option<<T::Future as IntoFuture>::Future>,
a: Option<A::Service>, a: Option<A::Service>,
t: Option<T::Transform>, t: Option<T::Transform>,
t_cell: Rc<T>, t_cell: Rc<T>,
} }
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C> impl<T, A, B, C> Future for AndThenTransformFuture<T, A, B, C>
where where
A: NewService<C>, A: NewService<C>,
B: NewService<C, InitError = A::InitError>, B: NewService<C, InitError = A::InitError>,
@ -102,7 +102,7 @@ where
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.fut_t.is_none() { if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_b.poll()? { if let Async::Ready(service) = self.fut_b.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service)); self.fut_t = Some(self.t_cell.new_transform(service).into_future());
} }
} }

View File

@ -195,8 +195,8 @@ where
a: None, a: None,
b: None, b: None,
f: self.f.clone(), f: self.f.clone(),
fut_a: self.a.new_service(cfg), fut_a: self.a.new_service(cfg).into_future(),
fut_b: self.b.new_service(cfg), fut_b: self.b.new_service(cfg).into_future(),
} }
} }
} }
@ -209,8 +209,8 @@ where
Out: IntoFuture, Out: IntoFuture,
Out::Error: Into<A::Error>, Out::Error: Into<A::Error>,
{ {
fut_b: B::Future, fut_b: <B::Future as IntoFuture>::Future,
fut_a: A::Future, fut_a: <A::Future as IntoFuture>::Future,
f: Cell<F>, f: Cell<F>,
a: Option<A::Service>, a: Option<A::Service>,
b: Option<B::Service>, b: Option<B::Service>,

View File

@ -124,7 +124,7 @@ where
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>; type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone()) ApplyNewServiceFuture::new(self.service.new_service(cfg).into_future(), self.f.clone())
} }
} }
@ -134,7 +134,7 @@ where
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
fut: T::Future, fut: <T::Future as IntoFuture>::Future,
f: Option<F>, f: Option<F>,
r: PhantomData<(In, Out)>, r: PhantomData<(In, Out)>,
} }
@ -145,7 +145,7 @@ where
F: FnMut(In, &mut T::Service) -> Out + Clone, F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture, Out: IntoFuture,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
ApplyNewServiceFuture { ApplyNewServiceFuture {
f: Some(f), f: Some(f),
fut, fut,

View File

@ -1,5 +1,5 @@
use crate::{NewService, Service}; use crate::{NewService, Service};
use futures::{Future, Poll}; use futures::{Future, IntoFuture, Poll};
pub type BoxedService<Req, Res, Err> = Box< pub type BoxedService<Req, Res, Err> = Box<
Service< Service<
@ -96,7 +96,12 @@ where
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>; type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
Box::new(self.service.new_service(cfg).map(ServiceWrapper::boxed)) Box::new(
self.service
.new_service(cfg)
.into_future()
.map(ServiceWrapper::boxed),
)
} }
} }

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Async, Future, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use super::{NewService, Service}; use super::{NewService, Service};
@ -124,7 +124,7 @@ where
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
FromErrNewServiceFuture { FromErrNewServiceFuture {
fut: self.a.new_service(cfg), fut: self.a.new_service(cfg).into_future(),
e: PhantomData, e: PhantomData,
} }
} }
@ -135,7 +135,7 @@ where
A: NewService<C>, A: NewService<C>,
E: From<A::Error>, E: From<A::Error>,
{ {
fut: A::Future, fut: <A::Future as IntoFuture>::Future,
e: PhantomData<E>, e: PhantomData<E>,
} }

View File

@ -23,7 +23,7 @@ mod transform;
mod transform_map_init_err; mod transform_map_init_err;
pub use self::and_then::{AndThen, AndThenNewService}; pub use self::and_then::{AndThen, AndThenNewService};
use self::and_then_apply::AndThenTransformNewService; pub use self::and_then_apply::AndThenTransform;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService}; use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService}; pub use self::apply::{Apply, ApplyNewService};
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService}; pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
@ -199,7 +199,7 @@ pub trait NewService<Config = ()> {
type InitError; type InitError;
/// The future of the `Service` instance. /// The future of the `Service` instance.
type Future: Future<Item = Self::Service, Error = Self::InitError>; type Future: IntoFuture<Item = Self::Service, Error = Self::InitError>;
/// Create and return a new service value asynchronously. /// Create and return a new service value asynchronously.
fn new_service(&self, cfg: &Config) -> Self::Future; fn new_service(&self, cfg: &Config) -> Self::Future;
@ -210,7 +210,7 @@ pub trait NewService<Config = ()> {
self, self,
transform: T1, transform: T1,
service: B1, service: B1,
) -> AndThenTransformNewService<T, Self, B, Config> ) -> AndThenTransform<T, Self, B, Config>
where where
Self: Sized, Self: Sized,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>, T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
@ -219,11 +219,7 @@ pub trait NewService<Config = ()> {
B: NewService<Config, InitError = Self::InitError>, B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>, B1: IntoNewService<B, Config>,
{ {
AndThenTransformNewService::new( AndThenTransform::new(transform.into_transform(), self, service.into_new_service())
transform.into_transform(),
self,
service.into_new_service(),
)
} }
/// Apply function to specified service and use it as a next service in /// Apply function to specified service and use it as a next service in

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Async, Future, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use super::{NewService, Service}; use super::{NewService, Service};
@ -146,7 +146,7 @@ where
type Future = MapNewServiceFuture<A, F, Res, Cfg>; type Future = MapNewServiceFuture<A, F, Res, Cfg>;
fn new_service(&self, cfg: &Cfg) -> Self::Future { fn new_service(&self, cfg: &Cfg) -> Self::Future {
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
} }
} }
@ -155,7 +155,7 @@ where
A: NewService<Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fut: A::Future, fut: <A::Future as IntoFuture>::Future,
f: Option<F>, f: Option<F>,
} }
@ -164,7 +164,7 @@ where
A: NewService<Cfg>, A: NewService<Cfg>,
F: FnMut(A::Response) -> Res, F: FnMut(A::Response) -> Res,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
MapNewServiceFuture { f: Some(f), fut } MapNewServiceFuture { f: Some(f), fut }
} }
} }

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Async, Future, Poll}; use futures::{Async, Future, IntoFuture, Poll};
use super::{NewService, Service}; use super::{NewService, Service};
@ -147,7 +147,7 @@ where
type Future = MapErrNewServiceFuture<A, F, E, C>; type Future = MapErrNewServiceFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone()) MapErrNewServiceFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
} }
} }
@ -156,7 +156,7 @@ where
A: NewService<C>, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fut: A::Future, fut: <A::Future as IntoFuture>::Future,
f: F, f: F,
} }
@ -165,7 +165,7 @@ where
A: NewService<C>, A: NewService<C>,
F: Fn(A::Error) -> E, F: Fn(A::Error) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
MapErrNewServiceFuture { f, fut } MapErrNewServiceFuture { f, fut }
} }
} }

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Future, Poll}; use futures::{Future, IntoFuture, Poll};
use super::NewService; use super::NewService;
@ -54,7 +54,7 @@ where
type Future = MapInitErrFuture<A, F, E, C>; type Future = MapInitErrFuture<A, F, E, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) MapInitErrFuture::new(self.a.new_service(cfg).into_future(), self.f.clone())
} }
} }
@ -64,7 +64,7 @@ where
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
f: F, f: F,
fut: A::Future, fut: <A::Future as IntoFuture>::Future,
} }
impl<A, F, E, C> MapInitErrFuture<A, F, E, C> impl<A, F, E, C> MapInitErrFuture<A, F, E, C>
@ -72,7 +72,7 @@ where
A: NewService<C>, A: NewService<C>,
F: Fn(A::InitError) -> E, F: Fn(A::InitError) -> E,
{ {
fn new(fut: A::Future, f: F) -> Self { fn new(fut: <A::Future as IntoFuture>::Future, f: F) -> Self {
MapInitErrFuture { f, fut } MapInitErrFuture { f, fut }
} }
} }

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, IntoFuture, Poll};
use super::{IntoNewService, NewService, Service}; use super::{IntoNewService, NewService, Service};
use crate::cell::Cell; use crate::cell::Cell;
@ -157,7 +157,10 @@ where
type Future = ThenNewServiceFuture<A, B, C>; type Future = ThenNewServiceFuture<A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future { fn new_service(&self, cfg: &C) -> Self::Future {
ThenNewServiceFuture::new(self.a.new_service(cfg), self.b.new_service(cfg)) ThenNewServiceFuture::new(
self.a.new_service(cfg).into_future(),
self.b.new_service(cfg).into_future(),
)
} }
} }
@ -185,8 +188,8 @@ where
InitError = A::InitError, InitError = A::InitError,
>, >,
{ {
fut_b: B::Future, fut_b: <B::Future as IntoFuture>::Future,
fut_a: A::Future, fut_a: <A::Future as IntoFuture>::Future,
a: Option<A::Service>, a: Option<A::Service>,
b: Option<B::Service>, b: Option<B::Service>,
} }
@ -201,7 +204,10 @@ where
InitError = A::InitError, InitError = A::InitError,
>, >,
{ {
fn new(fut_a: A::Future, fut_b: B::Future) -> Self { fn new(
fut_a: <A::Future as IntoFuture>::Future,
fut_b: <B::Future as IntoFuture>::Future,
) -> Self {
ThenNewServiceFuture { ThenNewServiceFuture {
fut_a, fut_a,
fut_b, fut_b,

View File

@ -1,7 +1,7 @@
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use futures::Future; use futures::IntoFuture;
use crate::transform_map_init_err::TransformMapInitErr; use crate::transform_map_init_err::TransformMapInitErr;
use crate::Service; use crate::Service;
@ -31,7 +31,7 @@ pub trait Transform<S> {
type InitError; type InitError;
/// The future response value. /// The future response value.
type Future: Future<Item = Self::Transform, Error = Self::InitError>; type Future: IntoFuture<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously. /// Create and return a new service value asynchronously.
fn new_transform(&self, service: S) -> Self::Future; fn new_transform(&self, service: S) -> Self::Future;

View File

@ -1,6 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use futures::{Future, Poll}; use futures::{Future, IntoFuture, Poll};
use super::Transform; use super::Transform;
@ -57,7 +57,10 @@ where
type Future = TransformMapInitErrFuture<T, S, F, E>; type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, service: S) -> Self::Future { fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone()) TransformMapInitErrFuture::new(
self.t.new_transform(service).into_future(),
self.f.clone(),
)
} }
} }
@ -66,7 +69,7 @@ where
T: Transform<S>, T: Transform<S>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fut: T::Future, fut: <T::Future as IntoFuture>::Future,
f: F, f: F,
} }
@ -75,7 +78,7 @@ where
T: Transform<S>, T: Transform<S>,
F: Fn(T::InitError) -> E, F: Fn(T::InitError) -> E,
{ {
fn new(fut: T::Future, f: F) -> Self { fn new(fut: <T::Future as IntoFuture>::Future, f: F) -> Self {
TransformMapInitErrFuture { f, fut } TransformMapInitErrFuture { f, fut }
} }
} }