2019-02-03 10:42:27 -08:00
|
|
|
use futures::{Async, Future, Poll};
|
2019-01-24 19:19:44 -08:00
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
use super::{NewService, NewTransform, Service, Transform};
|
2019-01-24 19:19:44 -08:00
|
|
|
use crate::cell::Cell;
|
|
|
|
|
|
|
|
/// `Apply` service combinator
|
2019-02-03 10:42:27 -08:00
|
|
|
pub struct AndThenTransform<T, A, B>
|
2019-01-24 20:41:42 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 20:41:42 -08:00
|
|
|
{
|
2019-01-24 19:19:44 -08:00
|
|
|
a: A,
|
|
|
|
b: Cell<B>,
|
2019-02-03 10:42:27 -08:00
|
|
|
t: Cell<T>,
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> AndThenTransform<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
2019-02-03 10:42:27 -08:00
|
|
|
pub fn new(t: T, a: A, b: B) -> Self {
|
2019-01-24 19:19:44 -08:00
|
|
|
Self {
|
2019-02-03 10:42:27 -08:00
|
|
|
a,
|
|
|
|
b: Cell::new(b),
|
|
|
|
t: Cell::new(t),
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> Clone for AndThenTransform<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service + Clone,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
2019-02-03 10:42:27 -08:00
|
|
|
AndThenTransform {
|
2019-01-24 19:19:44 -08:00
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
2019-02-03 10:42:27 -08:00
|
|
|
t: self.t.clone(),
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> Service for AndThenTransform<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
2019-02-01 19:53:13 -08:00
|
|
|
type Request = A::Request;
|
2019-02-03 10:42:27 -08:00
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = AndThenTransformFuture<T, A, B>;
|
2019-01-24 19:19:44 -08:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-02-03 10:42:27 -08:00
|
|
|
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(()))
|
|
|
|
}
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-01 19:53:13 -08:00
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
2019-02-03 10:42:27 -08:00
|
|
|
AndThenTransformFuture {
|
2019-01-24 19:19:44 -08:00
|
|
|
b: self.b.clone(),
|
2019-02-03 10:42:27 -08:00
|
|
|
t: self.t.clone(),
|
|
|
|
fut_t: None,
|
2019-01-24 19:19:44 -08:00
|
|
|
fut_a: Some(self.a.call(req)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
pub struct AndThenTransformFuture<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
b: Cell<B>,
|
2019-02-03 10:42:27 -08:00
|
|
|
t: Cell<T>,
|
2019-01-24 19:19:44 -08:00
|
|
|
fut_a: Option<A::Future>,
|
2019-02-03 10:42:27 -08:00
|
|
|
fut_t: Option<T::Future>,
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: Service,
|
|
|
|
B: Service<Error = A::Error>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Transform<B, Request = A::Response>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
2019-02-03 10:42:27 -08:00
|
|
|
type Item = T::Response;
|
|
|
|
type Error = T::Error;
|
2019-01-24 19:19:44 -08:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-02-03 10:42:27 -08:00
|
|
|
if let Some(ref mut fut) = self.fut_t {
|
2019-01-24 20:41:42 -08:00
|
|
|
return fut.poll().map_err(|e| e.into());
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.fut_a.as_mut().expect("Bug in actix-service").poll() {
|
|
|
|
Ok(Async::Ready(resp)) => {
|
|
|
|
let _ = self.fut_a.take();
|
2019-02-03 10:42:27 -08:00
|
|
|
self.fut_t = Some(self.t.get_mut().call(resp, self.b.get_mut()));
|
2019-01-24 19:19:44 -08:00
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Err(err) => Err(err.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
/// `Apply` new service combinator
|
|
|
|
pub struct AndThenTransformNewService<T, A, B> {
|
2019-01-24 19:19:44 -08:00
|
|
|
a: A,
|
|
|
|
b: B,
|
2019-02-03 10:42:27 -08:00
|
|
|
t: T,
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> AndThenTransformNewService<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: NewService,
|
|
|
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
/// Create new `ApplyNewService` new service instance
|
2019-02-03 10:42:27 -08:00
|
|
|
pub fn new(t: T, a: A, b: B) -> Self {
|
|
|
|
Self { a, b, t }
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> Clone for AndThenTransformNewService<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
|
|
|
A: Clone,
|
|
|
|
B: Clone,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: Clone,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
2019-02-03 10:42:27 -08:00
|
|
|
t: self.t.clone(),
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> NewService for AndThenTransformNewService<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: NewService,
|
|
|
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
2019-02-01 19:53:13 -08:00
|
|
|
type Request = A::Request;
|
2019-02-03 10:42:27 -08:00
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
2019-01-24 19:19:44 -08:00
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
|
|
|
|
type Future = AndThenTransformNewServiceFuture<T, A, B>;
|
2019-01-24 19:19:44 -08:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2019-02-03 10:42:27 -08:00
|
|
|
AndThenTransformNewServiceFuture {
|
2019-01-24 19:19:44 -08:00
|
|
|
a: None,
|
|
|
|
b: None,
|
2019-02-03 10:42:27 -08:00
|
|
|
t: None,
|
2019-01-24 19:19:44 -08:00
|
|
|
fut_a: self.a.new_service(),
|
|
|
|
fut_b: self.b.new_service(),
|
2019-02-03 10:42:27 -08:00
|
|
|
fut_t: self.t.new_transform(),
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
pub struct AndThenTransformNewServiceFuture<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: NewService,
|
|
|
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
|
|
|
fut_b: B::Future,
|
|
|
|
fut_a: A::Future,
|
2019-02-03 10:42:27 -08:00
|
|
|
fut_t: T::Future,
|
2019-01-24 19:19:44 -08:00
|
|
|
a: Option<A::Service>,
|
|
|
|
b: Option<B::Service>,
|
2019-02-03 10:42:27 -08:00
|
|
|
t: Option<T::Transform>,
|
2019-01-24 19:19:44 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
impl<T, A, B> Future for AndThenTransformNewServiceFuture<T, A, B>
|
2019-01-24 19:19:44 -08:00
|
|
|
where
|
2019-02-01 19:53:13 -08:00
|
|
|
A: NewService,
|
|
|
|
B: NewService<Error = A::Error, InitError = A::InitError>,
|
2019-02-03 10:42:27 -08:00
|
|
|
T: NewTransform<B::Service, Request = A::Response, InitError = A::InitError>,
|
|
|
|
T::Error: From<A::Error>,
|
2019-01-24 19:19:44 -08:00
|
|
|
{
|
2019-02-03 10:42:27 -08:00
|
|
|
type Item = AndThenTransform<T::Transform, A::Service, B::Service>;
|
|
|
|
type Error = T::InitError;
|
2019-01-24 19:19:44 -08:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-02-03 10:42:27 -08:00
|
|
|
if self.t.is_none() {
|
|
|
|
if let Async::Ready(transform) = self.fut_t.poll()? {
|
|
|
|
self.t = Some(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 19:19:44 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
if self.a.is_some() && self.b.is_some() && self.t.is_some() {
|
|
|
|
Ok(Async::Ready(AndThenTransform {
|
2019-01-24 19:19:44 -08:00
|
|
|
a: self.a.take().unwrap(),
|
2019-02-03 10:42:27 -08:00
|
|
|
t: Cell::new(self.t.take().unwrap()),
|
2019-01-24 19:19:44 -08:00
|
|
|
b: Cell::new(self.b.take().unwrap()),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
|
|
|
|
use crate::{IntoNewService, IntoService, NewService, Service, ServiceExt};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv;
|
2019-02-01 19:53:13 -08:00
|
|
|
impl Service for Srv {
|
|
|
|
type Request = ();
|
2019-01-24 19:19:44 -08:00
|
|
|
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]
|
2019-02-03 10:42:27 -08:00
|
|
|
fn test_apply() {
|
2019-01-24 19:19:44 -08:00
|
|
|
let blank = |req| Ok(req);
|
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
let mut srv = blank.into_service().apply(
|
|
|
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
|
|
|
Srv,
|
|
|
|
);
|
2019-01-24 19:19:44 -08:00
|
|
|
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 blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
|
|
|
|
|
|
|
let new_srv = blank.into_new_service().apply(
|
2019-02-03 10:42:27 -08:00
|
|
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
2019-01-24 19:19:44 -08:00
|
|
|
|| 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!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|