2019-03-05 04:38:11 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
|
|
|
|
|
|
|
use super::{IntoNewService, IntoService, NewService, Service};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-03-12 20:53:08 +01:00
|
|
|
/// Apply tranform function to a service
|
|
|
|
pub fn apply_fn<T, F, In, Out, U>(service: U, f: F) -> Apply<T, F, In, Out>
|
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
F: FnMut(In, &mut T) -> Out,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
|
|
|
U: IntoService<T>,
|
|
|
|
{
|
|
|
|
Apply::new(service.into_service(), f)
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Create factory for `apply` service.
|
|
|
|
pub fn new_apply_fn<T, F, In, Out, U>(service: U, f: F) -> ApplyNewService<T, F, In, Out>
|
2019-03-12 20:53:08 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-12 20:53:08 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
2019-05-12 15:03:50 +02:00
|
|
|
U: IntoNewService<T>,
|
2019-03-12 20:53:08 +01:00
|
|
|
{
|
|
|
|
ApplyNewService::new(service.into_new_service(), f)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
2018-08-31 21:51:26 +02:00
|
|
|
/// `Apply` service combinator
|
2019-03-09 15:36:23 +01:00
|
|
|
pub struct Apply<T, F, In, Out>
|
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
{
|
2019-03-05 04:38:11 +01:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2019-03-09 15:36:23 +01:00
|
|
|
r: PhantomData<(In, Out)>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T, F, In, Out> Apply<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Service,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T) -> Out,
|
2019-03-09 15:36:23 +01:00
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
2019-05-12 15:03:50 +02:00
|
|
|
pub(crate) fn new<I: IntoService<T>>(service: I, f: F) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
2018-09-18 01:16:42 +02:00
|
|
|
service: service.into_service(),
|
2019-03-05 04:38:11 +01:00
|
|
|
f,
|
|
|
|
r: PhantomData,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:54:15 +02:00
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T, F, In, Out> Clone for Apply<T, F, In, Out>
|
2018-09-05 22:54:15 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Service + Clone,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Apply {
|
|
|
|
service: self.service.clone(),
|
2019-03-05 04:38:11 +01:00
|
|
|
f: self.f.clone(),
|
|
|
|
r: PhantomData,
|
2018-09-05 22:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<T, F, In, Out> Service for Apply<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
T: Service,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T) -> Out,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = In;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
|
|
|
type Future = Out::Future;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-03-05 04:38:11 +01:00
|
|
|
self.service.poll_ready().map_err(|e| e.into())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 04:38:11 +01:00
|
|
|
fn call(&mut self, req: In) -> Self::Future {
|
|
|
|
(self.f)(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-05-12 15:03:50 +02:00
|
|
|
pub struct ApplyNewService<T, F, In, Out>
|
2019-03-09 15:36:23 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-09 15:36:23 +01:00
|
|
|
{
|
2019-03-05 04:38:11 +01:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2019-05-12 15:03:50 +02:00
|
|
|
r: PhantomData<(In, Out)>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, F, In, Out> ApplyNewService<T, F, In, Out>
|
2019-03-09 15:36:23 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-09 15:36:23 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
/// Create new `ApplyNewService` new service instance
|
2019-05-12 15:03:50 +02:00
|
|
|
pub(crate) fn new<F1: IntoNewService<T>>(service: F1, f: F) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
2019-03-05 04:38:11 +01:00
|
|
|
f,
|
2018-09-04 18:49:21 +02:00
|
|
|
service: service.into_new_service(),
|
2019-03-05 04:38:11 +01:00
|
|
|
r: PhantomData,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, F, In, Out> Clone for ApplyNewService<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService + Clone,
|
2019-03-09 15:36:23 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
service: self.service.clone(),
|
2019-03-05 04:38:11 +01:00
|
|
|
f: self.f.clone(),
|
|
|
|
r: PhantomData,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, F, In, Out> NewService for ApplyNewService<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = In;
|
2019-03-05 04:38:11 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = T::Config;
|
|
|
|
type Service = Apply<T::Service, F, In, Out>;
|
2018-08-30 18:17:17 +02:00
|
|
|
type InitError = T::InitError;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Future = ApplyNewServiceFuture<T, F, In, Out>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &T::Config) -> Self::Future {
|
2019-03-05 06:35:47 +01:00
|
|
|
ApplyNewServiceFuture::new(self.service.new_service(cfg), self.f.clone())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
pub struct ApplyNewServiceFuture<T, F, In, Out>
|
2019-03-05 04:38:11 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
2019-03-05 06:35:47 +01:00
|
|
|
fut: T::Future,
|
2019-03-05 04:38:11 +01:00
|
|
|
f: Option<F>,
|
|
|
|
r: PhantomData<(In, Out)>,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, F, In, Out> ApplyNewServiceFuture<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-03-05 06:35:47 +01:00
|
|
|
fn new(fut: T::Future, f: F) -> Self {
|
2019-03-05 04:38:11 +01:00
|
|
|
ApplyNewServiceFuture {
|
|
|
|
f: Some(f),
|
|
|
|
fut,
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<T, F, In, Out> Future for ApplyNewServiceFuture<T, F, In, Out>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
T: NewService,
|
2019-03-05 04:38:11 +01:00
|
|
|
F: FnMut(In, &mut T::Service) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Item = Apply<T::Service, F, In, Out>;
|
2018-08-30 18:17:17 +02:00
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-03-05 04:38:11 +01:00
|
|
|
if let Async::Ready(service) = self.fut.poll()? {
|
|
|
|
Ok(Async::Ready(Apply::new(service, self.f.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::*;
|
2019-03-05 04:38:11 +01:00
|
|
|
use crate::{IntoService, NewService, Service, ServiceExt};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv;
|
2019-03-09 15:36:23 +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-03-05 04:38:11 +01:00
|
|
|
fn test_call() {
|
|
|
|
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))
|
|
|
|
});
|
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(
|
2019-02-03 20:48:11 +01:00
|
|
|
|| Ok::<_, ()>(Srv),
|
|
|
|
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|
|
|
);
|
2019-02-22 21:44:37 +01:00
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
|
2019-02-03 20:48:11 +01: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!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|