1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-18 06:11:50 +01:00

simplify transform trait

This commit is contained in:
Nikolay Kim 2019-03-04 19:38:11 -08:00
parent 9bc492cf6c
commit 8c48bf4de7
15 changed files with 288 additions and 825 deletions

View File

@ -1,5 +1,12 @@
# Changes
## [0.3.1] - 2019-03-04
### Changed
* Simplify Transform trait
## [0.3.0] - 2019-03-02
## Added

View File

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

View File

@ -1,141 +1,24 @@
use std::rc::Rc;
use futures::{Async, Future, Poll};
use super::{NewService, NewTransform, Service, Transform};
use crate::cell::Cell;
/// `Apply` service combinator
pub struct AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
a: A,
b: Cell<B>,
t: Cell<T>,
}
impl<T, A, B> AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
/// Create new `Apply` combinator
pub fn new(t: T, a: A, b: B) -> Self {
Self {
a,
b: Cell::new(b),
t: Cell::new(t),
}
}
}
impl<T, A, B> Clone for AndThenTransform<T, A, B>
where
A: Service + Clone,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
fn clone(&self) -> Self {
AndThenTransform {
a: self.a.clone(),
b: self.b.clone(),
t: self.t.clone(),
}
}
}
impl<T, A, B> Service for AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
type Request = A::Request;
type Response = T::Response;
type Error = T::Error;
type Future = AndThenTransformFuture<T, A, B>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
let notready = Async::NotReady == self.a.poll_ready()?;
let notready = Async::NotReady == self.b.get_mut().poll_ready()? || notready;
let notready = Async::NotReady == self.t.get_mut().poll_ready()? || notready;
if notready {
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}
fn call(&mut self, req: A::Request) -> Self::Future {
AndThenTransformFuture {
b: self.b.clone(),
t: self.t.clone(),
fut_t: None,
fut_a: Some(self.a.call(req)),
}
}
}
pub struct AndThenTransformFuture<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
b: Cell<B>,
t: Cell<T>,
fut_a: Option<A::Future>,
fut_t: Option<T::Future>,
}
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
type Item = T::Response;
type Error = T::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut fut) = self.fut_t {
return fut.poll();
}
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
Ok(Async::Ready(resp)) => {
let _ = self.fut_a.take();
self.fut_t = Some(self.t.get_mut().call(resp, self.b.get_mut()));
self.poll()
}
Ok(Async::NotReady) => Ok(Async::NotReady),
Err(err) => Err(err.into()),
}
}
}
use crate::and_then::AndThen;
use crate::from_err::FromErr;
use crate::{NewService, Transform};
/// `Apply` new service combinator
pub struct AndThenTransformNewService<T, A, B, C> {
a: A,
b: B,
t: T,
t: Rc<T>,
_t: std::marker::PhantomData<C>,
}
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
/// Create new `ApplyNewService` new service instance
@ -143,7 +26,7 @@ where
Self {
a,
b,
t,
t: Rc::new(t),
_t: std::marker::PhantomData,
}
}
@ -153,7 +36,6 @@ impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C>
where
A: Clone,
B: Clone,
T: Clone,
{
fn clone(&self) -> Self {
Self {
@ -168,8 +50,8 @@ where
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
type Request = A::Request;
@ -177,17 +59,17 @@ where
type Error = T::Error;
type InitError = T::InitError;
type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
type Service = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Future = AndThenTransformNewServiceFuture<T, A, B, C>;
fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformNewServiceFuture {
a: None,
b: None,
t: None,
t_cell: self.t.clone(),
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg),
fut_t: self.t.new_transform(cfg),
fut_t: None,
}
}
}
@ -195,32 +77,32 @@ where
pub struct AndThenTransformNewServiceFuture<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
fut_b: B::Future,
fut_a: A::Future,
fut_t: T::Future,
fut_b: B::Future,
fut_t: Option<T::Future>,
a: Option<A::Service>,
b: Option<B::Service>,
t: Option<T::Transform>,
t_cell: Rc<T>,
}
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
where
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
B: NewService<C, InitError = A::InitError>,
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
{
type Item = AndThenTransform<T::Transform, A::Service, B::Service>;
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.t.is_none() {
if let Async::Ready(transform) = self.fut_t.poll()? {
self.t = Some(transform);
if self.fut_t.is_none() {
if let Async::Ready(service) = self.fut_b.poll()? {
self.fut_t = Some(self.t_cell.new_transform(service));
}
}
@ -230,18 +112,17 @@ where
}
}
if self.b.is_none() {
if let Async::Ready(service) = self.fut_b.poll()? {
self.b = Some(service);
if let Some(ref mut fut) = self.fut_t {
if let Async::Ready(transform) = fut.poll()? {
self.t = Some(transform);
}
}
if self.a.is_some() && self.b.is_some() && self.t.is_some() {
Ok(Async::Ready(AndThenTransform {
a: self.a.take().unwrap(),
t: Cell::new(self.t.take().unwrap()),
b: Cell::new(self.b.take().unwrap()),
}))
if self.a.is_some() && self.t.is_some() {
Ok(Async::Ready(AndThen::new(
FromErr::new(self.a.take().unwrap()),
self.t.take().unwrap(),
)))
} else {
Ok(Async::NotReady)
}
@ -276,10 +157,11 @@ mod tests {
fn test_apply() {
let blank = |req| Ok(req);
let mut srv = blank.into_service().apply(
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
Srv,
);
let mut srv = blank
.into_service()
.apply_fn(Srv, |req: &'static str, srv: &mut Srv| {
srv.call(()).map(move |res| (req, res))
});
assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll();
assert!(res.is_ok());

View File

@ -1,218 +1,172 @@
use futures::{try_ready, Async, Future, IntoFuture, Poll};
use std::marker::PhantomData;
use super::{FnNewTransform, FnTransform};
use super::{
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform,
Service, Transform,
};
use futures::{Async, Future, IntoFuture, Poll};
use super::{IntoNewService, IntoService, NewService, Service};
/// `Apply` service combinator
pub struct Apply<T, S>
pub struct Apply<T, F, In, Out>
where
T: Transform<S>,
T::Error: From<S::Error>,
S: Service,
T: Service,
{
transform: T,
service: S,
service: T,
f: F,
r: PhantomData<(In, Out)>,
}
impl<T, S> Apply<T, S>
impl<T, F, In, Out> Apply<T, F, In, Out>
where
T: Transform<S>,
T::Error: From<S::Error>,
S: Service,
{
/// Create new `Apply` combinator
pub fn new<T1: IntoTransform<T, S>, S1: IntoService<S>>(
transform: T1,
service: S1,
) -> Self {
Self {
transform: transform.into_transform(),
service: service.into_service(),
}
}
}
impl<F, S, Req, Out> Apply<FnTransform<F, S, Req, Out>, S>
where
F: FnMut(Req, &mut S) -> Out,
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<S::Error>,
S: Service,
Out::Error: From<T::Error>,
{
/// Create new `Apply` combinator
pub fn new_fn<S1: IntoService<S>>(service: S1, transform: F) -> Self {
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
Self {
service: service.into_service(),
transform: transform.into_transform(),
f,
r: PhantomData,
}
}
}
impl<T, S> Clone for Apply<T, S>
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
where
S: Service + Clone,
T::Error: From<S::Error>,
T: Transform<S> + Clone,
T: Service + Clone,
F: Clone,
{
fn clone(&self) -> Self {
Apply {
service: self.service.clone(),
transform: self.transform.clone(),
f: self.f.clone(),
r: PhantomData,
}
}
}
impl<T, S> Service for Apply<T, S>
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
where
T: Transform<S>,
T::Error: From<S::Error>,
S: Service,
T: Service,
F: FnMut(In, &mut T) -> Out,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Future = Out::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
try_ready!(self.service.poll_ready());
self.transform.poll_ready()
self.service.poll_ready().map_err(|e| e.into())
}
fn call(&mut self, req: Self::Request) -> Self::Future {
self.transform.call(req, &mut self.service).into_future()
fn call(&mut self, req: In) -> Self::Future {
(self.f)(req, &mut self.service).into_future()
}
}
/// `ApplyNewService` new service combinator
pub struct ApplyNewService<T, S, C>
pub struct ApplyNewService<T, F, In, Out, Cfg>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
T: NewService<Cfg>,
{
transform: T,
service: S,
_t: std::marker::PhantomData<C>,
service: T,
f: F,
r: PhantomData<(In, Out, Cfg)>,
}
impl<T, S, C> ApplyNewService<T, S, C>
impl<T, F, In, Out, Cfg> ApplyNewService<T, F, In, Out, Cfg>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
/// Create new `ApplyNewService` new service instance
pub fn new<T1: IntoNewTransform<T, S::Service, C>, S1: IntoNewService<S, C>>(
transform: T1,
service: S1,
) -> Self {
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
Self {
transform: transform.into_new_transform(),
f,
service: service.into_new_service(),
_t: std::marker::PhantomData,
r: PhantomData,
}
}
}
impl<F, S, In, Out, Cfg>
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError, Cfg>, S, Cfg>
impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
where
F: FnMut(In, &mut S::Service) -> Out + Clone,
T: NewService<Cfg> + Clone,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
S: NewService<Cfg>,
{
/// Create new `Apply` combinator factory
pub fn new_fn<S1: IntoNewService<S, Cfg>>(service: S1, transform: F) -> Self {
Self {
service: service.into_new_service(),
transform: FnNewTransform::new(transform),
_t: std::marker::PhantomData,
}
}
}
impl<T, S, C> Clone for ApplyNewService<T, S, C>
where
T: NewTransform<S::Service, C, InitError = S::InitError> + Clone,
T::Error: From<S::Error>,
S: NewService<C> + Clone,
{
fn clone(&self) -> Self {
Self {
service: self.service.clone(),
transform: self.transform.clone(),
_t: std::marker::PhantomData,
f: self.f.clone(),
r: PhantomData,
}
}
}
impl<T, S, C> NewService<C> for ApplyNewService<T, S, C>
impl<T, F, In, Out, Cfg> NewService<Cfg> for ApplyNewService<T, F, In, Out, Cfg>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Service = Apply<T::Transform, S::Service>;
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Service = Apply<T::Service, F, In, Out>;
type InitError = T::InitError;
type Future = ApplyNewServiceFuture<T, S, C>;
type Future = ApplyNewServiceFuture<T, F, In, Out, Cfg>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: &Cfg) -> Self::Future {
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
}
}
pub struct ApplyNewServiceFuture<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
}
impl<T, F, In, Out, Cfg> ApplyNewServiceFuture<T, F, In, Out, Cfg>
where
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
{
fn new(fut: T::Future, f: F) -> Self {
ApplyNewServiceFuture {
fut_t: self.transform.new_transform(cfg),
fut_s: self.service.new_service(cfg),
service: None,
transform: None,
f: Some(f),
fut,
r: PhantomData,
}
}
}
pub struct ApplyNewServiceFuture<T, S, C>
impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
T: NewService<Cfg>,
F: FnMut(In, &mut T::Service) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<T::Error>,
{
fut_s: S::Future,
fut_t: T::Future,
service: Option<S::Service>,
transform: Option<T::Transform>,
}
impl<T, S, C> Future for ApplyNewServiceFuture<T, S, C>
where
T: NewTransform<S::Service, C, InitError = S::InitError>,
T::Error: From<S::Error>,
S: NewService<C>,
{
type Item = Apply<T::Transform, S::Service>;
type Item = Apply<T::Service, F, In, Out>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if self.transform.is_none() {
if let Async::Ready(transform) = self.fut_t.poll()? {
self.transform = Some(transform);
}
}
if self.service.is_none() {
if let Async::Ready(service) = self.fut_s.poll()? {
self.service = Some(service);
}
}
if self.transform.is_some() && self.service.is_some() {
Ok(Async::Ready(Apply {
service: self.service.take().unwrap(),
transform: self.transform.take().unwrap(),
}))
if let Async::Ready(service) = self.fut.poll()? {
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
} else {
Ok(Async::NotReady)
}
@ -225,7 +179,7 @@ mod tests {
use futures::{Async, Future, Poll};
use super::*;
use crate::{NewService, Service};
use crate::{IntoService, NewService, Service, ServiceExt};
#[derive(Clone)]
struct Srv;
@ -245,10 +199,14 @@ mod tests {
}
#[test]
fn test_apply() {
let mut srv = Apply::new_fn(Srv, |req: &'static str, srv| {
srv.call(()).map(move |res| (req, res))
});
fn test_call() {
let blank = |req| Ok(req);
let mut srv = blank
.into_service()
.apply_fn(Srv, |req: &'static str, srv| {
srv.call(()).map(move |res| (req, res))
});
assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll();
assert!(res.is_ok());
@ -258,22 +216,6 @@ mod tests {
#[test]
fn test_new_service() {
let new_srv = ApplyNewService::new(
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|| Ok::<_, ()>(Srv),
);
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
assert!(srv.poll_ready().is_ok());
let res = srv.call("srv").poll();
assert!(res.is_ok());
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
} else {
panic!()
}
}
#[test]
fn test_new_service_fn() {
let new_srv = ApplyNewService::new_fn(
|| Ok::<_, ()>(Srv),
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
);

View File

@ -96,11 +96,7 @@ where
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
fn new_service(&self, cfg: &C) -> Self::Future {
Box::new(
self.service
.new_service(cfg)
.map(|service| ServiceWrapper::boxed(service)),
)
Box::new(self.service.new_service(cfg).map(ServiceWrapper::boxed))
}
}

View File

@ -1,122 +1,64 @@
use std::marker::PhantomData;
use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll};
use futures::IntoFuture;
use crate::{IntoNewTransform, IntoTransform, NewTransform, Transform};
use crate::{Apply, IntoTransform, Service, Transform};
pub struct FnTransform<F, S, Req, Res>
pub struct FnTransform<F, S, In, Out, Err>
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(S, Req, Res)>,
_t: PhantomData<(S, In, Out, Err)>,
}
impl<F, S, Req, Res> FnTransform<F, S, Req, Res>
impl<F, S, In, Out, Err> FnTransform<F, S, In, Out, Err>
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
pub fn new(f: F) -> Self {
FnTransform { f, _t: PhantomData }
}
}
impl<F, S, Req, Res> Clone for FnTransform<F, S, Req, Res>
impl<F, S, In, Out, Err> Transform<S> for FnTransform<F, S, In, Out, Err>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
S: Service,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
fn clone(&self) -> Self {
FnTransform {
f: self.f.clone(),
_t: PhantomData,
}
type Request = In;
type Response = Out::Item;
type Error = Out::Error;
type Transform = Apply<S, F, In, Out>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, service: S) -> Self::Future {
ok(Apply::new(service, self.f.clone()))
}
}
impl<F, S, Req, Res> Transform<S> for FnTransform<F, S, Req, Res>
impl<F, S, In, Out, Err> IntoTransform<FnTransform<F, S, In, Out, Err>, S> for F
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
S: Service,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
Out::Error: From<S::Error>,
{
type Request = Req;
type Response = Res::Item;
type Error = Res::Error;
type Future = Res::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
}
fn call(&mut self, request: Req, service: &mut S) -> Self::Future {
(self.f)(request, service).into_future()
}
}
impl<F, S, Req, Res> IntoTransform<FnTransform<F, S, Req, Res>, S> for F
where
F: FnMut(Req, &mut S) -> Res,
Res: IntoFuture,
{
fn into_transform(self) -> FnTransform<F, S, Req, Res> {
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
FnTransform::new(self)
}
}
pub struct FnNewTransform<F, S, Req, Out, Err, Cfg>
impl<F, S, In, Out, Err> Clone for FnTransform<F, S, In, Out, Err>
where
F: FnMut(Req, &mut S) -> Out + Clone,
F: FnMut(In, &mut S) -> Out + Clone,
Out: IntoFuture,
{
f: F,
_t: PhantomData<(S, Req, Out, Err, Cfg)>,
}
impl<F, S, Req, Res, Err, Cfg> FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
pub fn new(f: F) -> Self {
FnNewTransform { f, _t: PhantomData }
}
}
impl<F, S, Req, Res, Err, Cfg> NewTransform<S, Cfg> for FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
type Request = Req;
type Response = Res::Item;
type Error = Res::Error;
type Transform = FnTransform<F, S, Req, Res>;
type InitError = Err;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &Cfg) -> Self::Future {
ok(FnTransform::new(self.f.clone()))
}
}
impl<F, S, Req, Res, Err, Cfg>
IntoNewTransform<FnNewTransform<F, S, Req, Res, Err, Cfg>, S, Cfg> for F
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err, Cfg> {
FnNewTransform::new(self)
}
}
impl<F, S, Req, Res, Err, Cfg> Clone for FnNewTransform<F, S, Req, Res, Err, Cfg>
where
F: FnMut(Req, &mut S) -> Res + Clone,
Res: IntoFuture,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())

