mirror of
https://github.com/fafhrd91/actix-net
synced 2025-01-18 13:01:49 +01:00
simplify transform trait
This commit is contained in:
parent
9bc492cf6c
commit
8c48bf4de7
@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.3.1] - 2019-03-04
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Simplify Transform trait
|
||||||
|
|
||||||
|
|
||||||
## [0.3.0] - 2019-03-02
|
## [0.3.0] - 2019-03-02
|
||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.3.0"
|
version = "0.3.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,141 +1,24 @@
|
|||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::{NewService, NewTransform, Service, Transform};
|
use crate::and_then::AndThen;
|
||||||
use crate::cell::Cell;
|
use crate::from_err::FromErr;
|
||||||
|
use crate::{NewService, Transform};
|
||||||
/// `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()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `Apply` new service combinator
|
/// `Apply` new service combinator
|
||||||
pub struct AndThenTransformNewService<T, A, B, C> {
|
pub struct AndThenTransformNewService<T, A, B, C> {
|
||||||
a: A,
|
a: A,
|
||||||
b: B,
|
b: B,
|
||||||
t: T,
|
t: Rc<T>,
|
||||||
_t: std::marker::PhantomData<C>,
|
_t: std::marker::PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
|
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
A: NewService<C>,
|
||||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
B: NewService<C, InitError = A::InitError>,
|
||||||
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
T::Error: From<A::Error>,
|
T::Error: From<A::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
@ -143,7 +26,7 @@ where
|
|||||||
Self {
|
Self {
|
||||||
a,
|
a,
|
||||||
b,
|
b,
|
||||||
t,
|
t: Rc::new(t),
|
||||||
_t: std::marker::PhantomData,
|
_t: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -153,7 +36,6 @@ impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C>
|
|||||||
where
|
where
|
||||||
A: Clone,
|
A: Clone,
|
||||||
B: Clone,
|
B: Clone,
|
||||||
T: Clone,
|
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -168,8 +50,8 @@ where
|
|||||||
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
|
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
A: NewService<C>,
|
||||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
B: NewService<C, InitError = A::InitError>,
|
||||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
T::Error: From<A::Error>,
|
T::Error: From<A::Error>,
|
||||||
{
|
{
|
||||||
type Request = A::Request;
|
type Request = A::Request;
|
||||||
@ -177,17 +59,17 @@ where
|
|||||||
type Error = T::Error;
|
type Error = T::Error;
|
||||||
|
|
||||||
type InitError = T::InitError;
|
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>;
|
type Future = AndThenTransformNewServiceFuture<T, A, B, C>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
AndThenTransformNewServiceFuture {
|
AndThenTransformNewServiceFuture {
|
||||||
a: None,
|
a: None,
|
||||||
b: None,
|
|
||||||
t: None,
|
t: None,
|
||||||
|
t_cell: self.t.clone(),
|
||||||
fut_a: self.a.new_service(cfg),
|
fut_a: self.a.new_service(cfg),
|
||||||
fut_b: self.b.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>
|
pub struct AndThenTransformNewServiceFuture<T, A, B, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
A: NewService<C>,
|
||||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
B: NewService<C, InitError = A::InitError>,
|
||||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
T::Error: From<A::Error>,
|
T::Error: From<A::Error>,
|
||||||
{
|
{
|
||||||
fut_b: B::Future,
|
|
||||||
fut_a: A::Future,
|
fut_a: A::Future,
|
||||||
fut_t: T::Future,
|
fut_b: B::Future,
|
||||||
|
fut_t: Option<T::Future>,
|
||||||
a: Option<A::Service>,
|
a: Option<A::Service>,
|
||||||
b: Option<B::Service>,
|
|
||||||
t: Option<T::Transform>,
|
t: Option<T::Transform>,
|
||||||
|
t_cell: Rc<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
|
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
|
||||||
where
|
where
|
||||||
A: NewService<C>,
|
A: NewService<C>,
|
||||||
B: NewService<C, Error = A::Error, InitError = A::InitError>,
|
B: NewService<C, InitError = A::InitError>,
|
||||||
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
|
T: Transform<B::Service, Request = A::Response, InitError = A::InitError>,
|
||||||
T::Error: From<A::Error>,
|
T::Error: From<A::Error>,
|
||||||
{
|
{
|
||||||
type Item = AndThenTransform<T::Transform, A::Service, B::Service>;
|
type Item = AndThen<FromErr<A::Service, T::Error>, T::Transform>;
|
||||||
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 self.t.is_none() {
|
if self.fut_t.is_none() {
|
||||||
if let Async::Ready(transform) = self.fut_t.poll()? {
|
if let Async::Ready(service) = self.fut_b.poll()? {
|
||||||
self.t = Some(transform);
|
self.fut_t = Some(self.t_cell.new_transform(service));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,18 +112,17 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.b.is_none() {
|
if let Some(ref mut fut) = self.fut_t {
|
||||||
if let Async::Ready(service) = self.fut_b.poll()? {
|
if let Async::Ready(transform) = fut.poll()? {
|
||||||
self.b = Some(service);
|
self.t = Some(transform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.a.is_some() && self.b.is_some() && self.t.is_some() {
|
if self.a.is_some() && self.t.is_some() {
|
||||||
Ok(Async::Ready(AndThenTransform {
|
Ok(Async::Ready(AndThen::new(
|
||||||
a: self.a.take().unwrap(),
|
FromErr::new(self.a.take().unwrap()),
|
||||||
t: Cell::new(self.t.take().unwrap()),
|
self.t.take().unwrap(),
|
||||||
b: Cell::new(self.b.take().unwrap()),
|
)))
|
||||||
}))
|
|
||||||
} else {
|
} else {
|
||||||
Ok(Async::NotReady)
|
Ok(Async::NotReady)
|
||||||
}
|
}
|
||||||
@ -276,10 +157,11 @@ mod tests {
|
|||||||
fn test_apply() {
|
fn test_apply() {
|
||||||
let blank = |req| Ok(req);
|
let blank = |req| Ok(req);
|
||||||
|
|
||||||
let mut srv = blank.into_service().apply(
|
let mut srv = blank
|
||||||
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
.into_service()
|
||||||
Srv,
|
.apply_fn(Srv, |req: &'static str, srv: &mut 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());
|
||||||
|
@ -1,218 +1,172 @@
|
|||||||
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use super::{FnNewTransform, FnTransform};
|
use futures::{Async, Future, IntoFuture, Poll};
|
||||||
use super::{
|
|
||||||
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform,
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
Service, Transform,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// `Apply` service combinator
|
/// `Apply` service combinator
|
||||||
pub struct Apply<T, S>
|
pub struct Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Transform<S>,
|
T: Service,
|
||||||
T::Error: From<S::Error>,
|
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
transform: T,
|
service: T,
|
||||||
service: S,
|
f: F,
|
||||||
|
r: PhantomData<(In, Out)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S> Apply<T, S>
|
impl<T, F, In, Out> Apply<T, F, In, Out>
|
||||||
where
|
where
|
||||||
T: Transform<S>,
|
T: Service,
|
||||||
T::Error: From<S::Error>,
|
F: FnMut(In, &mut T) -> Out,
|
||||||
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,
|
|
||||||
Out: IntoFuture,
|
Out: IntoFuture,
|
||||||
Out::Error: From<S::Error>,
|
Out::Error: From<T::Error>,
|
||||||
S: Service,
|
|
||||||
{
|
{
|
||||||
/// Create new `Apply` combinator
|
/// 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 {
|
Self {
|
||||||
service: service.into_service(),
|
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
|
where
|
||||||
S: Service + Clone,
|
T: Service + Clone,
|
||||||
T::Error: From<S::Error>,
|
F: Clone,
|
||||||
T: Transform<S> + Clone,
|
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Apply {
|
Apply {
|
||||||
service: self.service.clone(),
|
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
|
where
|
||||||
T: Transform<S>,
|
T: Service,
|
||||||
T::Error: From<S::Error>,
|
F: FnMut(In, &mut T) -> Out,
|
||||||
S: Service,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = In;
|
||||||
type Response = T::Response;
|
type Response = Out::Item;
|
||||||
type Error = T::Error;
|
type Error = Out::Error;
|
||||||
type Future = T::Future;
|
type Future = Out::Future;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
try_ready!(self.service.poll_ready());
|
self.service.poll_ready().map_err(|e| e.into())
|
||||||
self.transform.poll_ready()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
fn call(&mut self, req: In) -> Self::Future {
|
||||||
self.transform.call(req, &mut self.service).into_future()
|
(self.f)(req, &mut self.service).into_future()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `ApplyNewService` new service combinator
|
/// `ApplyNewService` new service combinator
|
||||||
pub struct ApplyNewService<T, S, C>
|
pub struct ApplyNewService<T, F, In, Out, Cfg>
|
||||||
where
|
where
|
||||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
T: NewService<Cfg>,
|
||||||
T::Error: From<S::Error>,
|
|
||||||
S: NewService<C>,
|
|
||||||
{
|
{
|
||||||
transform: T,
|
service: T,
|
||||||
service: S,
|
f: F,
|
||||||
_t: std::marker::PhantomData<C>,
|
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
|
where
|
||||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
T: NewService<Cfg>,
|
||||||
T::Error: From<S::Error>,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
S: NewService<C>,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
/// Create new `ApplyNewService` new service instance
|
/// Create new `ApplyNewService` new service instance
|
||||||
pub fn new<T1: IntoNewTransform<T, S::Service, C>, S1: IntoNewService<S, C>>(
|
pub fn new<F1: IntoNewService<T, Cfg>>(service: F1, f: F) -> Self {
|
||||||
transform: T1,
|
|
||||||
service: S1,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
transform: transform.into_new_transform(),
|
f,
|
||||||
service: service.into_new_service(),
|
service: service.into_new_service(),
|
||||||
_t: std::marker::PhantomData,
|
r: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, S, In, Out, Cfg>
|
impl<T, F, In, Out, Cfg> Clone for ApplyNewService<T, F, In, Out, Cfg>
|
||||||
ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError, Cfg>, S, Cfg>
|
|
||||||
where
|
where
|
||||||
F: FnMut(In, &mut S::Service) -> Out + Clone,
|
T: NewService<Cfg> + Clone,
|
||||||
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
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 {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
service: self.service.clone(),
|
service: self.service.clone(),
|
||||||
transform: self.transform.clone(),
|
f: self.f.clone(),
|
||||||
_t: std::marker::PhantomData,
|
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
|
where
|
||||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
T: NewService<Cfg>,
|
||||||
T::Error: From<S::Error>,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
S: NewService<C>,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = In;
|
||||||
type Response = T::Response;
|
type Response = Out::Item;
|
||||||
type Error = T::Error;
|
type Error = Out::Error;
|
||||||
type Service = Apply<T::Transform, S::Service>;
|
type Service = Apply<T::Service, F, In, Out>;
|
||||||
|
|
||||||
type InitError = T::InitError;
|
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 {
|
ApplyNewServiceFuture {
|
||||||
fut_t: self.transform.new_transform(cfg),
|
f: Some(f),
|
||||||
fut_s: self.service.new_service(cfg),
|
fut,
|
||||||
service: None,
|
r: PhantomData,
|
||||||
transform: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ApplyNewServiceFuture<T, S, C>
|
impl<T, F, In, Out, Cfg> Future for ApplyNewServiceFuture<T, F, In, Out, Cfg>
|
||||||
where
|
where
|
||||||
T: NewTransform<S::Service, C, InitError = S::InitError>,
|
T: NewService<Cfg>,
|
||||||
T::Error: From<S::Error>,
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
||||||
S: NewService<C>,
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<T::Error>,
|
||||||
{
|
{
|
||||||
fut_s: S::Future,
|
type Item = Apply<T::Service, F, In, Out>;
|
||||||
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 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 self.transform.is_none() {
|
if let Async::Ready(service) = self.fut.poll()? {
|
||||||
if let Async::Ready(transform) = self.fut_t.poll()? {
|
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
|
||||||
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)
|
||||||
}
|
}
|
||||||
@ -225,7 +179,7 @@ mod tests {
|
|||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{NewService, Service};
|
use crate::{IntoService, NewService, Service, ServiceExt};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Srv;
|
struct Srv;
|
||||||
@ -245,8 +199,12 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_apply() {
|
fn test_call() {
|
||||||
let mut srv = Apply::new_fn(Srv, |req: &'static str, srv| {
|
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))
|
srv.call(()).map(move |res| (req, res))
|
||||||
});
|
});
|
||||||
assert!(srv.poll_ready().is_ok());
|
assert!(srv.poll_ready().is_ok());
|
||||||
@ -258,22 +216,6 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_new_service() {
|
fn test_new_service() {
|
||||||
let new_srv = ApplyNewService::new(
|
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),
|
|| Ok::<_, ()>(Srv),
|
||||||
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
||||||
);
|
);
|
||||||
|
@ -96,11 +96,7 @@ where
|
|||||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
Box::new(
|
Box::new(self.service.new_service(cfg).map(ServiceWrapper::boxed))
|
||||||
self.service
|
|
||||||
.new_service(cfg)
|
|
||||||
.map(|service| ServiceWrapper::boxed(service)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,122 +1,64 @@
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::future::{ok, FutureResult};
|
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
|
where
|
||||||
F: FnMut(Req, &mut S) -> Res,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
Res: IntoFuture,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
f: F,
|
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
|
where
|
||||||
F: FnMut(Req, &mut S) -> Res,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
Res: IntoFuture,
|
Out: IntoFuture,
|
||||||
{
|
{
|
||||||
pub fn new(f: F) -> Self {
|
pub fn new(f: F) -> Self {
|
||||||
FnTransform { f, _t: PhantomData }
|
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
|
where
|
||||||
F: FnMut(Req, &mut S) -> Res + Clone,
|
S: Service,
|
||||||
Res: IntoFuture,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<S::Error>,
|
||||||
{
|
{
|
||||||
fn clone(&self) -> Self {
|
type Request = In;
|
||||||
FnTransform {
|
type Response = Out::Item;
|
||||||
f: self.f.clone(),
|
type Error = Out::Error;
|
||||||
_t: PhantomData,
|
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
|
where
|
||||||
F: FnMut(Req, &mut S) -> Res,
|
S: Service,
|
||||||
Res: IntoFuture,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
Out::Error: From<S::Error>,
|
||||||
{
|
{
|
||||||
type Request = Req;
|
fn into_transform(self) -> FnTransform<F, S, In, Out, Err> {
|
||||||
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)
|
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
|
where
|
||||||
F: FnMut(Req, &mut S) -> Out + Clone,
|
F: FnMut(In, &mut S) -> Out + Clone,
|
||||||
Out: IntoFuture,
|
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 {
|
fn clone(&self) -> Self {
|
||||||
Self::new(self.f.clone())
|
Self::new(self.f.clone())
|
||||||
|
@ -20,21 +20,20 @@ mod map_err;
|
|||||||
mod map_init_err;
|
mod map_init_err;
|
||||||
mod then;
|
mod then;
|
||||||
mod transform;
|
mod transform;
|
||||||
mod transform_map_err;
|
|
||||||
mod transform_map_init_err;
|
mod transform_map_init_err;
|
||||||
|
|
||||||
pub use self::and_then::{AndThen, AndThenNewService};
|
pub use self::and_then::{AndThen, AndThenNewService};
|
||||||
use self::and_then_apply::{AndThenTransform, AndThenTransformNewService};
|
use self::and_then_apply::AndThenTransformNewService;
|
||||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{Apply, ApplyNewService};
|
||||||
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
||||||
pub use self::fn_transform::{FnNewTransform, FnTransform};
|
pub use self::fn_transform::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};
|
pub use self::transform::{IntoTransform, Transform};
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
pub trait Service {
|
pub trait Service {
|
||||||
@ -76,20 +75,6 @@ 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 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
|
/// Apply function to specified service and use it as a next service in
|
||||||
/// chain.
|
/// chain.
|
||||||
fn apply_fn<F, B, B1, Out>(self, service: B1, f: F) -> AndThenApply<Self, B, F, Out>
|
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>
|
) -> AndThenTransformNewService<T, Self, B, Config>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
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>,
|
T::Error: From<Self::Error>,
|
||||||
T1: IntoNewTransform<T, B::Service>,
|
T1: IntoTransform<T, B::Service>,
|
||||||
B: NewService<Config, Error = Self::Error, InitError = Self::InitError>,
|
B: NewService<Config, InitError = Self::InitError>,
|
||||||
B1: IntoNewService<B, Config>,
|
B1: IntoNewService<B, Config>,
|
||||||
{
|
{
|
||||||
AndThenTransformNewService::new(
|
AndThenTransformNewService::new(
|
||||||
transform.into_new_transform(),
|
transform.into_transform(),
|
||||||
self,
|
self,
|
||||||
service.into_new_service(),
|
service.into_new_service(),
|
||||||
)
|
)
|
||||||
|
@ -1,56 +1,16 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
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::transform_map_init_err::TransformMapInitErr;
|
||||||
use crate::Service;
|
use crate::Service;
|
||||||
|
|
||||||
/// An asynchronous function for transforming service call result.
|
/// `Transform` service factory.
|
||||||
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 factory creates service that wraps other services.
|
||||||
/// `Config` is a service factory configuration type.
|
/// `Config` is a service factory configuration type.
|
||||||
pub trait NewTransform<Service, Config = ()> {
|
pub trait Transform<S> {
|
||||||
/// Requests handled by the service.
|
/// Requests handled by the service.
|
||||||
type Request;
|
type Request;
|
||||||
|
|
||||||
@ -61,8 +21,7 @@ pub trait NewTransform<Service, Config = ()> {
|
|||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
/// The `TransformService` value created by this factory
|
/// The `TransformService` value created by this factory
|
||||||
type Transform: Transform<
|
type Transform: Service<
|
||||||
Service,
|
|
||||||
Request = Self::Request,
|
Request = Self::Request,
|
||||||
Response = Self::Response,
|
Response = Self::Response,
|
||||||
Error = Self::Error,
|
Error = Self::Error,
|
||||||
@ -75,21 +34,11 @@ pub trait NewTransform<Service, Config = ()> {
|
|||||||
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
type Future: Future<Item = Self::Transform, Error = Self::InitError>;
|
||||||
|
|
||||||
/// Create and return a new service value asynchronously.
|
/// Create and return a new service value asynchronously.
|
||||||
fn new_transform(&self, cfg: &Config) -> Self::Future;
|
fn new_transform(&self, service: S) -> 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Map this service's factory init error to a different error,
|
/// Map this service's factory init error to a different error,
|
||||||
/// returning a new transform service factory.
|
/// 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
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
F: Fn(Self::InitError) -> E,
|
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
|
where
|
||||||
T: Transform<S> + 'a,
|
T: Transform<S>,
|
||||||
S: Service<Error = T::Error>,
|
|
||||||
{
|
{
|
||||||
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 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 InitError = T::InitError;
|
||||||
|
type Transform = T::Transform;
|
||||||
type Future = T::Future;
|
type Future = T::Future;
|
||||||
|
|
||||||
fn new_transform(&self, cfg: &C) -> T::Future {
|
fn new_transform(&self, service: S) -> T::Future {
|
||||||
self.as_ref().new_transform(cfg)
|
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
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
{
|
{
|
||||||
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 Transform = T::Transform;
|
|
||||||
type InitError = T::InitError;
|
type InitError = T::InitError;
|
||||||
|
type Transform = T::Transform;
|
||||||
type Future = T::Future;
|
type Future = T::Future;
|
||||||
|
|
||||||
fn new_transform(&self, cfg: &C) -> T::Future {
|
fn new_transform(&self, service: S) -> T::Future {
|
||||||
self.as_ref().new_transform(cfg)
|
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>
|
pub trait IntoTransform<T, S>
|
||||||
where
|
where
|
||||||
T: Transform<S>,
|
T: Transform<S>,
|
||||||
@ -177,15 +88,6 @@ where
|
|||||||
fn into_transform(self) -> T;
|
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
|
impl<T, S> IntoTransform<T, S> for T
|
||||||
where
|
where
|
||||||
T: Transform<S>,
|
T: Transform<S>,
|
||||||
@ -194,12 +96,3 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, S, C> IntoNewTransform<T, S, C> for T
|
|
||||||
where
|
|
||||||
T: NewTransform<S, C>,
|
|
||||||
{
|
|
||||||
fn into_new_transform(self) -> T {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,23 +2,23 @@ use std::marker::PhantomData;
|
|||||||
|
|
||||||
use futures::{Future, Poll};
|
use futures::{Future, Poll};
|
||||||
|
|
||||||
use super::NewTransform;
|
use super::Transform;
|
||||||
|
|
||||||
/// NewTransform for the `map_init_err` combinator, changing the type of a new
|
/// NewTransform for the `map_init_err` combinator, changing the type of a new
|
||||||
/// transform's error.
|
/// transform's error.
|
||||||
///
|
///
|
||||||
/// This is created by the `NewTransform::map_init_err` method.
|
/// 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,
|
t: T,
|
||||||
f: F,
|
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
|
/// Create new `MapInitErr` new transform instance
|
||||||
pub fn new(t: T, f: F) -> Self
|
pub fn new(t: T, f: F) -> Self
|
||||||
where
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E,
|
F: Fn(T::InitError) -> E,
|
||||||
{
|
{
|
||||||
Self {
|
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
|
where
|
||||||
T: Clone,
|
T: Clone,
|
||||||
F: 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
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E + Clone,
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Request = T::Request;
|
type Request = T::Request;
|
||||||
@ -54,25 +54,25 @@ where
|
|||||||
type Transform = T::Transform;
|
type Transform = T::Transform;
|
||||||
|
|
||||||
type InitError = E;
|
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 {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
TransformMapInitErrFuture::new(self.t.new_transform(cfg), self.f.clone())
|
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
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E,
|
F: Fn(T::InitError) -> E,
|
||||||
{
|
{
|
||||||
fut: T::Future,
|
fut: T::Future,
|
||||||
f: F,
|
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
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E,
|
F: Fn(T::InitError) -> E,
|
||||||
{
|
{
|
||||||
fn new(fut: T::Future, f: F) -> Self {
|
fn new(fut: T::Future, 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
|
where
|
||||||
T: NewTransform<S, C>,
|
T: Transform<S>,
|
||||||
F: Fn(T::InitError) -> E + Clone,
|
F: Fn(T::InitError) -> E + Clone,
|
||||||
{
|
{
|
||||||
type Item = T::Transform;
|
type Item = T::Transform;
|
||||||
type Error = E;
|
type Error = E;
|
||||||
|
|
||||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [0.3.1] - 2019-03-04
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
* Use new type of transform trait
|
||||||
|
|
||||||
|
|
||||||
## [0.3.0] - 2019-03-02
|
## [0.3.0] - 2019-03-02
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-utils"
|
name = "actix-utils"
|
||||||
version = "0.3.0"
|
version = "0.3.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.3.0"
|
#actix-service = "0.3.1"
|
||||||
|
actix-service = { path="../actix-service" }
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1.24"
|
futures = "0.1.24"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use actix_service::{NewTransform, Service, Transform, Void};
|
use actix_service::{Service, Transform, Void};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::{Async, Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
@ -24,32 +24,34 @@ impl Default for InFlight {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Service, C> NewTransform<T, C> for InFlight {
|
impl<S: Service> Transform<S> for InFlight {
|
||||||
type Request = T::Request;
|
type Request = S::Request;
|
||||||
type Response = T::Response;
|
type Response = S::Response;
|
||||||
type Error = T::Error;
|
type Error = S::Error;
|
||||||
type InitError = Void;
|
type InitError = Void;
|
||||||
type Transform = InFlightService;
|
type Transform = InFlightService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||||
|
|
||||||
fn new_transform(&self, _: &C) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(InFlightService::new(self.max_inflight))
|
ok(InFlightService::new(self.max_inflight, service))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InFlightService {
|
pub struct InFlightService<S> {
|
||||||
count: Counter,
|
count: Counter,
|
||||||
|
service: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InFlightService {
|
impl<S> InFlightService<S> {
|
||||||
pub fn new(max: usize) -> Self {
|
pub fn new(max: usize, service: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
service,
|
||||||
count: Counter::new(max),
|
count: Counter::new(max),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Transform<T> for InFlightService
|
impl<T> Service for InFlightService<T>
|
||||||
where
|
where
|
||||||
T: Service,
|
T: Service,
|
||||||
{
|
{
|
||||||
@ -59,6 +61,8 @@ 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> {
|
||||||
|
self.service.poll_ready()?;
|
||||||
|
|
||||||
if !self.count.available() {
|
if !self.count.available() {
|
||||||
log::trace!("InFlight limit exceeded");
|
log::trace!("InFlight limit exceeded");
|
||||||
Ok(Async::NotReady)
|
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 {
|
InFlightServiceResponse {
|
||||||
fut: service.call(req),
|
fut: self.service.call(req),
|
||||||
_guard: self.count.get(),
|
_guard: self.count.get(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,7 +126,8 @@ mod tests {
|
|||||||
fn test_transform() {
|
fn test_transform() {
|
||||||
let wait_time = Duration::from_millis(50);
|
let wait_time = Duration::from_millis(50);
|
||||||
let _ = actix_rt::System::new("test").block_on(lazy(|| {
|
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(())));
|
assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
|
||||||
|
|
||||||
let mut res = srv.call(());
|
let mut res = srv.call(());
|
||||||
|
@ -3,7 +3,7 @@ use std::fmt;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_service::{NewTransform, Service, Transform, Void};
|
use actix_service::{Service, Transform, Void};
|
||||||
use futures::future::{ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use futures::task::AtomicTask;
|
use futures::task::AtomicTask;
|
||||||
use futures::unsync::oneshot;
|
use futures::unsync::oneshot;
|
||||||
@ -63,13 +63,8 @@ where
|
|||||||
Self { _t: PhantomData }
|
Self { _t: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn service() -> impl Transform<
|
pub fn service(service: S) -> InOrderService<S> {
|
||||||
S,
|
InOrderService::new(service)
|
||||||
Request = S::Request,
|
|
||||||
Response = S::Response,
|
|
||||||
Error = InOrderError<S::Error>,
|
|
||||||
> {
|
|
||||||
InOrderService::new()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +80,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, C> NewTransform<S, C> for InOrder<S>
|
impl<S> Transform<S> for InOrder<S>
|
||||||
where
|
where
|
||||||
S: Service,
|
S: Service,
|
||||||
S::Response: 'static,
|
S::Response: 'static,
|
||||||
@ -99,12 +94,13 @@ where
|
|||||||
type Transform = InOrderService<S>;
|
type Transform = InOrderService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||||
|
|
||||||
fn new_transform(&self, _: &C) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(InOrderService::new())
|
ok(InOrderService::new(service))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InOrderService<S: Service> {
|
pub struct InOrderService<S: Service> {
|
||||||
|
service: S,
|
||||||
task: Rc<AtomicTask>,
|
task: Rc<AtomicTask>,
|
||||||
acks: VecDeque<Record<S::Response, S::Error>>,
|
acks: VecDeque<Record<S::Response, S::Error>>,
|
||||||
}
|
}
|
||||||
@ -116,27 +112,16 @@ where
|
|||||||
S::Future: 'static,
|
S::Future: 'static,
|
||||||
S::Error: 'static,
|
S::Error: 'static,
|
||||||
{
|
{
|
||||||
pub fn new() -> Self {
|
pub fn new(service: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
service,
|
||||||
acks: VecDeque::new(),
|
acks: VecDeque::new(),
|
||||||
task: Rc::new(AtomicTask::new()),
|
task: Rc::new(AtomicTask::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> Default for InOrderService<S>
|
impl<S> Service 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>
|
|
||||||
where
|
where
|
||||||
S: Service,
|
S: Service,
|
||||||
S::Response: 'static,
|
S::Response: 'static,
|
||||||
@ -149,8 +134,12 @@ where
|
|||||||
type Future = InOrderServiceResponse<S>;
|
type Future = InOrderServiceResponse<S>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||||
|
// poll_ready could be called from different task
|
||||||
self.task.register();
|
self.task.register();
|
||||||
|
|
||||||
|
// check nested service
|
||||||
|
self.service.poll_ready().map_err(InOrderError::Service)?;
|
||||||
|
|
||||||
// check acks
|
// check acks
|
||||||
while !self.acks.is_empty() {
|
while !self.acks.is_empty() {
|
||||||
let rec = self.acks.front_mut().unwrap();
|
let rec = self.acks.front_mut().unwrap();
|
||||||
@ -167,13 +156,13 @@ where
|
|||||||
Ok(Async::Ready(()))
|
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 (tx1, rx1) = oneshot::channel();
|
||||||
let (tx2, rx2) = oneshot::channel();
|
let (tx2, rx2) = oneshot::channel();
|
||||||
self.acks.push_back(Record { rx: rx1, tx: tx2 });
|
self.acks.push_back(Record { rx: rx1, tx: tx2 });
|
||||||
|
|
||||||
let task = self.task.clone();
|
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();
|
task.notify();
|
||||||
let _ = tx1.send(res);
|
let _ = tx1.send(res);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -257,7 +246,7 @@ mod tests {
|
|||||||
let rx3 = rx3;
|
let rx3 = rx3;
|
||||||
let tx_stop = tx_stop;
|
let tx_stop = tx_stop;
|
||||||
let _ = actix_rt::System::new("test").block_on(lazy(move || {
|
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 res1 = srv.call(rx1);
|
||||||
let res2 = srv.call(rx2);
|
let res2 = srv.call(rx2);
|
||||||
|
@ -6,7 +6,7 @@ use std::fmt;
|
|||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use actix_service::{NewTransform, Service, Transform};
|
use actix_service::{Service, Transform};
|
||||||
use futures::future::{ok, FutureResult};
|
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};
|
||||||
@ -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
|
where
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -88,11 +88,12 @@ where
|
|||||||
type Response = S::Response;
|
type Response = S::Response;
|
||||||
type Error = TimeoutError<S::Error>;
|
type Error = TimeoutError<S::Error>;
|
||||||
type InitError = E;
|
type InitError = E;
|
||||||
type Transform = TimeoutService;
|
type Transform = TimeoutService<S>;
|
||||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||||
|
|
||||||
fn new_transform(&self, _: &C) -> Self::Future {
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
ok(TimeoutService {
|
ok(TimeoutService {
|
||||||
|
service,
|
||||||
timeout: self.timeout,
|
timeout: self.timeout,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -100,17 +101,18 @@ where
|
|||||||
|
|
||||||
/// Applies a timeout to requests.
|
/// Applies a timeout to requests.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TimeoutService {
|
pub struct TimeoutService<S> {
|
||||||
|
service: S,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimeoutService {
|
impl<S> TimeoutService<S> {
|
||||||
pub fn new(timeout: Duration) -> Self {
|
pub fn new(timeout: Duration, service: S) -> Self {
|
||||||
TimeoutService { timeout }
|
TimeoutService { service, timeout }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> Transform<S> for TimeoutService
|
impl<S> Service for TimeoutService<S>
|
||||||
where
|
where
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -120,12 +122,12 @@ where
|
|||||||
type Future = TimeoutServiceResponse<S>;
|
type Future = TimeoutServiceResponse<S>;
|
||||||
|
|
||||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
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 {
|
TimeoutServiceResponse {
|
||||||
fut: service.call(request),
|
fut: self.service.call(request),
|
||||||
sleep: Delay::new(clock::now() + self.timeout),
|
sleep: Delay::new(clock::now() + self.timeout),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -197,7 +199,7 @@ mod tests {
|
|||||||
|
|
||||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
let mut timeout = Blank::default()
|
let mut timeout = Blank::default()
|
||||||
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
||||||
timeout.call(())
|
timeout.call(())
|
||||||
}));
|
}));
|
||||||
assert_eq!(res, Ok(()));
|
assert_eq!(res, Ok(()));
|
||||||
@ -210,7 +212,7 @@ mod tests {
|
|||||||
|
|
||||||
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
let res = actix_rt::System::new("test").block_on(lazy(|| {
|
||||||
let mut timeout = Blank::default()
|
let mut timeout = Blank::default()
|
||||||
.apply(TimeoutService::new(resolution), SleepService(wait_time));
|
.and_then(TimeoutService::new(resolution, SleepService(wait_time)));
|
||||||
timeout.call(())
|
timeout.call(())
|
||||||
}));
|
}));
|
||||||
assert_eq!(res, Err(TimeoutError::Timeout));
|
assert_eq!(res, Err(TimeoutError::Timeout));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user