2019-02-03 19:42:27 +01:00
|
|
|
use futures::{try_ready, Async, Future, IntoFuture, Poll};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
use super::{FnNewTransform, FnTransform};
|
|
|
|
use super::{
|
|
|
|
IntoNewService, IntoNewTransform, IntoService, IntoTransform, NewService, NewTransform,
|
|
|
|
Service, Transform,
|
|
|
|
};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
/// `Apply` service combinator
|
2019-02-03 19:42:27 +01:00
|
|
|
pub struct Apply<T, S>
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: Transform<S>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: Service,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
transform: T,
|
|
|
|
service: S,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> Apply<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: Transform<S>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
|
|
|
pub fn new<T1: IntoTransform<T, S>, S1: IntoService<S>>(
|
|
|
|
transform: T1,
|
|
|
|
service: S1,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
transform: transform.into_transform(),
|
|
|
|
service: service.into_service(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, S, Req, Out> Apply<FnTransform<F, S, Req, Out>, S>
|
|
|
|
where
|
|
|
|
F: FnMut(Req, &mut S) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-02-03 19:42:27 +01:00
|
|
|
Out::Error: From<S::Error>,
|
|
|
|
S: Service,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
2019-02-03 19:52:44 +01:00
|
|
|
pub fn new_fn<S1: IntoService<S>>(service: S1, transform: F) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
2018-09-18 01:16:42 +02:00
|
|
|
service: service.into_service(),
|
2019-02-03 19:52:44 +01:00
|
|
|
transform: transform.into_transform(),
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:54:15 +02:00
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> Clone for Apply<T, S>
|
2018-09-05 22:54:15 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
S: Service + Clone,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
T: Transform<S> + Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Apply {
|
|
|
|
service: self.service.clone(),
|
2019-02-03 19:42:27 +01:00
|
|
|
transform: self.transform.clone(),
|
2018-09-05 22:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> Service for Apply<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: Transform<S>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: Service,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-02-03 19:42:27 +01:00
|
|
|
try_ready!(self.service.poll_ready());
|
|
|
|
self.transform.poll_ready().map_err(|e| e.into())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
self.transform.call(req, &mut self.service).into_future()
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:54:59 +02:00
|
|
|
/// `ApplyNewService` new service combinator
|
2019-02-03 19:42:27 +01:00
|
|
|
pub struct ApplyNewService<T, S>
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
// T::InitError: From<S::InitError>,
|
|
|
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
2019-02-03 20:16:24 +01:00
|
|
|
T::Error: From<S::Error>,
|
2019-02-03 19:42:27 +01:00
|
|
|
S: NewService,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
transform: T,
|
|
|
|
service: S,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> ApplyNewService<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: NewService,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
/// Create new `ApplyNewService` new service instance
|
2019-02-03 19:42:27 +01:00
|
|
|
pub fn new<T1: IntoNewTransform<T, S::Service>, S1: IntoNewService<S>>(
|
|
|
|
transform: T1,
|
|
|
|
service: S1,
|
|
|
|
) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
2019-02-03 19:42:27 +01:00
|
|
|
transform: transform.into_new_transform(),
|
2018-09-04 18:49:21 +02:00
|
|
|
service: service.into_new_service(),
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 20:16:24 +01:00
|
|
|
impl<F, S, In, Out> ApplyNewService<FnNewTransform<F, S::Service, In, Out, S::InitError>, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 20:16:24 +01:00
|
|
|
F: FnMut(In, &mut S::Service) -> Out + Clone,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-02-03 19:42:27 +01:00
|
|
|
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,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
service: self.service.clone(),
|
2019-02-03 19:42:27 +01:00
|
|
|
transform: self.transform.clone(),
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> NewService for ApplyNewService<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: NewService,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Service = Apply<T::Transform, S::Service>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
type InitError = T::InitError;
|
2019-02-03 19:42:27 +01:00
|
|
|
type Future = ApplyNewServiceFuture<T, S>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2019-02-03 19:42:27 +01:00
|
|
|
ApplyNewServiceFuture {
|
|
|
|
fut_t: self.transform.new_transform(),
|
|
|
|
fut_s: self.service.new_service(),
|
|
|
|
service: None,
|
|
|
|
transform: None,
|
|
|
|
}
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
pub struct ApplyNewServiceFuture<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: NewService,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
fut_s: S::Future,
|
|
|
|
fut_t: T::Future,
|
|
|
|
service: Option<S::Service>,
|
|
|
|
transform: Option<T::Transform>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<T, S> Future for ApplyNewServiceFuture<T, S>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
T: NewTransform<S::Service, InitError = S::InitError>,
|
|
|
|
T::Error: From<S::Error>,
|
|
|
|
S: NewService,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
type Item = Apply<T::Transform, S::Service>;
|
2018-08-30 18:17:17 +02:00
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-02-03 19:42:27 +01:00
|
|
|
if self.transform.is_none() {
|
|
|
|
if let Async::Ready(transform) = self.fut_t.poll()? {
|
|
|
|
self.transform = Some(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.service.is_none() {
|
|
|
|
if let Async::Ready(service) = self.fut_s.poll()? {
|
|
|
|
self.service = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.transform.is_some() && self.service.is_some() {
|
|
|
|
Ok(Async::Ready(Apply {
|
|
|
|
service: self.service.take().unwrap(),
|
|
|
|
transform: self.transform.take().unwrap(),
|
|
|
|
}))
|
2018-08-30 18:17:17 +02:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
use super::*;
|
|
|
|
use crate::{NewService, Service};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv;
|
2019-02-02 04:53:13 +01:00
|
|
|
impl Service for Srv {
|
|
|
|
type Request = ();
|
2018-09-12 22:34:53 +02: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 19:42:27 +01:00
|
|
|
fn test_apply() {
|
2019-02-03 20:48:11 +01:00
|
|
|
let mut srv = Apply::new_fn(Srv, |req: &'static str, srv| {
|
|
|
|
srv.call(()).map(move |res| (req, res))
|
|
|
|
});
|
2018-09-18 01:50:35 +02:00
|
|
|
assert!(srv.poll_ready().is_ok());
|
2018-09-12 22:34:53 +02:00
|
|
|
let res = srv.call("srv").poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
2019-02-03 19:42:27 +01:00
|
|
|
let new_srv = ApplyNewService::new(
|
|
|
|
|req: &'static str, srv: &mut Srv| srv.call(()).map(move |res| (req, res)),
|
|
|
|
|| Ok::<_, ()>(Srv),
|
2018-09-18 04:21:24 +02:00
|
|
|
);
|
|
|
|
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!()
|
|
|
|
}
|
|
|
|
}
|
2019-02-03 19:42:27 +01:00
|
|
|
|
2019-02-03 20:48:11 +01:00
|
|
|
#[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!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|