2020-12-27 15:15:42 +01:00
|
|
|
use core::{future::Future, marker::PhantomData, task::Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2020-12-27 19:24:57 +01:00
|
|
|
use crate::{ok, IntoService, IntoServiceFactory, Ready, 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`
|
2019-12-08 14:05:05 +01:00
|
|
|
pub fn fn_service<F, Fut, Req, Res, Err, Cfg>(
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut + Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
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-14 13:38:24 +01:00
|
|
|
/// Create `ServiceFactory` for function that can produce services
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-01-09 15:13:16 +01:00
|
|
|
/// ```
|
2019-12-10 16:11:39 +01:00
|
|
|
/// use std::io;
|
|
|
|
/// use actix_service::{fn_factory, fn_service, Service, ServiceFactory};
|
|
|
|
/// use futures_util::future::ok;
|
|
|
|
///
|
|
|
|
/// /// Service that divides two usize values.
|
|
|
|
/// async fn div((x, y): (usize, usize)) -> Result<usize, io::Error> {
|
|
|
|
/// if y == 0 {
|
2020-08-09 17:10:58 +02:00
|
|
|
/// Err(io::Error::new(io::ErrorKind::Other, "divide by zero"))
|
2019-12-10 16:11:39 +01:00
|
|
|
/// } else {
|
|
|
|
/// Ok(x / y)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// #[actix_rt::main]
|
|
|
|
/// async fn main() -> io::Result<()> {
|
|
|
|
/// // Create service factory that produces `div` services
|
|
|
|
/// let factory = fn_factory(|| {
|
|
|
|
/// ok::<_, io::Error>(fn_service(div))
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// // construct new service
|
2021-01-23 04:06:22 +01:00
|
|
|
/// let srv = factory.new_service(()).await?;
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// // now we can use `div` service
|
|
|
|
/// let result = srv.call((10, 20)).await?;
|
|
|
|
///
|
|
|
|
/// println!("10 / 20 = {}", result);
|
|
|
|
///
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn fn_factory<F, Cfg, Srv, Req, Fut, Err>(
|
|
|
|
f: F,
|
|
|
|
) -> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
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-12-10 16:11:39 +01:00
|
|
|
/// Create `ServiceFactory` for function that accepts config argument and can produce services
|
|
|
|
///
|
|
|
|
/// Any function that has following form `Fn(Config) -> Future<Output = Service>` could
|
|
|
|
/// act as a `ServiceFactory`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2021-01-09 15:13:16 +01:00
|
|
|
/// ```
|
2019-12-10 16:11:39 +01:00
|
|
|
/// use std::io;
|
|
|
|
/// use actix_service::{fn_factory_with_config, fn_service, Service, ServiceFactory};
|
|
|
|
/// use futures_util::future::ok;
|
|
|
|
///
|
|
|
|
/// #[actix_rt::main]
|
|
|
|
/// async fn main() -> io::Result<()> {
|
|
|
|
/// // Create service factory. factory uses config argument for
|
|
|
|
/// // services it generates.
|
|
|
|
/// let factory = fn_factory_with_config(|y: usize| {
|
|
|
|
/// ok::<_, io::Error>(fn_service(move |x: usize| ok::<_, io::Error>(x * y)))
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// // construct new service with config argument
|
2021-01-23 04:06:22 +01:00
|
|
|
/// let srv = factory.new_service(10).await?;
|
2019-12-10 16:11:39 +01:00
|
|
|
///
|
|
|
|
/// let result = srv.call(10).await?;
|
|
|
|
/// assert_eq!(result, 100);
|
|
|
|
///
|
|
|
|
/// println!("10 * 10 = {}", result);
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-12-27 05:28:00 +01:00
|
|
|
pub fn fn_factory_with_config<F, Fut, Cfg, Srv, Req, Err>(
|
2019-12-08 14:05:05 +01:00
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
) -> FnServiceConfig<F, Fut, Cfg, Srv, Req, 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>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Service<Req> for FnService<F, Fut, Req, Res, Err>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
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
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
crate::always_ready!();
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn call(&self, req: Req) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
(self.f)(req)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> IntoService<FnService<F, Fut, Req, Res, Err>, Req> for F
|
2018-09-04 18:57:47 +02:00
|
|
|
where
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut + Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut + Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Req, Res, Err> Service<Req> for FnServiceFactory<F, Fut, Req, Res, Err, ()>
|
2019-12-08 14:05:05 +01:00
|
|
|
where
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut + Clone,
|
2019-12-08 14:05:05 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
{
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type Future = Fut;
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
crate::always_ready!();
|
2019-12-08 14:05:05 +01:00
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
fn call(&self, req: Req) -> Self::Future {
|
2019-12-08 14:05:05 +01:00
|
|
|
(self.f)(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Req, Res, Err, Cfg> ServiceFactory<Req>
|
|
|
|
for FnServiceFactory<F, Fut, Req, Res, Err, Cfg>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2021-01-23 04:06:22 +01:00
|
|
|
F: Fn(Req) -> Fut + Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
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>
|
2020-12-27 05:28:00 +01:00
|
|
|
IntoServiceFactory<FnServiceFactory<F, Fut, Req, Res, Err, Cfg>, Req> for F
|
2019-11-18 15:46:49 +01:00
|
|
|
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
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Req, 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>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
_t: PhantomData<(Fut, Cfg, Req, Srv, Err)>,
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Req, Err> FnServiceConfig<F, Fut, Cfg, Srv, Req, 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>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Req, Err> Clone for FnServiceConfig<F, Fut, Cfg, Srv, Req, Err>
|
2019-12-03 09:05:23 +01:00
|
|
|
where
|
|
|
|
F: Fn(Cfg) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
2019-12-03 09:05:23 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
FnServiceConfig {
|
|
|
|
f: self.f.clone(),
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Fut, Cfg, Srv, Req, Err> ServiceFactory<Req>
|
|
|
|
for FnServiceConfig<F, Fut, Cfg, Srv, Req, 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>>,
|
2020-12-27 05:28:00 +01:00
|
|
|
Srv: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
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
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
|
2019-02-22 23:30:00 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn() -> Fut,
|
|
|
|
Srv: Service<Req>,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
2019-02-22 23:30:00 +01:00
|
|
|
{
|
2019-05-12 15:03:50 +02:00
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
_t: PhantomData<(Cfg, Req)>,
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Cfg, Srv, Req, Fut, Err> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn() -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service<Req>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Cfg, Srv, Req, Fut, Err> ServiceFactory<Req>
|
|
|
|
for FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn() -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service<Req>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Response = Srv::Response;
|
|
|
|
type Error = Srv::Error;
|
|
|
|
type Config = Cfg;
|
2021-01-23 04:06:22 +01:00
|
|
|
type Service = Srv;
|
2020-12-27 05:28:00 +01:00
|
|
|
type InitError = Err;
|
|
|
|
type Future = Fut;
|
|
|
|
|
|
|
|
fn new_service(&self, _: Cfg) -> Self::Future {
|
2019-11-14 13:38:24 +01:00
|
|
|
(self.f)()
|
2019-05-12 15:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Cfg, Srv, Req, Fut, Err> Clone for FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn() -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service<Req>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<F, Cfg, Srv, Req, Fut, Err>
|
|
|
|
IntoServiceFactory<FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err>, Req> for F
|
2019-05-12 15:03:50 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
F: Fn() -> Fut,
|
|
|
|
Fut: Future<Output = Result<Srv, Err>>,
|
|
|
|
Srv: Service<Req>,
|
2019-05-12 15:03:50 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
fn into_factory(self) -> FnServiceNoConfig<F, Cfg, Srv, Req, Fut, Err> {
|
2019-11-18 09:30:04 +01:00
|
|
|
FnServiceNoConfig::new(self)
|
2019-02-22 23:30:00 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-08 14:05:05 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-12-27 15:15:42 +01:00
|
|
|
use core::task::Poll;
|
2019-12-08 14:05:05 +01:00
|
|
|
|
2020-12-27 19:24:57 +01:00
|
|
|
use futures_util::future::lazy;
|
2019-12-08 14:05:05 +01:00
|
|
|
|
|
|
|
use super::*;
|
2020-12-27 19:24:57 +01:00
|
|
|
use crate::{ok, Service, ServiceFactory};
|
2019-12-08 14:05:05 +01:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_fn_service() {
|
|
|
|
let new_srv = fn_service(|()| ok::<_, ()>("srv"));
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = new_srv.new_service(()).await.unwrap();
|
2019-12-08 14:05:05 +01:00
|
|
|
let res = srv.call(()).await;
|
|
|
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), "srv");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_fn_service_service() {
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = fn_service(|()| ok::<_, ()>("srv"));
|
2019-12-08 14:05:05 +01:00
|
|
|
|
|
|
|
let res = srv.call(()).await;
|
|
|
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), "srv");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_fn_service_with_config() {
|
|
|
|
let new_srv = fn_factory_with_config(|cfg: usize| {
|
|
|
|
ok::<_, ()>(fn_service(move |()| ok::<_, ()>(("srv", cfg))))
|
|
|
|
});
|
|
|
|
|
2021-01-23 04:06:22 +01:00
|
|
|
let srv = new_srv.new_service(1).await.unwrap();
|
2019-12-08 14:05:05 +01:00
|
|
|
let res = srv.call(()).await;
|
|
|
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), ("srv", 1));
|
|
|
|
}
|
|
|
|
}
|