1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-02-12 20:15:32 +01:00
actix-net/actix-service/src/and_then_apply.rs

307 lines
8.1 KiB
Rust
Raw Normal View History

use futures::{Async, Future, Poll};
2019-01-24 19:19:44 -08:00
use super::{NewService, NewTransform, Service, Transform};
2019-01-24 19:19:44 -08:00
use crate::cell::Cell;
/// `Apply` service combinator
pub struct AndThenTransform<T, A, B>
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
{
2019-01-24 19:19:44 -08:00
a: A,
b: Cell<B>,
t: Cell<T>,
2019-01-24 19:19:44 -08:00
}
impl<T, A, B> AndThenTransform<T, A, B>
2019-01-24 19:19:44 -08:00
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -08:00
{
/// Create new `Apply` combinator
pub fn new(t: T, a: A, b: B) -> Self {
2019-01-24 19:19:44 -08:00
Self {
a,
b: Cell::new(b),
t: Cell::new(t),
2019-01-24 19:19:44 -08:00
}
}
}
impl<T, A, B> Clone for AndThenTransform<T, A, B>
2019-01-24 19:19:44 -08:00
where
A: Service + Clone,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -08:00
{
fn clone(&self) -> Self {
AndThenTransform {
2019-01-24 19:19:44 -08:00
a: self.a.clone(),
b: self.b.clone(),
t: self.t.clone(),
2019-01-24 19:19:44 -08:00
}
}
}
impl<T, A, B> Service for AndThenTransform<T, A, B>
2019-01-24 19:19:44 -08:00
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -08:00
{
type Request = A::Request;
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> {
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
}
fn call(&mut self, req: A::Request) -> Self::Future {
AndThenTransformFuture {
2019-01-24 19:19:44 -08:00
b: self.b.clone(),
t: self.t.clone(),
fut_t: None,
2019-01-24 19:19:44 -08:00
fut_a: Some(self.a.call(req)),
}
}
}
pub struct AndThenTransformFuture<T, A, B>
2019-01-24 19:19:44 -08:00
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -08:00
{
b: Cell<B>,
t: Cell<T>,
2019-01-24 19:19:44 -08:00
fut_a: Option<A::Future>,
fut_t: Option<T::Future>,
2019-01-24 19:19:44 -08:00
}
impl<T, A, B> Future for AndThenTransformFuture<T, A, B>
2019-01-24 19:19:44 -08:00
where
A: Service,
B: Service<Error = A::Error>,
T: Transform<B, Request = A::Response>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -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> {
if let Some(ref mut fut) = self.fut_t {
2019-02-04 11:04:10 -08:00
return fut.poll();
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();
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()),
}
}
}
/// `Apply` new service combinator
2019-02-22 12:44:37 -08:00
pub struct AndThenTransformNewService<T, A, B, C> {
2019-01-24 19:19:44 -08:00
a: A,
b: B,
t: T,
2019-02-22 12:44:37 -08:00
_t: std::marker::PhantomData<C>,
2019-01-24 19:19:44 -08:00
}
2019-02-22 12:44:37 -08:00
impl<T, A, B, C> AndThenTransformNewService<T, A, B, C>
2019-01-24 19:19:44 -08:00
where
2019-02-22 12:44:37 -08:00
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
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
pub fn new(t: T, a: A, b: B) -> Self {
2019-02-22 12:44:37 -08:00
Self {
a,
b,
t,
_t: std::marker::PhantomData,
}
2019-01-24 19:19:44 -08:00
}
}
2019-02-22 12:44:37 -08:00
impl<T, A, B, C> Clone for AndThenTransformNewService<T, A, B, C>
2019-01-24 19:19:44 -08:00
where
A: Clone,
B: Clone,
T: Clone,
2019-01-24 19:19:44 -08:00
{
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: self.b.clone(),
t: self.t.clone(),
2019-02-22 12:44:37 -08:00
_t: std::marker::PhantomData,
2019-01-24 19:19:44 -08:00
}
}
}
2019-02-22 12:44:37 -08:00
impl<T, A, B, C> NewService<C> for AndThenTransformNewService<T, A, B, C>
2019-01-24 19:19:44 -08:00
where
2019-02-22 12:44:37 -08:00
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -08:00
{
type Request = A::Request;
type Response = T::Response;
type Error = T::Error;
2019-01-24 19:19:44 -08:00
type InitError = T::InitError;
type Service = AndThenTransform<T::Transform, A::Service, B::Service>;
2019-02-22 12:44:37 -08:00
type Future = AndThenTransformNewServiceFuture<T, A, B, C>;
2019-01-24 19:19:44 -08:00
2019-02-22 12:44:37 -08:00
fn new_service(&self, cfg: &C) -> Self::Future {
AndThenTransformNewServiceFuture {
2019-01-24 19:19:44 -08:00
a: None,
b: None,
t: None,
2019-02-22 12:44:37 -08:00
fut_a: self.a.new_service(cfg),
fut_b: self.b.new_service(cfg),
fut_t: self.t.new_transform(cfg),
2019-01-24 19:19:44 -08:00
}
}
}
2019-02-22 12:44:37 -08:00
pub struct AndThenTransformNewServiceFuture<T, A, B, C>
2019-01-24 19:19:44 -08:00
where
2019-02-22 12:44:37 -08:00
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, 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,
fut_t: T::Future,
2019-01-24 19:19:44 -08:00
a: Option<A::Service>,
b: Option<B::Service>,
t: Option<T::Transform>,
2019-01-24 19:19:44 -08:00
}
2019-02-22 12:44:37 -08:00
impl<T, A, B, C> Future for AndThenTransformNewServiceFuture<T, A, B, C>
2019-01-24 19:19:44 -08:00
where
2019-02-22 12:44:37 -08:00
A: NewService<C>,
B: NewService<C, Error = A::Error, InitError = A::InitError>,
T: NewTransform<B::Service, C, Request = A::Response, InitError = A::InitError>,
T::Error: From<A::Error>,
2019-01-24 19:19:44 -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> {
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);
}
}
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(),
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;
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]
fn test_apply() {
2019-01-24 19:19:44 -08:00
let blank = |req| Ok(req);
let mut srv = blank.into_service().apply(
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
Srv,
);
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(
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
2019-01-24 19:19:44 -08:00
|| Ok(Srv),
);
2019-02-22 12:44:37 -08:00
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
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", ())));
} else {
panic!()
}
}
}