View File

@ -20,21 +20,20 @@ mod map_err;
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};
use self::and_then_apply::AndThenTransformNewService;
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
pub use self::apply::{Apply, ApplyNewService};
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
pub use self::fn_transform::{FnNewTransform, FnTransform};
pub use self::fn_transform::FnTransform;
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::{IntoNewTransform, IntoTransform, NewTransform, Transform};
pub use self::transform::{IntoTransform, Transform};
/// An asynchronous function from `Request` to a `Response`.
pub trait Service {
@ -76,20 +75,6 @@ pub trait Service {
/// An extension trait for `Service`s that provides a variety of convenient
/// adapters
pub trait ServiceExt: Service {
/// Apply tranformation to specified service and use it as a next service in
/// chain.
fn apply<T, T1, B, B1>(self, transform: T1, service: B1) -> AndThenTransform<T, Self, B>
where
Self: Sized,
T: Transform<B, Request = Self::Response>,
T::Error: From<Self::Error>,
T1: IntoTransform<T, B>,
B: Service<Error = Self::Error>,
B1: IntoService<B>,
{
AndThenTransform::new(transform.into_transform(), self, service.into_service())
}
/// Apply function to specified service and use it as a next service in
/// chain.
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
@ -228,14 +213,14 @@ pub trait NewService<Config = ()> {
) -> AndThenTransformNewService<T, Self, B, Config>
where
Self: Sized,
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T: Transform<B::Service, Request = Self::Response, InitError = Self::InitError>,
T::Error: From<Self::Error>,
T1: IntoNewTransform<T, B::Service>,
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
T1: IntoTransform<T, B::Service>,
B: NewService<Config, InitError = Self::InitError>,
B1: IntoNewService<B, Config>,
{
AndThenTransformNewService::new(
transform.into_new_transform(),
transform.into_transform(),
self,
service.into_new_service(),
)

View File

@ -1,56 +1,16 @@
use std::rc::Rc;
use std::sync::Arc;
use futures::{Future, Poll};
use futures::Future;
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.
pub trait Transform<Service> {
/// Requests handled by the service.
type Request;
/// Responses given by the service.
type Response;
/// Errors produced by the service.
type Error;
/// The future response value.
type Future: Future<Item = Self::Response, Error = Self::Error>;
/// Returns `Ready` when the service is able to process requests.
///
/// This method is similar to `Service::poll_ready` method.
fn poll_ready(&mut self) -> Poll<(), Self::Error>;
/// Process the request and apply it to provided service,
/// return the response asynchronously.
fn call(&mut self, request: Self::Request, service: &mut Service) -> Self::Future;
/// Map this transform's error to a different error, returning a new transform.
///
/// This function is similar to the `Result::map_err` where it will change
/// the error type of the underlying transform. This is useful for example to
/// ensure that services and transforms have the same error type.
///
/// Note that this function consumes the receiving transform and returns a
/// wrapped version of it.
fn map_err<F, E>(self, f: F) -> TransformMapErr<Self, Service, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
{
TransformMapErr::new(self, f)
}
}
/// `Transform` service factory
/// `Transform` service factory.
///
/// Transform factory creates service that wraps other services.
/// `Config` is a service factory configuration type.
pub trait NewTransform<Service, Config = ()> {
pub trait Transform<S> {
/// Requests handled by the service.
type Request;
@ -61,8 +21,7 @@ pub trait NewTransform<Service, Config = ()> {
type Error;
/// The `TransformService` value created by this factory
type Transform: Transform<
Service,
type Transform: Service<
Request = Self::Request,
Response = Self::Response,
Error = Self::Error,
@ -75,21 +34,11 @@ pub trait NewTransform<Service, Config = ()> {
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
/// Create and return a new service value asynchronously.
fn new_transform(&self, cfg: &Config) -> Self::Future;
/// Map this transforms's output to a different type, returning a new transform
/// of the resulting type.
fn map_err<F, E>(self, f: F) -> TransformMapErrNewTransform<Self, Service, Config, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E,
{
TransformMapErrNewTransform::new(self, f)
}
fn new_transform(&self, service: S) -> Self::Future;
/// 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>
fn map_init_err<F, E>(self, f: F) -> TransformMapInitErr<Self, S, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E,
@ -98,77 +47,39 @@ pub trait NewTransform<Service, Config = ()> {
}
}
impl<'a, T, S> Transform<S> for &'a mut T
impl<T, S> Transform<S> for Rc<T>
where
T: Transform<S> + 'a,
S: Service<Error = T::Error>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self) -> Poll<(), T::Error> {
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
(**self).call(request, service)
}
}
impl<T, S> Transform<S> for Box<T>
where
T: Transform<S> + ?Sized,
S: Service<Error = T::Error>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = T::Future;
fn poll_ready(&mut self) -> Poll<(), S::Error> {
(**self).poll_ready()
}
fn call(&mut self, request: Self::Request, service: &mut S) -> T::Future {
(**self).call(request, service)
}
}
impl<S, C, T> NewTransform<S, C> for Rc<T>
where
T: NewTransform<S, C>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError;
type Transform = T::Transform;
type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future {
self.as_ref().new_transform(cfg)
fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(service)
}
}
impl<S, C, T> NewTransform<S, C> for Arc<T>
impl<T, S> Transform<S> for Arc<T>
where
T: NewTransform<S, C>,
T: Transform<S>,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Transform = T::Transform;
type InitError = T::InitError;
type Transform = T::Transform;
type Future = T::Future;
fn new_transform(&self, cfg: &C) -> T::Future {
self.as_ref().new_transform(cfg)
fn new_transform(&self, service: S) -> T::Future {
self.as_ref().new_transform(service)
}
}
/// Trait for types that can be converted to a `TransformService`
/// Trait for types that can be converted to a *transform service*
pub trait IntoTransform<T, S>
where
T: Transform<S>,
@ -177,15 +88,6 @@ where
fn into_transform(self) -> T;
}
/// Trait for types that can be converted to a TransfromNewService
pub trait IntoNewTransform<T, S, C = ()>
where
T: NewTransform<S, C>,
{
/// Convert to an `TranformNewService`
fn into_new_transform(self) -> T;
}
impl<T, S> IntoTransform<T, S> for T
where
T: Transform<S>,
@ -194,12 +96,3 @@ where
self
}
}
impl<T, S, C> IntoNewTransform<T, S, C> for T
where
T: NewTransform<S, C>,
{
fn into_new_transform(self) -> T {
self
}
}

