mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-30 16:34:36 +01:00
generalize apply combinator with transform trait
This commit is contained in:
parent
d45fb9521f
commit
bd977373bc
@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.1] - 2019-02-xx
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Generalize `.apply` combinator with Transform trait
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2019-02-01
|
## [0.2.0] - 2019-02-01
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
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"]
|
||||||
|
@ -1,125 +1,120 @@
|
|||||||
use std::marker::PhantomData;
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
use super::{NewService, NewTransform, Service, Transform};
|
||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
|
||||||
use crate::cell::Cell;
|
use crate::cell::Cell;
|
||||||
|
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct AndThenApply<A, B, F, Out>
|
pub struct AndThenTransform<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
a: A,
|
a: A,
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
f: Cell<F>,
|
t: Cell<T>,
|
||||||
r: PhantomData<(Out,)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> AndThenApply<A, B, F, Out>
|
impl<T, A, B> AndThenTransform<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self {
|
pub fn new(t: T, a: A, b: B) -> Self {
|
||||||
Self {
|
Self {
|
||||||
f: Cell::new(f),
|
a,
|
||||||
a: a.into_service(),
|
b: Cell::new(b),
|
||||||
b: Cell::new(b.into_service()),
|
t: Cell::new(t),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out>
|
impl<T, A, B> Clone for AndThenTransform<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service + Clone,
|
A: Service + Clone,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
AndThenApply {
|
AndThenTransform {
|
||||||
a: self.a.clone(),
|
a: self.a.clone(),
|
||||||
b: self.b.clone(),
|
b: self.b.clone(),
|
||||||
f: self.f.clone(),
|
t: self.t.clone(),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out>
|
impl<T, A, B> Service for AndThenTransform<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
type Response = Out::Item;
|
type Response = T::Response;
|
||||||
type Error = A::Error;
|
type Error = T::Error;
|
||||||
type Future = AndThenApplyFuture<A, B, F, Out>;
|
type Future = AndThenTransformFuture<T, A, B>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.a.poll_ready());
|
let notready = Async::NotReady == self.a.poll_ready()?;
|
||||||
self.b.get_mut().poll_ready().map_err(|e| e.into())
|
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 {
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
AndThenApplyFuture {
|
AndThenTransformFuture {
|
||||||
b: self.b.clone(),
|
b: self.b.clone(),
|
||||||
f: self.f.clone(),
|
t: self.t.clone(),
|
||||||
fut_b: None,
|
fut_t: None,
|
||||||
fut_a: Some(self.a.call(req)),
|
fut_a: Some(self.a.call(req)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenApplyFuture<A, B, F, Out>
|
pub struct AndThenTransformFuture<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
b: Cell<B>,
|
b: Cell<B>,
|
||||||
f: Cell<F>,
|
t: Cell<T>,
|
||||||
fut_a: Option<A::Future>,
|
fut_a: Option<A::Future>,
|
||||||
fut_b: Option<Out::Future>,
|
fut_t: Option<T::Future>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out>
|
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
|
||||||
where
|
where
|
||||||
A: Service,
|
A: Service,
|
||||||
B: Service<Error = A::Error>,
|
B: Service<Error = A::Error>,
|
||||||
F: FnMut(A::Response, &mut B) -> Out,
|
T: Transform<B, Request = A::Response>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
type Item = Out::Item;
|
type Item = T::Response;
|
||||||
type Error = A::Error;
|
type Error = T::Error;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
if let Some(ref mut fut) = self.fut_b {
|
if let Some(ref mut fut) = self.fut_t {
|
||||||
return fut.poll().map_err(|e| e.into());
|
return fut.poll().map_err(|e| e.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
||||||
Ok(Async::Ready(resp)) => {
|
Ok(Async::Ready(resp)) => {
|
||||||
let _ = self.fut_a.take();
|
let _ = self.fut_a.take();
|
||||||
self.fut_b =
|
self.fut_t = Some(self.t.get_mut().call(resp, self.b.get_mut()));
|
||||||
Some((&mut *self.f.get_mut())(resp, self.b.get_mut()).into_future());
|
|
||||||
self.poll()
|
self.poll()
|
||||||
}
|
}
|
||||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||||
@ -128,102 +123,100 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `Apply` new service combinator
|
||||||
pub struct AndThenApplyNewService<A, B, F, Out> {
|
pub struct AndThenTransformNewService<T, A, B> {
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
f: Cell<F>,
|
t: T,
|
||||||
r: PhantomData<(Out)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out>
|
impl<T, A, B> AndThenTransformNewService<T, A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: NewService,
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self {
|
pub fn new(t: T, a: A, b: B) -> Self {
|
||||||
Self {
|
Self { a, b, t }
|
||||||
f: Cell::new(f),
|
|
||||||
a: a.into_new_service(),
|
|
||||||
b: b.into_new_service(),
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out>
|
impl<T, A, B> Clone for AndThenTransformNewService<T, A, B>
|
||||||
where
|
where
|
||||||
A: Clone,
|
A: Clone,
|
||||||
B: Clone,
|
B: Clone,
|
||||||
|
T: Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
a: self.a.clone(),
|
a: self.a.clone(),
|
||||||
b: self.b.clone(),
|
b: self.b.clone(),
|
||||||
f: self.f.clone(),
|
t: self.t.clone(),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out>
|
impl<T, A, B> NewService for AndThenTransformNewService<T, A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: NewService,
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
type Response = Out::Item;
|
type Response = T::Response;
|
||||||
type Error = A::Error;
|
type Error = T::Error;
|
||||||
|
|
||||||
type InitError = A::InitError;
|
type InitError = T::InitError;
|
||||||
type Service = AndThenApply<A::Service, B::Service, F, Out>;
|
type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
|
||||||
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
|
type Future = AndThenTransformNewServiceFuture<T, A, B>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
AndThenApplyNewServiceFuture {
|
AndThenTransformNewServiceFuture {
|
||||||
a: None,
|
a: None,
|
||||||
b: None,
|
b: None,
|
||||||
f: self.f.clone(),
|
t: None,
|
||||||
fut_a: self.a.new_service(),
|
fut_a: self.a.new_service(),
|
||||||
fut_b: self.b.new_service(),
|
fut_b: self.b.new_service(),
|
||||||
|
fut_t: self.t.new_transform(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AndThenApplyNewServiceFuture<A, B, F, Out>
|
pub struct AndThenTransformNewServiceFuture<T, A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: NewService,
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
fut_b: B::Future,
|
fut_b: B::Future,
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
f: Cell<F>,
|
fut_t: T::Future,
|
||||||
a: Option<A::Service>,
|
a: Option<A::Service>,
|
||||||
b: Option<B::Service>,
|
b: Option<B::Service>,
|
||||||
|
t: Option<T::Transform>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out>
|
impl<T, A, B> Future for AndThenTransformNewServiceFuture<T, A, B>
|
||||||
where
|
where
|
||||||
A: NewService,
|
A: NewService,
|
||||||
B: NewService<Error = A::Error, InitError = A::InitError>,
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
F: FnMut(A::Response, &mut B::Service) -> Out,
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
Out: IntoFuture,
|
T::Error: From<A::Error>,
|
||||||
Out::Error: Into<A::Error>,
|
|
||||||
{
|
{
|
||||||
type Item = AndThenApply<A::Service, B::Service, F, Out>;
|
type Item = AndThenTransform<T::Transform, A::Service, B::Service>;
|
||||||
type Error = A::InitError;
|
type Error = T::InitError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
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.a.is_none() {
|
if self.a.is_none() {
|
||||||
if let Async::Ready(service) = self.fut_a.poll()? {
|
if let Async::Ready(service) = self.fut_a.poll()? {
|
||||||
self.a = Some(service);
|
self.a = Some(service);
|
||||||
@ -236,12 +229,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.a.is_some() && self.b.is_some() {
|
if self.a.is_some() && self.b.is_some() && self.t.is_some() {
|
||||||
Ok(Async::Ready(AndThenApply {
|
Ok(Async::Ready(AndThenTransform {
|
||||||
f: self.f.clone(),
|
|
||||||
a: self.a.take().unwrap(),
|
a: self.a.take().unwrap(),
|
||||||
|
t: Cell::new(self.t.take().unwrap()),
|
||||||
b: Cell::new(self.b.take().unwrap()),
|
b: Cell::new(self.b.take().unwrap()),
|
||||||
r: PhantomData,
|
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
@ -274,12 +266,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_call() {
|
fn test_apply() {
|
||||||
let blank = |req| Ok(req);
|
let blank = |req| Ok(req);
|
||||||
|
|
||||||
let mut srv = blank.into_service().apply(Srv, |req: &'static str, srv| {
|
let mut srv = blank.into_service().apply(
|
||||||
srv.call(()).map(move |res| (req, res))
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
||||||
});
|
Srv,
|
||||||
|
);
|
||||||
assert!(srv.poll_ready().is_ok());
|
assert!(srv.poll_ready().is_ok());
|
||||||
let res = srv.call("srv").poll();
|
let res = srv.call("srv").poll();
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
@ -291,8 +284,8 @@ mod tests {
|
|||||||
let blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
let blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
||||||
|
|
||||||
let new_srv = blank.into_new_service().apply(
|
let new_srv = blank.into_new_service().apply(
|
||||||
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
||||||
|| Ok(Srv),
|
|| Ok(Srv),
|
||||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|
||||||
);
|
);
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
||||||
assert!(srv.poll_ready().is_ok());
|
assert!(srv.poll_ready().is_ok());
|
||||||
|
302
actix-service/src/and_then_apply_fn.rs
Normal file
302
actix-service/src/and_then_apply_fn.rs
Normal file
@ -0,0 +1,302 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
||||||
|
|
||||||
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
|
use crate::cell::Cell;
|
||||||
|
|
||||||
|
/// `Apply` service combinator
|
||||||
|
pub struct AndThenApply<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
a: A,
|
||||||
|
b: Cell<B>,
|
||||||
|
f: Cell<F>,
|
||||||
|
r: PhantomData<(Out,)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> AndThenApply<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
/// Create new `Apply` combinator
|
||||||
|
pub fn new<A1: IntoService<A>, B1: IntoService<B>>(a: A1, b: B1, f: F) -> Self {
|
||||||
|
Self {
|
||||||
|
f: Cell::new(f),
|
||||||
|
a: a.into_service(),
|
||||||
|
b: Cell::new(b.into_service()),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> Clone for AndThenApply<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service + Clone,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
AndThenApply {
|
||||||
|
a: self.a.clone(),
|
||||||
|
b: self.b.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> Service for AndThenApply<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
type Request = A::Request;
|
||||||
|
type Response = Out::Item;
|
||||||
|
type Error = A::Error;
|
||||||
|
type Future = AndThenApplyFuture<A, B, F, Out>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
try_ready!(self.a.poll_ready());
|
||||||
|
self.b.get_mut().poll_ready().map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||||
|
AndThenApplyFuture {
|
||||||
|
b: self.b.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
fut_b: None,
|
||||||
|
fut_a: Some(self.a.call(req)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AndThenApplyFuture<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
b: Cell<B>,
|
||||||
|
f: Cell<F>,
|
||||||
|
fut_a: Option<A::Future>,
|
||||||
|
fut_b: Option<Out::Future>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> Future for AndThenApplyFuture<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Service,
|
||||||
|
B: Service<Error = A::Error>,
|
||||||
|
F: FnMut(A::Response, &mut B) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
type Item = Out::Item;
|
||||||
|
type Error = A::Error;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if let Some(ref mut fut) = self.fut_b {
|
||||||
|
return fut.poll().map_err(|e| e.into());
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
||||||
|
Ok(Async::Ready(resp)) => {
|
||||||
|
let _ = self.fut_a.take();
|
||||||
|
self.fut_b =
|
||||||
|
Some((&mut *self.f.get_mut())(resp, self.b.get_mut()).into_future());
|
||||||
|
self.poll()
|
||||||
|
}
|
||||||
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||||
|
Err(err) => Err(err.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `ApplyNewService` new service combinator
|
||||||
|
pub struct AndThenApplyNewService<A, B, F, Out> {
|
||||||
|
a: A,
|
||||||
|
b: B,
|
||||||
|
f: Cell<F>,
|
||||||
|
r: PhantomData<(Out)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> AndThenApplyNewService<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
/// Create new `ApplyNewService` new service instance
|
||||||
|
pub fn new<A1: IntoNewService<A>, B1: IntoNewService<B>>(a: A1, b: B1, f: F) -> Self {
|
||||||
|
Self {
|
||||||
|
f: Cell::new(f),
|
||||||
|
a: a.into_new_service(),
|
||||||
|
b: b.into_new_service(),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> Clone for AndThenApplyNewService<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: Clone,
|
||||||
|
B: Clone,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
a: self.a.clone(),
|
||||||
|
b: self.b.clone(),
|
||||||
|
f: self.f.clone(),
|
||||||
|
r: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> NewService for AndThenApplyNewService<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
type Request = A::Request;
|
||||||
|
type Response = Out::Item;
|
||||||
|
type Error = A::Error;
|
||||||
|
|
||||||
|
type InitError = A::InitError;
|
||||||
|
type Service = AndThenApply<A::Service, B::Service, F, Out>;
|
||||||
|
type Future = AndThenApplyNewServiceFuture<A, B, F, Out>;
|
||||||
|
|
||||||
|
fn new_service(&self) -> Self::Future {
|
||||||
|
AndThenApplyNewServiceFuture {
|
||||||
|
a: None,
|
||||||
|
b: None,
|
||||||
|
f: self.f.clone(),
|
||||||
|
fut_a: self.a.new_service(),
|
||||||
|
fut_b: self.b.new_service(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AndThenApplyNewServiceFuture<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
fut_b: B::Future,
|
||||||
|
fut_a: A::Future,
|
||||||
|
f: Cell<F>,
|
||||||
|
a: Option<A::Service>,
|
||||||
|
b: Option<B::Service>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, B, F, Out> Future for AndThenApplyNewServiceFuture<A, B, F, Out>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
||||||
|
F: FnMut(A::Response, &mut B::Service) -> Out,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: Into<A::Error>,
|
||||||
|
{
|
||||||
|
type Item = AndThenApply<A::Service, B::Service, F, Out>;
|
||||||
|
type Error = A::InitError;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if self.a.is_none() {
|
||||||
|
if let Async::Ready(service) = self.fut_a.poll()? {
|
||||||
|
self.a = Some(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.b.is_none() {
|
||||||
|
if let Async::Ready(service) = self.fut_b.poll()? {
|
||||||
|
self.b = Some(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.a.is_some() && self.b.is_some() {
|
||||||
|
Ok(Async::Ready(AndThenApply {
|
||||||
|
f: self.f.clone(),
|
||||||
|
a: self.a.take().unwrap(),
|
||||||
|
b: Cell::new(self.b.take().unwrap()),
|
||||||
|
r: PhantomData,
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use futures::future::{ok, FutureResult};
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
|
use crate::{Blank, BlankNewService, NewService, Service, ServiceExt};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Srv;
|
||||||
|
impl Service for Srv {
|
||||||
|
type Request = ();
|
||||||
|
type Response = ();
|
||||||
|
type Error = ();
|
||||||
|
type Future = FutureResult<(), ()>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
|
ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_call() {
|
||||||
|
let mut srv = Blank::new().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());
|
||||||
|
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_service() {
|
||||||
|
let new_srv = BlankNewService::new_unit().apply_fn(
|
||||||
|
|| Ok(Srv),
|
||||||
|
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
||||||
|
);
|
||||||
|
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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,172 +1,214 @@
|
|||||||
use std::marker::PhantomData;
|
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
||||||
|
|
||||||
use futures::{Async, Future, IntoFuture, Poll};
|
use super::{FnNewTransform, FnTransform};
|
||||||
|
use super::{
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform,
|
||||||
|
Service, Transform,
|
||||||
|
};
|
||||||
|
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, F, In, Out>
|
pub struct Apply<T, S>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Transform<S>,
|
||||||
|
T::Error: From<S::Error>,
|
||||||
|
S: Service,
|
||||||
{
|
{
|
||||||
service: T,
|
transform: T,
|
||||||
f: F,
|
service: S,
|
||||||
r: PhantomData<(In, Out)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Apply<T, F, In, Out>
|
impl<T, S> Apply<T, S>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Transform<S>,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
T::Error: From<S::Error>,
|
||||||
Out: IntoFuture,
|
S: Service,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// Create new `Apply` combinator
|
||||||
pub fn new<I: IntoService<T>>(service: I, f: F) -> Self {
|
pub fn new<T1: IntoTransform<T, S>, S1: IntoService<S>>(
|
||||||
|
transform: T1,
|
||||||
|
service: S1,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
transform: transform.into_transform(),
|
||||||
service: service.into_service(),
|
service: service.into_service(),
|
||||||
f,
|
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
|
impl<F, S, Req, Out> Apply<FnTransform<F, S, Req, Out>, S>
|
||||||
where
|
where
|
||||||
T: Service + Clone,
|
F: FnMut(Req, &mut S) -> Out,
|
||||||
F: Clone,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<S::Error>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
/// Create new `Apply` combinator
|
||||||
|
pub fn new_fn<S1: IntoService<S>>(transform: F, service: S1) -> Self {
|
||||||
|
Self {
|
||||||
|
transform: transform.into_transform(),
|
||||||
|
service: service.into_service(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> Clone for Apply<T, S>
|
||||||
|
where
|
||||||
|
S: Service + Clone,
|
||||||
|
T::Error: From<S::Error>,
|
||||||
|
T: Transform<S> + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Apply {
|
Apply {
|
||||||
service: self.service.clone(),
|
service: self.service.clone(),
|
||||||
f: self.f.clone(),
|
transform: self.transform.clone(),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
|
impl<T, S> Service for Apply<T, S>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Transform<S>,
|
||||||
F: FnMut(In, &mut T) -> Out,
|
T::Error: From<S::Error>,
|
||||||
Out: IntoFuture,
|
S: Service,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = In;
|
type Request = T::Request;
|
||||||
type Response = Out::Item;
|
type Response = T::Response;
|
||||||
type Error = Out::Error;
|
type Error = T::Error;
|
||||||
type Future = Out::Future;
|
type Future = T::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.service.poll_ready().map_err(|e| e.into())
|
try_ready!(self.service.poll_ready());
|
||||||
|
self.transform.poll_ready().map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: In) -> Self::Future {
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||||
(self.f)(req, &mut self.service).into_future()
|
self.transform.call(req, &mut self.service).into_future()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct ApplyNewService<T, F, In, Out>
|
pub struct ApplyNewService<T, S>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T::Error: From<S::Error>,
|
||||||
|
// T::InitError: From<S::InitError>,
|
||||||
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||||
|
S: NewService,
|
||||||
{
|
{
|
||||||
service: T,
|
transform: T,
|
||||||
f: F,
|
service: S,
|
||||||
r: PhantomData<(In, Out)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
|
impl<T, S> ApplyNewService<T, S>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
T::Error: From<S::Error>,
|
||||||
Out: IntoFuture,
|
S: NewService,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self {
|
pub fn new<T1: IntoNewTransform<T, S::Service>, S1: IntoNewService<S>>(
|
||||||
|
transform: T1,
|
||||||
|
service: S1,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
f,
|
transform: transform.into_new_transform(),
|
||||||
service: service.into_new_service(),
|
service: service.into_new_service(),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
|
impl<F, S, Req, Out> ApplyNewService<FnNewTransform<F, S::Service, Req, Out, S::InitError>, S>
|
||||||
where
|
where
|
||||||
T: NewService + Clone,
|
F: FnMut(Req, &mut S::Service) -> Out + Clone,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<S::Error>,
|
||||||
|
S: NewService,
|
||||||
|
{
|
||||||
|
/// Create new `Apply` combinator factory
|
||||||
|
pub fn new_fn<S1: IntoNewService<S>>(service: S1, transform: F) -> Self {
|
||||||
|
Self {
|
||||||
|
service: service.into_new_service(),
|
||||||
|
transform: FnNewTransform::new(transform),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> Clone for ApplyNewService<T, S>
|
||||||
|
where
|
||||||
|
T: NewTransform<S::Service, InitError = S::InitError> + Clone,
|
||||||
|
T::Error: From<S::Error>,
|
||||||
|
S: NewService + Clone,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: self.service.clone(),
|
service: self.service.clone(),
|
||||||
f: self.f.clone(),
|
transform: self.transform.clone(),
|
||||||
r: PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out>
|
impl<T, S> NewService for ApplyNewService<T, S>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
T::Error: From<S::Error>,
|
||||||
Out: IntoFuture,
|
S: NewService,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Request = In;
|
type Request = T::Request;
|
||||||
type Response = Out::Item;
|
type Response = T::Response;
|
||||||
type Error = Out::Error;
|
type Error = T::Error;
|
||||||
type Service = Apply<T::Service, F, In, Out>;
|
type Service = Apply<T::Transform, S::Service>;
|
||||||
|
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
type Future = ApplyNewServiceFuture<T, F, In, Out>;
|
type Future = ApplyNewServiceFuture<T, S>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_service(&self) -> Self::Future {
|
||||||
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ApplyNewServiceFuture<T, F, In, Out>
|
|
||||||
where
|
|
||||||
T: NewService,
|
|
||||||
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> ApplyNewServiceFuture<T, F, In, Out>
|
|
||||||
where
|
|
||||||
T: NewService,
|
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
fn new(fut: T::Future, f: F) -> Self {
|
|
||||||
ApplyNewServiceFuture {
|
ApplyNewServiceFuture {
|
||||||
f: Some(f),
|
fut_t: self.transform.new_transform(),
|
||||||
fut,
|
fut_s: self.service.new_service(),
|
||||||
r: PhantomData,
|
service: None,
|
||||||
|
transform: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
|
pub struct ApplyNewServiceFuture<T, S>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||||
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
T::Error: From<S::Error>,
|
||||||
Out: IntoFuture,
|
S: NewService,
|
||||||
Out::Error: From<T::Error>,
|
|
||||||
{
|
{
|
||||||
type Item = Apply<T::Service, F, In, Out>;
|
fut_s: S::Future,
|
||||||
|
fut_t: T::Future,
|
||||||
|
service: Option<S::Service>,
|
||||||
|
transform: Option<T::Transform>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> Future for ApplyNewServiceFuture<T, S>
|
||||||
|
where
|
||||||
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
||||||
|
T::Error: From<S::Error>,
|
||||||
|
S: NewService,
|
||||||
|
{
|
||||||
|
type Item = Apply<T::Transform, S::Service>;
|
||||||
type Error = T::InitError;
|
type Error = T::InitError;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
if let Async::Ready(service) = self.fut.poll()? {
|
if self.transform.is_none() {
|
||||||
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
|
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(),
|
||||||
|
}))
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
@ -178,7 +220,8 @@ mod tests {
|
|||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use crate::{IntoNewService, IntoService, NewService, Service, ServiceExt};
|
use super::*;
|
||||||
|
use crate::{NewService, Service};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv;
|
struct Srv;
|
||||||
@ -198,12 +241,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_call() {
|
fn test_apply() {
|
||||||
let blank = |req| Ok(req);
|
let mut srv = Apply::new_fn(
|
||||||
|
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
||||||
let mut srv = blank.into_service().apply(Srv, |req: &'static str, srv| {
|
Srv,
|
||||||
srv.call(()).map(move |res| (req, res))
|
);
|
||||||
});
|
|
||||||
assert!(srv.poll_ready().is_ok());
|
assert!(srv.poll_ready().is_ok());
|
||||||
let res = srv.call("srv").poll();
|
let res = srv.call("srv").poll();
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
@ -212,11 +254,9 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new_service() {
|
fn test_new_service() {
|
||||||
let blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
let new_srv = ApplyNewService::new(
|
||||||
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
||||||
let new_srv = blank.into_new_service().apply(
|
|| Ok::<_, ()>(Srv),
|
||||||
|| Ok(Srv),
|
|
||||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|
||||||
);
|
);
|
||||||
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
||||||
assert!(srv.poll_ready().is_ok());
|
assert!(srv.poll_ready().is_ok());
|
||||||
@ -227,4 +267,20 @@ mod tests {
|
|||||||
panic!()
|
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)),
|
||||||
|
// );
|
||||||
|
// 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!()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
85
actix-service/src/blank.rs
Normal file
85
actix-service/src/blank.rs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::future::{ok, FutureResult};
|
||||||
|
use futures::{Async, Poll};
|
||||||
|
|
||||||
|
use super::{NewService, Service};
|
||||||
|
|
||||||
|
/// Empty service
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Blank<R, E> {
|
||||||
|
_t: PhantomData<(R, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E> Blank<R, E> {
|
||||||
|
//pub fn new() -> Blank<R, E> {
|
||||||
|
// Blank { _t: PhantomData }
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub fn err<E1>(self) -> Blank<R, E1> {
|
||||||
|
Blank { _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R> Blank<R, ()> {
|
||||||
|
pub fn new<E>() -> Blank<R, E> {
|
||||||
|
Blank { _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E> Default for Blank<R, E> {
|
||||||
|
fn default() -> Blank<R, E> {
|
||||||
|
Blank { _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E> Service for Blank<R, E> {
|
||||||
|
type Request = R;
|
||||||
|
type Response = R;
|
||||||
|
type Error = E;
|
||||||
|
type Future = FutureResult<R, E>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, req: R) -> Self::Future {
|
||||||
|
ok(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Empty service factory
|
||||||
|
pub struct BlankNewService<R, E1, E2 = ()> {
|
||||||
|
_t: PhantomData<(R, E1, E2)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E1, E2> BlankNewService<R, E1, E2> {
|
||||||
|
pub fn new() -> BlankNewService<R, E1, E2> {
|
||||||
|
BlankNewService { _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E1> BlankNewService<R, E1, ()> {
|
||||||
|
pub fn new_unit() -> BlankNewService<R, E1, ()> {
|
||||||
|
BlankNewService { _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E1, E2> Default for BlankNewService<R, E1, E2> {
|
||||||
|
fn default() -> BlankNewService<R, E1, E2> {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, E1, E2> NewService for BlankNewService<R, E1, E2> {
|
||||||
|
type Request = R;
|
||||||
|
type Response = R;
|
||||||
|
type Error = E1;
|
||||||
|
type InitError = E2;
|
||||||
|
type Service = Blank<R, E1>;
|
||||||
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
|
fn new_service(&self) -> Self::Future {
|
||||||
|
ok(Blank::default())
|
||||||
|
}
|
||||||
|
}
|
@ -1,56 +1,51 @@
|
|||||||
use std::marker;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{
|
use futures::future::{ok, FutureResult};
|
||||||
future::{ok, FutureResult},
|
use futures::{Async, IntoFuture, Poll};
|
||||||
Async, IntoFuture, Poll,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
|
|
||||||
pub struct FnService<F, Req, Resp, E, Fut>
|
pub struct FnService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut,
|
F: FnMut(Req) -> Out,
|
||||||
Fut: IntoFuture<Item = Resp, Error = E>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
_t: marker::PhantomData<(Req, Resp, E)>,
|
_t: PhantomData<(Req,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, E, Fut> FnService<F, Req, Resp, E, Fut>
|
impl<F, Req, Out> FnService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut,
|
F: FnMut(Req) -> Out,
|
||||||
Fut: IntoFuture<Item = Resp, Error = E>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
pub fn new(f: F) -> Self {
|
||||||
FnService {
|
FnService { f, _t: PhantomData }
|
||||||
f,
|
|
||||||
_t: marker::PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, E, Fut> Clone for FnService<F, Req, Resp, E, Fut>
|
impl<F, Req, Out> Clone for FnService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone,
|
F: FnMut(Req) -> Out + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = E>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
FnService {
|
FnService {
|
||||||
f: self.f.clone(),
|
f: self.f.clone(),
|
||||||
_t: marker::PhantomData,
|
_t: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, E, Fut> Service for FnService<F, Req, Resp, E, Fut>
|
impl<F, Req, Out> Service for FnService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut,
|
F: FnMut(Req) -> Out,
|
||||||
Fut: IntoFuture<Item = Resp, Error = E>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = Out::Item;
|
||||||
type Error = E;
|
type Error = Out::Error;
|
||||||
type Future = Fut::Future;
|
type Future = Out::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
Ok(Async::Ready(()))
|
Ok(Async::Ready(()))
|
||||||
@ -61,47 +56,44 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> IntoService<FnService<F, Req, Resp, Err, Fut>> for F
|
impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + 'static,
|
F: FnMut(Req) -> Out + 'static,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
fn into_service(self) -> FnService<F, Req, Resp, Err, Fut> {
|
fn into_service(self) -> FnService<F, Req, Out> {
|
||||||
FnService::new(self)
|
FnService::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FnNewService<F, Req, Resp, Err, Fut>
|
pub struct FnNewService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut,
|
F: FnMut(Req) -> Out,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
f: F,
|
f: F,
|
||||||
_t: marker::PhantomData<(Req, Resp, Err)>,
|
_t: PhantomData<(Req,)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> FnNewService<F, Req, Resp, Err, Fut>
|
impl<F, Req, Out> FnNewService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone,
|
F: FnMut(Req) -> Out + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
pub fn new(f: F) -> Self {
|
||||||
FnNewService {
|
FnNewService { f, _t: PhantomData }
|
||||||
f,
|
|
||||||
_t: marker::PhantomData,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> NewService for FnNewService<F, Req, Resp, Err, Fut>
|
impl<F, Req, Out> NewService for FnNewService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone,
|
F: FnMut(Req) -> Out + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
type Request = Req;
|
||||||
type Response = Resp;
|
type Response = Out::Item;
|
||||||
type Error = Err;
|
type Error = Out::Error;
|
||||||
type Service = FnService<F, Req, Resp, Err, Fut>;
|
type Service = FnService<F, Req, Out>;
|
||||||
type InitError = ();
|
type InitError = ();
|
||||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
@ -110,20 +102,20 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> IntoNewService<FnNewService<F, Req, Resp, Err, Fut>> for F
|
impl<F, Req, Out> IntoNewService<FnNewService<F, Req, Out>> for F
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone + 'static,
|
F: FnMut(Req) -> Out + Clone + 'static,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, Fut> {
|
fn into_new_service(self) -> FnNewService<F, Req, Out> {
|
||||||
FnNewService::new(self)
|
FnNewService::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut> Clone for FnNewService<F, Req, Resp, Err, Fut>
|
impl<F, Req, Out> Clone for FnNewService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Fut + Clone,
|
F: FnMut(Req) -> Out + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self::new(self.f.clone())
|
Self::new(self.f.clone())
|
||||||
|
123
actix-service/src/fn_transform.rs
Normal file
123
actix-service/src/fn_transform.rs
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::future::{ok, FutureResult};
|
||||||
|
use futures::{Async, IntoFuture, Poll};
|
||||||
|
|
||||||
|
use super::{IntoNewTransform, IntoTransform, NewTransform, Transform};
|
||||||
|
|
||||||
|
pub struct FnTransform<F, S, Req, Res>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
f: F,
|
||||||
|
_t: PhantomData<(S, Req, Res)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res> FnTransform<F, S, Req, Res>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
pub fn new(f: F) -> Self {
|
||||||
|
FnTransform { f, _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res> Clone for FnTransform<F, S, Req, Res>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
FnTransform {
|
||||||
|
f: self.f.clone(),
|
||||||
|
_t: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res> Transform<S> for FnTransform<F, S, Req, Res>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
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> {
|
||||||
|
FnTransform::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FnNewTransform<F, S, Req, Out, Err>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
{
|
||||||
|
f: F,
|
||||||
|
_t: PhantomData<(S, Req, Out, Err)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res, Err> FnNewTransform<F, S, Req, Res, Err>
|
||||||
|
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> NewTransform<S> for FnNewTransform<F, S, Req, Res, Err>
|
||||||
|
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) -> Self::Future {
|
||||||
|
ok(FnTransform::new(self.f.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res, Err> IntoNewTransform<FnNewTransform<F, S, Req, Res, Err>, S> for F
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
fn into_new_transform(self) -> FnNewTransform<F, S, Req, Res, Err> {
|
||||||
|
FnNewTransform::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, S, Req, Res, Err> Clone for FnNewTransform<F, S, Req, Res, Err>
|
||||||
|
where
|
||||||
|
F: FnMut(Req, &mut S) -> Res + Clone,
|
||||||
|
Res: IntoFuture,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self::new(self.f.clone())
|
||||||
|
}
|
||||||
|
}
|
@ -2,24 +2,32 @@ use futures::{Future, IntoFuture, Poll};
|
|||||||
|
|
||||||
mod and_then;
|
mod and_then;
|
||||||
mod and_then_apply;
|
mod and_then_apply;
|
||||||
|
mod and_then_apply_fn;
|
||||||
mod apply;
|
mod apply;
|
||||||
|
mod blank;
|
||||||
mod cell;
|
mod cell;
|
||||||
mod fn_service;
|
mod fn_service;
|
||||||
|
mod fn_transform;
|
||||||
mod from_err;
|
mod from_err;
|
||||||
mod map;
|
mod map;
|
||||||
mod map_err;
|
mod map_err;
|
||||||
mod map_init_err;
|
mod map_init_err;
|
||||||
mod then;
|
mod then;
|
||||||
|
mod transform;
|
||||||
|
|
||||||
pub use self::and_then::{AndThen, AndThenNewService};
|
pub use self::and_then::{AndThen, AndThenNewService};
|
||||||
pub use self::and_then_apply::{AndThenApply, AndThenApplyNewService};
|
use self::and_then_apply::{AndThenTransform, AndThenTransformNewService};
|
||||||
|
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{Apply, ApplyNewService};
|
||||||
|
pub use self::blank::{Blank, BlankNewService};
|
||||||
pub use self::fn_service::{FnNewService, FnService};
|
pub use self::fn_service::{FnNewService, FnService};
|
||||||
|
pub use self::fn_transform::{FnNewTransform, FnTransform};
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
pub use self::map::{Map, MapNewService};
|
pub use self::map::{Map, MapNewService};
|
||||||
pub use self::map_err::{MapErr, MapErrNewService};
|
pub use self::map_err::{MapErr, MapErrNewService};
|
||||||
pub use self::map_init_err::MapInitErr;
|
pub use self::map_init_err::MapInitErr;
|
||||||
pub use self::then::{Then, ThenNewService};
|
pub use self::then::{Then, ThenNewService};
|
||||||
|
pub use self::transform::{IntoNewTransform, IntoTransform, NewTransform, Transform};
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
pub trait Service {
|
pub trait Service {
|
||||||
@ -61,16 +69,30 @@ pub trait Service {
|
|||||||
/// An extension trait for `Service`s that provides a variety of convenient
|
/// An extension trait for `Service`s that provides a variety of convenient
|
||||||
/// adapters
|
/// adapters
|
||||||
pub trait ServiceExt: Service {
|
pub trait ServiceExt: Service {
|
||||||
/// Apply function to specified service and use it as a next service in
|
/// Apply tranformation to specified service and use it as a next service in
|
||||||
/// chain.
|
/// chain.
|
||||||
fn apply<B, I, F, Out, Req>(self, service: I, f: F) -> AndThenApply<Self, B, F, Out>
|
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>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
B: Service<Request = Req, Error = Self::Error>,
|
|
||||||
I: IntoService<B>,
|
|
||||||
F: FnMut(Self::Response, &mut B) -> Out,
|
F: FnMut(Self::Response, &mut B) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: Into<Self::Error>,
|
Out::Error: Into<Self::Error>,
|
||||||
|
B: Service<Error = Self::Error>,
|
||||||
|
B1: IntoService<B>,
|
||||||
{
|
{
|
||||||
AndThenApply::new(self, service, f)
|
AndThenApply::new(self, service, f)
|
||||||
}
|
}
|
||||||
@ -190,14 +212,32 @@ pub trait NewService {
|
|||||||
|
|
||||||
/// 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
|
||||||
/// chain.
|
/// chain.
|
||||||
fn apply<B, I, F, Out, Req>(
|
fn apply<T, T1, B, B1>(
|
||||||
self,
|
self,
|
||||||
service: I,
|
transform: T1,
|
||||||
f: F,
|
service: B1,
|
||||||
) -> AndThenApplyNewService<Self, B, F, Out>
|
) -> AndThenTransformNewService<T, Self, B>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
B: NewService<Request = Req, Error = Self::Error, InitError = Self::InitError>,
|
T: NewTransform<B::Service, Request = Self::Response, InitError = Self::InitError>,
|
||||||
|
T::Error: From<Self::Error>,
|
||||||
|
T1: IntoNewTransform<T, B::Service>,
|
||||||
|
B: NewService<Error = Self::Error, InitError = Self::InitError>,
|
||||||
|
B1: IntoNewService<B>,
|
||||||
|
{
|
||||||
|
AndThenTransformNewService::new(
|
||||||
|
transform.into_new_transform(),
|
||||||
|
self,
|
||||||
|
service.into_new_service(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Apply function to specified service and use it as a next service in
|
||||||
|
/// chain.
|
||||||
|
fn apply_fn<B, I, F, Out>(self, service: I, f: F) -> AndThenApplyNewService<Self, B, F, Out>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
B: NewService<Error = Self::Error, InitError = Self::InitError>,
|
||||||
I: IntoNewService<B>,
|
I: IntoNewService<B>,
|
||||||
F: FnMut(Self::Response, &mut B::Service) -> Out,
|
F: FnMut(Self::Response, &mut B::Service) -> Out,
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
@ -345,7 +385,7 @@ where
|
|||||||
fn into_service(self) -> T;
|
fn into_service(self) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Trait for types that can be converted to a Service
|
/// Trait for types that can be converted to a `NewService`
|
||||||
pub trait IntoNewService<T>
|
pub trait IntoNewService<T>
|
||||||
where
|
where
|
||||||
T: NewService,
|
T: NewService,
|
||||||
|
341
actix-service/src/transform.rs
Normal file
341
actix-service/src/transform.rs
Normal file
@ -0,0 +1,341 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
|
use super::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
|
||||||
|
pub trait NewTransform<Service> {
|
||||||
|
/// Requests handled by the service.
|
||||||
|
type Request;
|
||||||
|
|
||||||
|
/// Responses given by the service.
|
||||||
|
type Response;
|
||||||
|
|
||||||
|
/// Errors produced by the service.
|
||||||
|
type Error;
|
||||||
|
|
||||||
|
/// The `TransformService` value created by this factory
|
||||||
|
type Transform: Transform<
|
||||||
|
Service,
|
||||||
|
Request = Self::Request,
|
||||||
|
Response = Self::Response,
|
||||||
|
Error = Self::Error,
|
||||||
|
>;
|
||||||
|
|
||||||
|
/// Errors produced while building a service.
|
||||||
|
type InitError;
|
||||||
|
|
||||||
|
/// The future response value.
|
||||||
|
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
||||||
|
|
||||||
|
/// Create and return a new service value asynchronously.
|
||||||
|
fn new_transform(&self) -> 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, F, E>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: Fn(Self::Error) -> E,
|
||||||
|
{
|
||||||
|
TransformMapErrNewTransform::new(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T, S> Transform<S> for &'a mut T
|
||||||
|
where
|
||||||
|
T: Transform<S> + 'a,
|
||||||
|
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<(), 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait for types that can be converted to a `TransformService`
|
||||||
|
pub trait IntoTransform<T, S>
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
{
|
||||||
|
/// Convert to a `TransformService`
|
||||||
|
fn into_transform(self) -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait for types that can be converted to a TransfromNewService
|
||||||
|
pub trait IntoNewTransform<T, S>
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
{
|
||||||
|
/// Convert to an `TranformNewService`
|
||||||
|
fn into_new_transform(self) -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> IntoTransform<T, S> for T
|
||||||
|
where
|
||||||
|
T: Transform<S>,
|
||||||
|
{
|
||||||
|
fn into_transform(self) -> T {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S> IntoNewTransform<T, S> for T
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
{
|
||||||
|
fn into_new_transform(self) -> T {
|
||||||
|
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<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, F, E> {
|
||||||
|
t: T,
|
||||||
|
f: F,
|
||||||
|
e: PhantomData<(S, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> TransformMapErrNewTransform<T, S, F, E> {
|
||||||
|
/// Create new `MapErr` new service instance
|
||||||
|
pub fn new(t: T, f: F) -> Self
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
F: Fn(T::Error) -> E,
|
||||||
|
{
|
||||||
|
Self {
|
||||||
|
t,
|
||||||
|
f,
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Clone for TransformMapErrNewTransform<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> NewTransform<S> for TransformMapErrNewTransform<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
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, F, E>;
|
||||||
|
|
||||||
|
fn new_transform(&self) -> Self::Future {
|
||||||
|
TransformMapErrNewTransformFuture::new(self.t.new_transform(), self.f.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TransformMapErrNewTransformFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
F: Fn(T::Error) -> E,
|
||||||
|
{
|
||||||
|
fut: T::Future,
|
||||||
|
f: F,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> TransformMapErrNewTransformFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
F: Fn(T::Error) -> E,
|
||||||
|
{
|
||||||
|
fn new(fut: T::Future, f: F) -> Self {
|
||||||
|
TransformMapErrNewTransformFuture { f, fut }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, S, F, E> Future for TransformMapErrNewTransformFuture<T, S, F, E>
|
||||||
|
where
|
||||||
|
T: NewTransform<S>,
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.2.1] - 2019-02-xx
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Convert `Timeout` and `InFlight` services to a transforms
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2019-02-01
|
## [0.2.0] - 2019-02-01
|
||||||
|
|
||||||
* Fix framed transport error handling
|
* Fix framed transport error handling
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-utils"
|
name = "actix-utils"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix utils - various actix net related services"
|
description = "Actix utils - various actix net related services"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
@ -18,7 +18,8 @@ name = "actix_utils"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.2.0"
|
#actix-service = "0.2.0"
|
||||||
|
actix-service = { path="../actix-service" }
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
use actix_service::{NewTransform, Service, Transform};
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
use futures::future::{ok, FutureResult};
|
||||||
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::counter::{Counter, CounterGuard};
|
use super::counter::{Counter, CounterGuard};
|
||||||
|
|
||||||
@ -7,98 +8,48 @@ use super::counter::{Counter, CounterGuard};
|
|||||||
/// async requests.
|
/// async requests.
|
||||||
///
|
///
|
||||||
/// Default number of in-flight requests is 15
|
/// Default number of in-flight requests is 15
|
||||||
pub struct InFlight<T> {
|
pub struct InFlight {
|
||||||
factory: T,
|
|
||||||
max_inflight: usize,
|
max_inflight: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> InFlight<T> {
|
impl InFlight {
|
||||||
pub fn new<F>(factory: F) -> Self
|
pub fn new(max: usize) -> Self {
|
||||||
where
|
Self { max_inflight: max }
|
||||||
T: NewService,
|
|
||||||
F: IntoNewService<T>,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
factory: factory.into_new_service(),
|
|
||||||
max_inflight: 15,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set max number of in-flight requests.
|
|
||||||
///
|
|
||||||
/// By default max in-flight requests is 15.
|
|
||||||
pub fn max_inflight(mut self, max: usize) -> Self {
|
|
||||||
self.max_inflight = max;
|
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> NewService for InFlight<T>
|
impl Default for InFlight {
|
||||||
where
|
fn default() -> Self {
|
||||||
T: NewService,
|
Self::new(15)
|
||||||
{
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Service> NewTransform<T> for InFlight {
|
||||||
type Request = T::Request;
|
type Request = T::Request;
|
||||||
type Response = T::Response;
|
type Response = T::Response;
|
||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
type InitError = T::InitError;
|
type InitError = ();
|
||||||
type Service = InFlightService<T::Service>;
|
type Transform = InFlightService;
|
||||||
type Future = InFlightResponseFuture<T>;
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
fn new_transform(&self) -> Self::Future {
|
||||||
InFlightResponseFuture {
|
ok(InFlightService::new(self.max_inflight))
|
||||||
fut: self.factory.new_service(),
|
|
||||||
max_inflight: self.max_inflight,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InFlightResponseFuture<T: NewService> {
|
pub struct InFlightService {
|
||||||
fut: T::Future,
|
|
||||||
max_inflight: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: NewService> Future for InFlightResponseFuture<T> {
|
|
||||||
type Item = InFlightService<T::Service>;
|
|
||||||
type Error = T::InitError;
|
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
||||||
Ok(Async::Ready(InFlightService::with_max_inflight(
|
|
||||||
self.max_inflight,
|
|
||||||
try_ready!(self.fut.poll()),
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct InFlightService<T> {
|
|
||||||
service: T,
|
|
||||||
count: Counter,
|
count: Counter,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> InFlightService<T> {
|
impl InFlightService {
|
||||||
pub fn new<F>(service: F) -> Self
|
pub fn new(max: usize) -> Self {
|
||||||
where
|
|
||||||
T: Service,
|
|
||||||
F: IntoService<T>,
|
|
||||||
{
|
|
||||||
Self {
|
Self {
|
||||||
service: service.into_service(),
|
|
||||||
count: Counter::new(15),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_max_inflight<F>(max: usize, service: F) -> Self
|
|
||||||
where
|
|
||||||
T: Service,
|
|
||||||
F: IntoService<T>,
|
|
||||||
{
|
|
||||||
Self {
|
|
||||||
service: service.into_service(),
|
|
||||||
count: Counter::new(max),
|
count: Counter::new(max),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Service for InFlightService<T>
|
impl<T> Transform<T> for InFlightService
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
{
|
{
|
||||||
@ -108,17 +59,17 @@ where
|
|||||||
type Future = InFlightServiceResponse<T>;
|
type Future = InFlightServiceResponse<T>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
let res = self.service.poll_ready()?;
|
if !self.count.available() {
|
||||||
if res.is_ready() && !self.count.available() {
|
|
||||||
log::trace!("InFlight limit exceeded");
|
log::trace!("InFlight limit exceeded");
|
||||||
return Ok(Async::NotReady);
|
return Ok(Async::NotReady);
|
||||||
|
} else {
|
||||||
|
return Ok(Async::Ready(()));
|
||||||
}
|
}
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: T::Request) -> Self::Future {
|
fn call(&mut self, req: T::Request, service: &mut T) -> Self::Future {
|
||||||
InFlightServiceResponse {
|
InFlightServiceResponse {
|
||||||
fut: self.service.call(req),
|
fut: service.call(req),
|
||||||
_guard: self.count.get(),
|
_guard: self.count.get(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,3 +89,73 @@ impl<T: Service> Future for InFlightServiceResponse<T> {
|
|||||||
self.fut.poll()
|
self.fut.poll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use futures::future::lazy;
|
||||||
|
use futures::{Async, Poll};
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use actix_service::{Blank, BlankNewService, NewService, Service, ServiceExt};
|
||||||
|
|
||||||
|
struct SleepService(Duration);
|
||||||
|
|
||||||
|
impl Service for SleepService {
|
||||||
|
type Request = ();
|
||||||
|
type Response = ();
|
||||||
|
type Error = ();
|
||||||
|
type Future = Box<Future<Item = (), Error = ()>>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
|
Box::new(tokio_timer::sleep(self.0).map_err(|_| ()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
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));
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||||
|
|
||||||
|
let mut res = srv.call(());
|
||||||
|
let _ = res.poll();
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::NotReady));
|
||||||
|
|
||||||
|
drop(res);
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||||
|
|
||||||
|
Ok::<_, ()>(())
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_newtransform() {
|
||||||
|
let wait_time = Duration::from_millis(50);
|
||||||
|
let _ = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
|
let srv =
|
||||||
|
BlankNewService::new().apply(InFlight::new(1), || Ok(SleepService(wait_time)));
|
||||||
|
|
||||||
|
if let Async::Ready(mut srv) = srv.new_service().poll().unwrap() {
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||||
|
|
||||||
|
let mut res = srv.call(());
|
||||||
|
let _ = res.poll();
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::NotReady));
|
||||||
|
|
||||||
|
drop(res);
|
||||||
|
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||||
|
} else {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok::<_, ()>(())
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,15 +5,14 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{NewTransform, Service, Transform};
|
||||||
use futures::try_ready;
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
use tokio_timer::{clock, Delay};
|
use tokio_timer::{clock, Delay};
|
||||||
|
|
||||||
/// Applies a timeout to requests.
|
/// Applies a timeout to requests.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Timeout<T> {
|
pub struct Timeout {
|
||||||
inner: T,
|
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +24,12 @@ pub enum TimeoutError<E> {
|
|||||||
Timeout,
|
Timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<E> From<E> for TimeoutError<E> {
|
||||||
|
fn from(err: E) -> Self {
|
||||||
|
TimeoutError::Service(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
@ -34,107 +39,73 @@ impl<E: fmt::Debug> fmt::Debug for TimeoutError<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Timeout<T> {
|
impl<E: PartialEq> PartialEq for TimeoutError<E> {
|
||||||
pub fn new(timeout: Duration, inner: T) -> Self
|
fn eq(&self, other: &TimeoutError<E>) -> bool {
|
||||||
where
|
match self {
|
||||||
T: NewService + Clone,
|
TimeoutError::Service(e1) => match other {
|
||||||
{
|
TimeoutError::Service(e2) => e1 == e2,
|
||||||
Timeout { inner, timeout }
|
TimeoutError::Timeout => false,
|
||||||
}
|
},
|
||||||
}
|
TimeoutError::Timeout => match other {
|
||||||
|
TimeoutError::Service(_) => false,
|
||||||
impl<T> Clone for Timeout<T>
|
TimeoutError::Timeout => true,
|
||||||
where
|
},
|
||||||
T: Clone,
|
|
||||||
{
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Timeout {
|
|
||||||
inner: self.inner.clone(),
|
|
||||||
timeout: self.timeout,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> NewService for Timeout<T>
|
impl Timeout {
|
||||||
where
|
pub fn new(timeout: Duration) -> Self {
|
||||||
T: NewService + Clone,
|
Timeout { timeout }
|
||||||
{
|
|
||||||
type Request = T::Request;
|
|
||||||
type Response = T::Response;
|
|
||||||
type Error = TimeoutError<T::Error>;
|
|
||||||
type InitError = T::InitError;
|
|
||||||
type Service = TimeoutService<T::Service>;
|
|
||||||
type Future = TimeoutFut<T>;
|
|
||||||
|
|
||||||
fn new_service(&self) -> Self::Future {
|
|
||||||
TimeoutFut {
|
|
||||||
fut: self.inner.new_service(),
|
|
||||||
timeout: self.timeout,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `Timeout` response future
|
impl<S> NewTransform<S> for Timeout
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct TimeoutFut<T: NewService> {
|
|
||||||
fut: T::Future,
|
|
||||||
timeout: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Future for TimeoutFut<T>
|
|
||||||
where
|
where
|
||||||
T: NewService,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Item = TimeoutService<T::Service>;
|
type Request = S::Request;
|
||||||
type Error = T::InitError;
|
type Response = S::Response;
|
||||||
|
type Error = TimeoutError<S::Error>;
|
||||||
|
type InitError = ();
|
||||||
|
type Transform = TimeoutService;
|
||||||
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
fn new_transform(&self) -> Self::Future {
|
||||||
let service = try_ready!(self.fut.poll());
|
ok(TimeoutService {
|
||||||
Ok(Async::Ready(TimeoutService::new(self.timeout, service)))
|
timeout: self.timeout,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies a timeout to requests.
|
/// Applies a timeout to requests.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TimeoutService<T> {
|
pub struct TimeoutService {
|
||||||
inner: T,
|
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> TimeoutService<T> {
|
impl TimeoutService {
|
||||||
pub fn new(timeout: Duration, inner: T) -> Self
|
pub fn new(timeout: Duration) -> Self {
|
||||||
where
|
TimeoutService { timeout }
|
||||||
T: Service,
|
|
||||||
{
|
|
||||||
TimeoutService { inner, timeout }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> Clone for TimeoutService<T> {
|
impl<S> Transform<S> for TimeoutService
|
||||||
fn clone(&self) -> Self {
|
|
||||||
TimeoutService {
|
|
||||||
inner: self.inner.clone(),
|
|
||||||
timeout: self.timeout,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Service for TimeoutService<T>
|
|
||||||
where
|
where
|
||||||
T: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = S::Request;
|
||||||
type Response = T::Response;
|
type Response = S::Response;
|
||||||
type Error = TimeoutError<T::Error>;
|
type Error = TimeoutError<S::Error>;
|
||||||
type Future = TimeoutServiceResponse<T>;
|
type Future = TimeoutServiceResponse<S>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
self.inner.poll_ready().map_err(TimeoutError::Service)
|
Ok(Async::Ready(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, request: T::Request) -> Self::Future {
|
fn call(&mut self, request: S::Request, service: &mut S) -> Self::Future {
|
||||||
TimeoutServiceResponse {
|
TimeoutServiceResponse {
|
||||||
fut: self.inner.call(request),
|
fut: service.call(request),
|
||||||
sleep: Delay::new(clock::now() + self.timeout),
|
sleep: Delay::new(clock::now() + self.timeout),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,3 +141,74 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use futures::future::lazy;
|
||||||
|
use futures::{Async, Poll};
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use actix_service::{Blank, BlankNewService, NewService, Service, ServiceExt};
|
||||||
|
|
||||||
|
struct SleepService(Duration);
|
||||||
|
|
||||||
|
impl Service for SleepService {
|
||||||
|
type Request = ();
|
||||||
|
type Response = ();
|
||||||
|
type Error = ();
|
||||||
|
type Future = Box<Future<Item = (), Error = ()>>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
Ok(Async::Ready(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, _: ()) -> Self::Future {
|
||||||
|
Box::new(tokio_timer::sleep(self.0).map_err(|_| ()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_success() {
|
||||||
|
let resolution = Duration::from_millis(100);
|
||||||
|
let wait_time = Duration::from_millis(50);
|
||||||
|
|
||||||
|
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
|
let mut timeout = Blank::default()
|
||||||
|
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
||||||
|
timeout.call(())
|
||||||
|
}));
|
||||||
|
assert_eq!(res, Ok(()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_timeout() {
|
||||||
|
let resolution = Duration::from_millis(100);
|
||||||
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
|
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
|
let mut timeout = Blank::default()
|
||||||
|
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
||||||
|
timeout.call(())
|
||||||
|
}));
|
||||||
|
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_timeout_newservice() {
|
||||||
|
let resolution = Duration::from_millis(100);
|
||||||
|
let wait_time = Duration::from_millis(150);
|
||||||
|
|
||||||
|
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
|
let timeout = BlankNewService::default()
|
||||||
|
.apply(Timeout::new(resolution), || Ok(SleepService(wait_time)));
|
||||||
|
if let Async::Ready(mut to) = timeout.new_service().poll().unwrap() {
|
||||||
|
to.call(())
|
||||||
|
} else {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user