2019-02-03 19:42:27 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-03-09 05:50:29 +01:00
|
|
|
use futures::future::{ok, Future, FutureResult};
|
|
|
|
use futures::{try_ready, Async, IntoFuture, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
use crate::{IntoNewService, IntoService, NewService, Service};
|
2018-09-04 18:49:21 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Create `NewService` for function that can act as a Service
|
|
|
|
pub fn service_fn<F, Req, Out, Cfg>(f: F) -> NewServiceFn<F, Req, Out, Cfg>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
NewServiceFn::new(f)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `NewService` for function that can produce services
|
2019-05-12 15:03:50 +02:00
|
|
|
pub fn new_service_fn<F, C, R, S, E>(f: F) -> FnNewServiceNoConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-05-12 15:03:50 +02:00
|
|
|
R::Item: IntoService<S>,
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceNoConfig::new(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `NewService` for function that can produce services with configuration
|
2019-05-12 15:03:50 +02:00
|
|
|
pub fn new_service_cfg<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R,
|
2019-03-09 05:51:50 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceConfig::new(f)
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
pub struct ServiceFn<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-05-12 15:03:50 +02:00
|
|
|
_t: PhantomData<Req>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out> ServiceFn<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-05-12 15:03:50 +02:00
|
|
|
pub(crate) fn new(f: F) -> Self {
|
|
|
|
ServiceFn { f, _t: PhantomData }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out> Clone for ServiceFn<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 {
|
2019-05-12 15:03:50 +02:00
|
|
|
ServiceFn::new(self.f.clone())
|
2018-08-28 05:32:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out> Service for ServiceFn<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-03-09 15:36:23 +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-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out> IntoService<ServiceFn<F, Req, Out>> for F
|
2018-09-04 18:57:47 +02:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: FnMut(Req) -> Out,
|
2019-02-03 19:42:27 +01:00
|
|
|
Out: IntoFuture,
|
2018-09-04 18:57:47 +02:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
fn into_service(self) -> ServiceFn<F, Req, Out> {
|
|
|
|
ServiceFn::new(self)
|
2018-09-04 18:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
pub struct NewServiceFn<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-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out, Cfg> NewServiceFn<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-05-12 15:03:50 +02:00
|
|
|
pub(crate) fn new(f: F) -> Self {
|
|
|
|
NewServiceFn { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, Req, Out, Cfg> Clone for NewServiceFn<F, Req, Out, Cfg>
|
|
|
|
where
|
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
NewServiceFn::new(self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out, Cfg> NewService for NewServiceFn<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-03-09 15:36:23 +01:00
|
|
|
type Request = Req;
|
2019-02-03 19:42:27 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
2019-02-22 21:44:37 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = Cfg;
|
|
|
|
type Service = ServiceFn<F, Req, Out>;
|
2019-03-02 22:49:21 +01: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 {
|
2019-05-12 15:03:50 +02:00
|
|
|
ok(ServiceFn::new(self.f.clone()))
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out, Cfg> IntoService<ServiceFn<F, Req, Out>> for NewServiceFn<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-05-12 15:03:50 +02:00
|
|
|
fn into_service(self) -> ServiceFn<F, Req, Out> {
|
|
|
|
ServiceFn::new(self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 22:08:31 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, Req, Out, Cfg> IntoNewService<NewServiceFn<F, Req, Out, Cfg>> for F
|
2019-02-22 23:19:43 +01:00
|
|
|
where
|
|
|
|
F: Fn(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
fn into_new_service(self) -> NewServiceFn<F, Req, Out, Cfg> {
|
|
|
|
NewServiceFn::new(self)
|
2019-02-22 22:20:52 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 23:13:48 +01:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-03-09 15:36:23 +01:00
|
|
|
pub struct FnNewServiceConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R,
|
2019-03-09 05:50:29 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
f: F,
|
2019-03-09 15:36:23 +01:00
|
|
|
_t: PhantomData<(C, R, S, E)>,
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R,
|
2019-03-09 05:50:29 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
FnNewServiceConfig { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<F, C, R, S, E> NewService for FnNewServiceConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R,
|
2019-03-09 05:50:29 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = S::Request;
|
2019-02-22 23:13:48 +01:00
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = C;
|
|
|
|
type Service = S;
|
2019-02-22 23:13:48 +01:00
|
|
|
type InitError = E;
|
2019-03-09 15:36:23 +01:00
|
|
|
type Future = FnNewServiceConfigFut<R, S, E>;
|
2019-02-22 23:13:48 +01:00
|
|
|
|
|
|
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
2019-03-09 05:50:29 +01:00
|
|
|
FnNewServiceConfigFut {
|
|
|
|
fut: (self.f)(cfg).into_future(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
pub struct FnNewServiceConfigFut<R, S, E>
|
2019-03-09 05:50:29 +01:00
|
|
|
where
|
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-03-09 05:50:29 +01:00
|
|
|
{
|
|
|
|
fut: R::Future,
|
2019-03-09 15:36:23 +01:00
|
|
|
_t: PhantomData<(S,)>,
|
2019-03-09 05:50:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<R, S, E> Future for FnNewServiceConfigFut<R, S, E>
|
2019-03-09 05:50:29 +01:00
|
|
|
where
|
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-03-09 05:50:29 +01:00
|
|
|
{
|
|
|
|
type Item = S;
|
|
|
|
type Error = R::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R + Clone,
|
2019-03-09 05:50:29 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
R::Item: IntoService<S>,
|
|
|
|
S: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Converter for `Fn() -> Future<Service>` fn
|
|
|
|
pub struct FnNewServiceNoConfig<F, C, R, S, E>
|
2019-02-22 23:30:00 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-02-22 23:30:00 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
f: F,
|
|
|
|
_t: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, R, S, E> FnNewServiceNoConfig<F, C, R, S, E>
|
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
FnNewServiceNoConfig { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, R, S, E> NewService for FnNewServiceNoConfig<F, C, R, S, E>
|
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
type Config = C;
|
|
|
|
type InitError = E;
|
|
|
|
type Future = R::Future;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &C) -> Self::Future {
|
|
|
|
(self.f)().into_future()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, R, S, E> Clone for FnNewServiceNoConfig<F, C, R, S, E>
|
|
|
|
where
|
|
|
|
F: Fn() -> R + Clone,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, R, S, E> IntoNewService<FnNewServiceNoConfig<F, C, R, S, E>> for F
|
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
|
|
|
|
FnNewServiceNoConfig::new(self)
|
2019-02-22 23:30:00 +01:00
|
|
|
}
|
|
|
|
}
|