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::task::{Context, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
use futures_util::future::{ok, Ready};
|
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,
|
2019-11-18 09:30:04 +01:00
|
|
|
) -> FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
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-11-18 09:30:04 +01:00
|
|
|
FnServiceFactory::new(f)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub fn service_fn2<F, Fut, Req, Res, Err>(f: F) -> FnService<F, Fut, Req, Res, Err>
|
2019-11-14 13:38:24 +01:00
|
|
|
where
|
|
|
|
F: FnMut(Req) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
FnService::new(f)
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `ServiceFactory` for function that can produce services
|
2019-11-18 09:30:04 +01:00
|
|
|
pub fn factory_fn<F, Cfg, Srv, Fut, Err>(f: F) -> FnServiceNoConfig<F, Cfg, Srv, Fut, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-11-18 09:30:04 +01:00
|
|
|
Srv: Service,
|
2019-11-14 13:38:24 +01:00
|
|
|
F: Fn() -> Fut,
|
2019-11-18 09:30:04 +01:00
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
FnServiceNoConfig::new(f)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
/// Create `ServiceFactory` for function that can produce services with configuration
|
2019-11-18 09:30:04 +01:00
|
|
|
pub fn factory_fn_cfg<F, Fut, Cfg, Srv, Err>(f: F) -> FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(Cfg) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
FnServiceConfig::new(f)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct FnService<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-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> FnService<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 {
|
2019-11-18 09:30:04 +01:00
|
|
|
Self { f, _t: PhantomData }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Clone for FnService<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-11-18 09:30:04 +01:00
|
|
|
Self::new(self.f.clone())
|
2018-08-28 05:32:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Service for FnService<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-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> IntoService<FnService<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-18 09:30:04 +01:00
|
|
|
fn into_service(self) -> FnService<F, Fut, Req, Res, Err> {
|
|
|
|
FnService::new(self)
|
2018-09-04 18:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct FnServiceFactory<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-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> FnServiceFactory<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-11-18 09:30:04 +01:00
|
|
|
FnServiceFactory { f, _t: PhantomData }
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> Clone for FnServiceFactory<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 {
|
2019-11-18 09:30:04 +01:00
|
|
|
Self::new(self.f.clone())
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory for FnServiceFactory<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-18 09:30:04 +01:00
|
|
|
type Service = FnService<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-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, _: Cfg) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
ok(FnService::new(self.f.clone()))
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:46:49 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg>
|
|
|
|
IntoServiceFactory<FnServiceFactory<F, Fut, Req, Res, Err, Cfg>> for F
|
|
|
|
where
|
|
|
|
F: Fn(Req) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
{
|
|
|
|
fn into_factory(self) -> FnServiceFactory<F, Fut, Req, Res, Err, Cfg> {
|
|
|
|
FnServiceFactory::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:13:48 +01:00
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(Cfg) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
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-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Err> FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(Cfg) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
fn new(f: F) -> Self {
|
|
|
|
FnServiceConfig { f, _t: PhantomData }
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 09:05:23 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Err> Clone for FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
|
|
|
where
|
|
|
|
F: Fn(Cfg) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
FnServiceConfig {
|
|
|
|
f: self.f.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2019-12-02 16:27:48 +01:00
|
|
|
F: Fn(Cfg) -> Fut,
|
2019-11-19 09:51:40 +01:00
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
2019-11-14 13:38:24 +01:00
|
|
|
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;
|
2019-11-19 09:51:40 +01:00
|
|
|
type Future = Fut;
|
2019-02-22 23:13:48 +01:00
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
2019-11-19 09:51:40 +01:00
|
|
|
(self.f)(cfg)
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
/// Converter for `Fn() -> Future<Service>` fn
|
2019-11-18 09:30:04 +01:00
|
|
|
pub struct FnServiceNoConfig<F, C, S, R, E>
|
2019-02-22 23:30:00 +01:00
|
|
|
where
|
2019-05-12 15:03:50 +02:00
|
|
|
F: Fn() -> R,
|
2019-03-09 15:36:23 +01:00
|
|
|
S: Service,
|
2019-11-18 09:30:04 +01:00
|
|
|
R: Future<Output = Result<S, E>>,
|
2019-02-22 23:30:00 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
f: F,
|
|
|
|
_t: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, S, R, E> FnServiceNoConfig<F, C, S, R, 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,
|
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
fn new(f: F) -> Self {
|
2019-11-18 09:30:04 +01:00
|
|
|
Self { f, _t: PhantomData }
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, S, R, E> ServiceFactory for FnServiceNoConfig<F, C, S, R, 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
|
|
|
|
2019-12-02 16:27:48 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<F, C, S, R, E> Clone for FnServiceNoConfig<F, C, S, R, E>
|
2019-05-12 15:03:50 +02:00
|
|
|
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-18 09:30:04 +01:00
|
|
|
impl<F, C, S, R, E> IntoServiceFactory<FnServiceNoConfig<F, C, S, R, 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-18 09:30:04 +01:00
|
|
|
fn into_factory(self) -> FnServiceNoConfig<F, C, S, R, E> {
|
|
|
|
FnServiceNoConfig::new(self)
|
2019-02-22 23:30:00 +01:00
|
|
|
}
|
|
|
|
}
|