View File

@ -1,188 +0,0 @@
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<T, S, F, E> {
transform: T,
f: F,
_t: PhantomData<(S, E)>,
}
impl<T, S, F, E> TransformMapErr<T, S, F, E> {
/// Create new `MapErr` combinator
pub fn new(transform: T, f: F) -> Self
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
Self {
transform,
f,
_t: PhantomData,
}
}
}
impl<T, S, F, E> Clone for TransformMapErr<T, S, F, E>
where
T: Clone,
F: Clone,
{
fn clone(&self) -> Self {
TransformMapErr {
transform: self.transform.clone(),
f: self.f.clone(),
_t: PhantomData,
}
}
}
impl<T, S, F, E> Transform<S> for TransformMapErr<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = E;
type Future = TransformMapErrFuture<T, S, F, E>;
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<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
f: F,
fut: T::Future,
}
impl<T, S, F, E> TransformMapErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapErrFuture { f, fut }
}
}
impl<T, S, F, E> Future for TransformMapErrFuture<T, S, F, E>
where
T: Transform<S>,
F: Fn(T::Error) -> E,
{
type Item = T::Response;
type Error = E;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
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, S, C, F, E> {
t: T,
f: F,
e: PhantomData<(S, C, E)>,
}
impl<T, S, C, F, E> TransformMapErrNewTransform<T, S, C, F, E> {
/// Create new `MapErr` new service instance
pub fn new(t: T, f: F) -> Self
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
Self {
t,
f,
e: PhantomData,
}
}
}
impl<T, S, C, F, E> Clone for TransformMapErrNewTransform<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 TransformMapErrNewTransform<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone,
{
type Request = T::Request;
type Response = T::Response;
type Error = E;
type Transform = TransformMapErr<T::Transform, S, F, E>;
type InitError = T::InitError;
type Future = TransformMapErrNewTransformFuture<T, S, C, F, E>;
fn new_transform(&self, cfg: &C) -> Self::Future {
TransformMapErrNewTransformFuture::new(self.t.new_transform(cfg), self.f.clone())
}
}
pub struct TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, C, F, E> TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
TransformMapErrNewTransformFuture { f, fut }
}
}
impl<T, S, C, F, E> Future for TransformMapErrNewTransformFuture<T, S, C, F, E>
where
T: NewTransform<S, C>,
F: Fn(T::Error) -> E + Clone,
{
type Item = TransformMapErr<T::Transform, S, F, E>;
type Error = T::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Async::Ready(tr) = self.fut.poll()? {
Ok(Async::Ready(TransformMapErr::new(tr, self.f.clone())))
} else {
Ok(Async::NotReady)
}
}
}

