2019-02-03 19:42:27 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Async, IntoFuture, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
use super::{IntoService, NewService, Service};
|
2018-09-04 18:49:21 +02:00
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
pub struct FnService<F, Req, Out>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
f: F,
|
2019-02-03 19:42:27 +01:00
|
|
|
_t: PhantomData<(Req,)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<F, Req, Out> FnService<F, Req, Out>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
2019-02-03 19:42:27 +01:00
|
|
|
FnService { f, _t: PhantomData }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<F, Req, Out> Clone for FnService<F, Req, Out>
|
2018-08-28 05:32:49 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-28 05:32:49 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
FnService {
|
|
|
|
f: self.f.clone(),
|
2019-02-03 19:42:27 +01:00
|
|
|
_t: PhantomData,
|
2018-08-28 05:32:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<F, Req, Out> Service for FnService<F, Req, Out>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-02-02 04:53:13 +01:00
|
|
|
type Request = Req;
|
2019-02-03 19:42:27 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
|
|
|
type Future = Out::Future;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
|
|
|
(self.f)(req).into_future()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 19:42:27 +01:00
|
|
|
impl<F, Req, Out> IntoService<FnService<F, Req, Out>> for F
|
2018-09-04 18:57:47 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out + 'static,
|
|
|
|
Out: IntoFuture,
|
2018-09-04 18:57:47 +02:00
|
|
|
{
|
2019-02-03 19:42:27 +01:00
|
|
|
fn into_service(self) -> FnService<F, Req, Out> {
|
2018-09-04 18:57:47 +02:00
|
|
|
FnService::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
pub struct FnNewService<F, Req, Out, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
f: F,
|
2019-02-22 21:44:37 +01:00
|
|
|
_t: PhantomData<(Req, Cfg)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
impl<F, Req, Out, Cfg> FnNewService<F, Req, Out, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2018-08-29 05:32:01 +02:00
|
|
|
pub fn new(f: F) -> Self {
|
2019-02-03 19:42:27 +01:00
|
|
|
FnNewService { f, _t: PhantomData }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
impl<F, Req, Out, Cfg> NewService<Cfg> for FnNewService<F, Req, Out, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-02-02 04:53:13 +01:00
|
|
|
type Request = Req;
|
2019-02-03 19:42:27 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
|
|
|
type Service = FnService<F, Req, Out>;
|
2019-02-22 21:44:37 +01:00
|
|
|
|
2018-10-03 06:31:11 +02:00
|
|
|
type InitError = ();
|
2018-08-25 18:02:14 +02:00
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
fn new_service(&self, _: &Cfg) -> Self::Future {
|
2018-08-25 18:02:14 +02:00
|
|
|
ok(FnService::new(self.f.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:44:37 +01:00
|
|
|
impl<F, Req, Out, Cfg> Clone for FnNewService<F, Req, Out, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-02-03 19:42:27 +01:00
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|