2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2019-02-03 19:42:27 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::future::{ok, Ready};
|
|
|
|
use pin_project::pin_project;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
2018-09-04 18:49:21 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Create `ServiceFactory` for function that can act as a `Service`
|
|
|
|
pub fn service_fn<F, Fut, Req, Res, Err, Cfg>(
|
|
|
|
f: F,
|
|
|
|
) -> impl ServiceFactory<Config = Cfg, Request = Req, Response = Res, Error = Err, InitError = ()>
|
|
|
|
+ Clone
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
NewServiceFn::new(f)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
pub fn service_fn2<F, Fut, Req, Res, Err>(
|
|
|
|
f: F,
|
|
|
|
) -> impl Service<Request = Req, Response = Res, Error = Err>
|
|
|
|
where
|
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
{
|
|
|
|
ServiceFn::new(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `ServiceFactory` for function that can produce services
|
|
|
|
pub fn factory_fn<S, F, Cfg, Fut, Err>(
|
|
|
|
f: F,
|
|
|
|
) -> impl ServiceFactory<
|
|
|
|
Config = Cfg,
|
|
|
|
Service = S,
|
|
|
|
Request = S::Request,
|
|
|
|
Response = S::Response,
|
|
|
|
Error = S::Error,
|
|
|
|
InitError = Err,
|
|
|
|
Future = Fut,
|
|
|
|
>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn() -> Fut,
|
|
|
|
Fut: Future<Output = Result<S, Err>>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceNoConfig::new(f)
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Create `ServiceFactory` for function that can produce services with configuration
|
|
|
|
pub fn factory_fn_cfg<F, Fut, Cfg, Srv, Err>(
|
|
|
|
f: F,
|
|
|
|
) -> impl ServiceFactory<
|
|
|
|
Config = Cfg,
|
|
|
|
Service = Srv,
|
|
|
|
Request = Srv::Request,
|
|
|
|
Response = Srv::Response,
|
|
|
|
Error = Srv::Error,
|
|
|
|
InitError = Err,
|
|
|
|
>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn(&Cfg) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceConfig::new(f)
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
pub struct ServiceFn<F, Fut, Req, Res, Err>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
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-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> ServiceFn<F, Fut, Req, Res, Err>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
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-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Clone for ServiceFn<F, Fut, Req, Res, Err>
|
2018-08-28 05:32:49 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
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-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Service for ServiceFn<F, Fut, Req, Res, Err>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = Req;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type Future = Fut;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
(self.f)(req)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> IntoService<ServiceFn<F, Fut, Req, Res, Err>> for F
|
2018-09-04 18:57:47 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-09-04 18:57:47 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn into_service(self) -> ServiceFn<F, Fut, Req, Res, Err> {
|
2019-05-12 15:03:50 +02:00
|
|
|
ServiceFn::new(self)
|
2018-09-04 18:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
struct NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
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-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn new(f: F) -> Self {
|
2019-05-12 15:03:50 +02:00
|
|
|
NewServiceFn { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> Clone for NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
NewServiceFn::new(self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory for NewServiceFn<F, Fut, Req, Res, Err, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: FnMut(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = Req;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
2019-02-22 21:44:37 +01:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = Cfg;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Service = ServiceFn<F, Fut, Req, Res, Err>;
|
2019-03-02 22:49:21 +01:00
|
|
|
type InitError = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<Self::Service, Self::InitError>>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
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-02-22 23:13:48 +01:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-11-14 13:38:24 +01:00
|
|
|
struct FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn(&Cfg) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
f: F,
|
2019-11-14 13:38:24 +01:00
|
|
|
_t: PhantomData<(Fut, Cfg, Srv, Err)>,
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Err> FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn(&Cfg) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
FnNewServiceConfig { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnNewServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn(&Cfg) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Request = Srv::Request;
|
|
|
|
type Response = Srv::Response;
|
|
|
|
type Error = Srv::Error;
|
2019-02-22 23:13:48 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
type Config = Cfg;
|
|
|
|
type Service = Srv;
|
|
|
|
type InitError = Err;
|
|
|
|
type Future = FnNewServiceConfigFut<Fut, Srv, Err>;
|
2019-02-22 23:13:48 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn new_service(&self, cfg: &Cfg) -> Self::Future {
|
2019-03-09 05:50:29 +01:00
|
|
|
FnNewServiceConfigFut {
|
2019-11-14 13:38:24 +01:00
|
|
|
fut: (self.f)(cfg),
|
2019-03-09 05:50:29 +01:00
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project]
|
|
|
|
struct FnNewServiceConfigFut<R, S, E>
|
2019-03-09 05:50:29 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-03-09 05:50:29 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin]
|
|
|
|
fut: R,
|
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
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-03-09 05:50:29 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<S, E>;
|
2019-03-09 05:50:29 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
Poll::Ready(Ok(futures::ready!(self.project().fut.poll(cx))?))
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, 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,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn new(f: F) -> Self {
|
2019-05-12 15:03:50 +02:00
|
|
|
FnNewServiceNoConfig { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, C, R, S, E> ServiceFactory for FnNewServiceNoConfig<F, C, R, S, E>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
type Request = S::Request;
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
type Config = C;
|
|
|
|
type InitError = E;
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = R;
|
2019-05-12 15:03:50 +02:00
|
|
|
|
|
|
|
fn new_service(&self, _: &C) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
(self.f)()
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, C, R, S, E> Clone for FnNewServiceNoConfig<F, C, R, S, E>
|
|
|
|
where
|
|
|
|
F: Fn() -> R + Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<F, C, R, S, E> IntoServiceFactory<FnNewServiceNoConfig<F, C, R, S, E>> for F
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
2019-11-14 13:38:24 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-05-12 15:03:50 +02:00
|
|
|
S: Service,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn into_factory(self) -> FnNewServiceNoConfig<F, C, R, S, E> {
|
2019-05-12 15:03:50 +02:00
|
|
|
FnNewServiceNoConfig::new(self)
|
2019-02-22 23:30:00 +01:00
|
|
|
}
|
|
|
|
}
|