View File

@ -2,23 +2,23 @@ use std::marker::PhantomData;
use futures::{Future, Poll};
use super::NewTransform;
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, C, F, E> {
pub struct TransformMapInitErr<T, S, F, E> {
t: T,
f: F,
e: PhantomData<(S, C, E)>,
e: PhantomData<(S, E)>,
}
impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, 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: NewTransform<S, C>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
Self {
@ -29,7 +29,7 @@ impl<T, S, C, F, E> TransformMapInitErr<T, S, C, F, E> {
}
}
impl<T, S, C, F, E> Clone for TransformMapInitErr<T, S, C, F, E>
impl<T, S, F, E> Clone for TransformMapInitErr<T, S, F, E>
where
T: Clone,
F: Clone,
@ -43,9 +43,9 @@ where
}
}
impl<T, S, C, F, E> NewTransform<S, C> for TransformMapInitErr<T, S, C, F, E>
impl<T, S, F, E> Transform<S> for TransformMapInitErr<T, S, F, E>
where
T: NewTransform<S, C>,
T: Transform<S>,
F: Fn(T::InitError) -> E + Clone,
{
type Request = T::Request;
@ -54,25 +54,25 @@ where
type Transform = T::Transform;
type InitError = E;
type Future = TransformMapInitErrFuture<T, S, C, F, E>;
type Future = TransformMapInitErrFuture<T, S, F, E>;
fn new_transform(&self, cfg: &C) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(cfg), self.f.clone())
fn new_transform(&self, service: S) -> Self::Future {
TransformMapInitErrFuture::new(self.t.new_transform(service), self.f.clone())
}
}
pub struct TransformMapInitErrFuture<T, S, C, F, E>
pub struct TransformMapInitErrFuture<T, S, F, E>
where
T: NewTransform<S, C>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fut: T::Future,
f: F,
}
impl<T, S, C, F, E> TransformMapInitErrFuture<T, S, C, F, E>
impl<T, S, F, E> TransformMapInitErrFuture<T, S, F, E>
where
T: NewTransform<S, C>,
T: Transform<S>,
F: Fn(T::InitError) -> E,
{
fn new(fut: T::Future, f: F) -> Self {
@ -80,15 +80,15 @@ where
}
}
impl<T, S, C, F, E> Future for TransformMapInitErrFuture<T, S, C, F, E>
impl<T, S, F, E> Future for TransformMapInitErrFuture<T, S, F, E>
where
T: NewTransform<S, C>,
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(|e| (self.f)(e))
self.fut.poll().map_err(&self.f)
}
}

View File

@ -1,5 +1,12 @@
# Changes
## [0.3.1] - 2019-03-04
### Changed
* Use new type of transform trait
## [0.3.0] - 2019-03-02
### Changed

View File

@ -1,6 +1,6 @@
[package]
name = "actix-utils"
version = "0.3.0"
version = "0.3.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actix utils - various actix net related services"
keywords = ["network", "framework", "async", "futures"]
@ -18,7 +18,8 @@ name = "actix_utils"
path = "src/lib.rs"
[dependencies]
actix-service = "0.3.0"
#actix-service = "0.3.1"
actix-service = { path="../actix-service" }
actix-codec = "0.1.0"
bytes = "0.4"
futures = "0.1.24"

View File

@ -1,4 +1,4 @@
use actix_service::{NewTransform, Service, Transform, Void};
use actix_service::{Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
@ -24,32 +24,34 @@ impl Default for InFlight {
}
}
impl<T: Service, C> NewTransform<T, C> for InFlight {
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
impl<S: Service> Transform<S> for InFlight {
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type InitError = Void;
type Transform = InFlightService;
type Transform = InFlightService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future {
ok(InFlightService::new(self.max_inflight))
fn new_transform(&self, service: S) -> Self::Future {
ok(InFlightService::new(self.max_inflight, service))
}
}
pub struct InFlightService {
pub struct InFlightService<S> {
count: Counter,
service: S,
}
impl InFlightService {
pub fn new(max: usize) -> Self {
impl<S> InFlightService<S> {
pub fn new(max: usize, service: S) -> Self {
Self {
service,
count: Counter::new(max),
}
}
}
impl<T> Transform<T> for InFlightService
impl<T> Service for InFlightService<T>
where
T: Service,
{
@ -59,6 +61,8 @@ where
type Future = InFlightServiceResponse<T>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.poll_ready()?;
if !self.count.available() {
log::trace!("InFlight limit exceeded");
Ok(Async::NotReady)
@ -67,9 +71,9 @@ where
}
}
fn call(&mut self, req: T::Request, service: &mut T) -> Self::Future {
fn call(&mut self, req: T::Request) -> Self::Future {
InFlightServiceResponse {
fut: service.call(req),
fut: self.service.call(req),
_guard: self.count.get(),
}
}
@ -122,7 +126,8 @@ mod tests {
fn test_transform() {
let wait_time = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(lazy(|| {
let mut srv = Blank::new().apply(InFlightService::new(1), SleepService(wait_time));
let mut srv =
Blank::new().and_then(InFlightService::new(1, SleepService(wait_time)));
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
let mut res = srv.call(());

View File

@ -3,7 +3,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
use actix_service::{NewTransform, Service, Transform, Void};
use actix_service::{Service, Transform, Void};
use futures::future::{ok, FutureResult};
use futures::task::AtomicTask;
use futures::unsync::oneshot;
@ -63,13 +63,8 @@ where
Self { _t: PhantomData }
}
pub fn service() -> impl Transform<
S,
Request = S::Request,
Response = S::Response,
Error = InOrderError<S::Error>,
> {
InOrderService::new()
pub fn service(service: S) -> InOrderService<S> {
InOrderService::new(service)
}
}
@ -85,7 +80,7 @@ where
}
}
impl<S, C> NewTransform<S, C> for InOrder<S>
impl<S> Transform<S> for InOrder<S>
where
S: Service,
S::Response: 'static,
@ -99,12 +94,13 @@ where
type Transform = InOrderService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future {
ok(InOrderService::new())
fn new_transform(&self, service: S) -> Self::Future {
ok(InOrderService::new(service))
}
}
pub struct InOrderService<S: Service> {
service: S,
task: Rc<AtomicTask>,
acks: VecDeque<Record<S::Response, S::Error>>,
}
@ -116,27 +112,16 @@ where
S::Future: 'static,
S::Error: 'static,
{
pub fn new() -> Self {
pub fn new(service: S) -> Self {
Self {
service,
acks: VecDeque::new(),
task: Rc::new(AtomicTask::new()),
}
}
}
impl<S> Default for InOrderService<S>
where
S: Service,
S::Response: 'static,
S::Future: 'static,
S::Error: 'static,
{
fn default() -> Self {
Self::new()
}
}
impl<S> Transform<S> for InOrderService<S>
impl<S> Service for InOrderService<S>
where
S: Service,
S::Response: 'static,
@ -149,8 +134,12 @@ where
type Future = InOrderServiceResponse<S>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
// poll_ready could be called from different task
self.task.register();
// check nested service
self.service.poll_ready().map_err(InOrderError::Service)?;
// check acks
while !self.acks.is_empty() {
let rec = self.acks.front_mut().unwrap();
@ -167,13 +156,13 @@ where
Ok(Async::Ready(()))
}
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future {
fn call(&mut self, request: S::Request) -> Self::Future {
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
self.acks.push_back(Record { rx: rx1, tx: tx2 });
let task = self.task.clone();
tokio_current_thread::spawn(service.call(request).then(move |res| {
tokio_current_thread::spawn(self.service.call(request).then(move |res| {
task.notify();
let _ = tx1.send(res);
Ok(())
@ -257,7 +246,7 @@ mod tests {
let rx3 = rx3;
let tx_stop = tx_stop;
let _ = actix_rt::System::new("test").block_on(lazy(move || {
let mut srv = Blank::new().apply(InOrderService::new(), Srv);
let mut srv = Blank::new().and_then(InOrderService::new(Srv));
let res1 = srv.call(rx1);
let res2 = srv.call(rx2);

View File

@ -6,7 +6,7 @@ use std::fmt;
use std::marker::PhantomData;
use std::time::Duration;
use actix_service::{NewTransform, Service, Transform};
use actix_service::{Service, Transform};
use futures::future::{ok, FutureResult};
use futures::{Async, Future, Poll};
use tokio_timer::{clock, Delay};
@ -80,7 +80,7 @@ impl<E> Clone for Timeout<E> {
}
}
impl<S, C, E> NewTransform<S, C> for Timeout<E>
impl<S, E> Transform<S> for Timeout<E>
where
S: Service,
{
@ -88,11 +88,12 @@ where
type Response = S::Response;
type Error = TimeoutError<S::Error>;
type InitError = E;
type Transform = TimeoutService;
type Transform = TimeoutService<S>;
type Future = FutureResult<Self::Transform, Self::InitError>;
fn new_transform(&self, _: &C) -> Self::Future {
fn new_transform(&self, service: S) -> Self::Future {
ok(TimeoutService {
service,
timeout: self.timeout,
})
}
@ -100,17 +101,18 @@ where
/// Applies a timeout to requests.
#[derive(Debug, Clone)]
pub struct TimeoutService {
pub struct TimeoutService<S> {
service: S,
timeout: Duration,
}
impl TimeoutService {
pub fn new(timeout: Duration) -> Self {
TimeoutService { timeout }
impl<S> TimeoutService<S> {
pub fn new(timeout: Duration, service: S) -> Self {
TimeoutService { service, timeout }
}
}
impl<S> Transform<S> for TimeoutService
impl<S> Service for TimeoutService<S>
where
S: Service,
{
@ -120,12 +122,12 @@ where
type Future = TimeoutServiceResponse<S>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
Ok(Async::Ready(()))
self.service.poll_ready().map_err(TimeoutError::Service)
}
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future {
fn call(&mut self, request: S::Request) -> Self::Future {
TimeoutServiceResponse {
fut: service.call(request),
fut: self.service.call(request),
sleep: Delay::new(clock::now() + self.timeout),
}
}
@ -197,7 +199,7 @@ mod tests {
let res = actix_rt::System::new("test").block_on(lazy(|| {
let mut timeout = Blank::default()
.apply(TimeoutService::new(resolution), SleepService(wait_time));
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
timeout.call(())
}));
assert_eq!(res, Ok(()));
@ -210,7 +212,7 @@ mod tests {
let res = actix_rt::System::new("test").block_on(lazy(|| {
let mut timeout = Blank::default()
.apply(TimeoutService::new(resolution), SleepService(wait_time));
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
timeout.call(())
}));
assert_eq!(res, Err(TimeoutError::Timeout));