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-03-02 21:16:30 +01:00
|
|
|
use crate::{IntoConfigurableNewService, IntoNewService, IntoService, NewService, Service};
|
2018-09-04 18:49:21 +02:00
|
|
|
|
2019-02-22 23:13:48 +01:00
|
|
|
/// Create `NewService` for function that can act as Service
|
2019-02-23 02:12:26 +01:00
|
|
|
pub fn fn_service<F, Req, Out, Cfg>(f: F) -> FnNewService<F, Req, Out, Cfg>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: FnMut(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
|
|
|
FnNewService::new(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `NewService` for function that can produce services
|
2019-03-05 16:35:26 +01:00
|
|
|
pub fn fn_factory<F, R, S, E, Req>(f: F) -> FnNewServiceNoConfig<F, R, S, E, Req>
|
2019-02-22 23:13:48 +01:00
|
|
|
where
|
|
|
|
F: Fn() -> R,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceNoConfig::new(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create `NewService` for function that can produce services with configuration
|
2019-03-05 16:35:26 +01:00
|
|
|
pub fn fn_cfg_factory<F, C, R, S, E, Req>(f: F) -> FnNewServiceConfig<F, C, R, S, E, Req>
|
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>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
FnNewServiceConfig::new(f)
|
|
|
|
}
|
|
|
|
|
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-03-05 16:35:26 +01:00
|
|
|
impl<F, Req, Out> Service<Req> 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-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-03-05 16:35:26 +01:00
|
|
|
impl<F, Req, Out> IntoService<FnService<F, Req, Out>, Req> 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-03-05 16:35:26 +01:00
|
|
|
impl<F, Req, Out, Cfg> NewService<Req, 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-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
|
|
|
|
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 {
|
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())
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 22:08:31 +01:00
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, Req, Out, Cfg> IntoNewService<FnNewService<F, Req, Out, Cfg>, Req, Cfg> for F
|
2019-02-22 23:19:43 +01:00
|
|
|
where
|
|
|
|
F: Fn(Req) -> Out + Clone,
|
|
|
|
Out: IntoFuture,
|
|
|
|
{
|
|
|
|
fn into_new_service(self) -> FnNewService<F, Req, Out, Cfg> {
|
|
|
|
FnNewService::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 22:20:52 +01:00
|
|
|
/// Converter for `Fn() -> Future<Service>` fn
|
2019-03-05 16:35:26 +01:00
|
|
|
pub struct FnNewServiceNoConfig<F, R, S, E, Req>
|
2019-02-22 22:08:31 +01:00
|
|
|
where
|
2019-02-22 23:13:48 +01:00
|
|
|
F: Fn() -> R,
|
2019-02-22 22:08:31 +01:00
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 22:08:31 +01:00
|
|
|
{
|
|
|
|
f: F,
|
2019-03-05 16:35:26 +01:00
|
|
|
_t: PhantomData<Req>,
|
2019-02-22 22:08:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, R, S, E, Req> FnNewServiceNoConfig<F, R, S, E, Req>
|
2019-02-22 22:08:31 +01:00
|
|
|
where
|
2019-02-22 23:13:48 +01:00
|
|
|
F: Fn() -> R,
|
2019-02-22 22:08:31 +01:00
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 22:08:31 +01:00
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
2019-03-05 16:35:26 +01:00
|
|
|
FnNewServiceNoConfig { f, _t: PhantomData }
|
2019-02-22 22:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, R, S, E, Req> NewService<Req, ()> for FnNewServiceNoConfig<F, R, S, E, Req>
|
2019-02-22 22:08:31 +01:00
|
|
|
where
|
2019-02-22 23:13:48 +01:00
|
|
|
F: Fn() -> R,
|
2019-02-22 22:08:31 +01:00
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 22:08:31 +01:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
|
|
|
|
type InitError = E;
|
|
|
|
type Future = R::Future;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
(self.f)().into_future()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, R, S, E, Req> Clone for FnNewServiceNoConfig<F, R, S, E, Req>
|
2019-02-22 22:08:31 +01:00
|
|
|
where
|
|
|
|
F: Fn() -> R + Clone,
|
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 22:08:31 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 22:20:52 +01:00
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, R, S, E, Req> IntoNewService<FnNewServiceNoConfig<F, R, S, E, Req>, Req, ()> for F
|
2019-02-22 22:20:52 +01:00
|
|
|
where
|
2019-02-22 23:13:48 +01:00
|
|
|
F: Fn() -> R,
|
2019-02-22 22:20:52 +01:00
|
|
|
R: IntoFuture<Item = S, Error = E>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 22:20:52 +01:00
|
|
|
{
|
2019-03-05 16:35:26 +01:00
|
|
|
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E, Req> {
|
2019-02-22 22:20:52 +01:00
|
|
|
FnNewServiceNoConfig::new(self)
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 23:13:48 +01:00
|
|
|
|
|
|
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
2019-03-05 16:35:26 +01:00
|
|
|
pub struct FnNewServiceConfig<F, C, R, S, E, Req>
|
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>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
f: F,
|
2019-03-05 16:35:26 +01:00
|
|
|
_t: PhantomData<(C, R, S, E, Req)>,
|
2019-02-22 23:13:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, C, R, S, E, Req> FnNewServiceConfig<F, C, R, S, E, Req>
|
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>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
pub fn new(f: F) -> Self {
|
|
|
|
FnNewServiceConfig { f, _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, C, R, S, E, Req> NewService<Req, C> for FnNewServiceConfig<F, C, R, S, E, Req>
|
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>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = S::Error;
|
|
|
|
type Service = S;
|
|
|
|
|
|
|
|
type InitError = E;
|
2019-03-09 05:50:29 +01:00
|
|
|
type Future = FnNewServiceConfigFut<R, S, E, Req>;
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FnNewServiceConfigFut<R, S, E, Req>
|
|
|
|
where
|
|
|
|
R: IntoFuture<Error = E>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
|
|
|
S: Service<Req>,
|
|
|
|
{
|
|
|
|
fut: R::Future,
|
|
|
|
_t: PhantomData<(S, Req)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R, S, E, Req> Future for FnNewServiceConfigFut<R, S, E, Req>
|
|
|
|
where
|
|
|
|
R: IntoFuture<Error = E>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
|
|
|
S: Service<Req>,
|
|
|
|
{
|
|
|
|
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-05 16:35:26 +01:00
|
|
|
impl<F, C, R, S, E, Req> Clone for FnNewServiceConfig<F, C, R, S, E, Req>
|
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>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:13:48 +01:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self::new(self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:35:26 +01:00
|
|
|
impl<F, C, R, S, E, Req>
|
|
|
|
IntoConfigurableNewService<FnNewServiceConfig<F, C, R, S, E, Req>, Req, C> for F
|
2019-02-22 23:30:00 +01:00
|
|
|
where
|
|
|
|
F: Fn(&C) -> R,
|
2019-03-09 05:50:29 +01:00
|
|
|
R: IntoFuture<Error = E>,
|
|
|
|
R::Item: IntoService<S, Req>,
|
2019-03-05 16:35:26 +01:00
|
|
|
S: Service<Req>,
|
2019-02-22 23:30:00 +01:00
|
|
|
{
|
2019-03-05 16:35:26 +01:00
|
|
|
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E, Req> {
|
2019-02-22 23:30:00 +01:00
|
|
|
FnNewServiceConfig::new(self)
|
|
|
|
}
|
|
|
|
}
|
2019-03-05 16:35:26 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-05 18:49:08 +01:00
|
|
|
use crate::{IntoService, Service, ServiceExt};
|
2019-03-05 16:35:26 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fn_service() {
|
|
|
|
let mut rt = actix_rt::Runtime::new().unwrap();
|
|
|
|
|
2019-03-05 18:49:08 +01:00
|
|
|
let srv = (|_t: &str| -> Result<usize, ()> { Ok(1) }).into_service();
|
2019-03-05 16:35:26 +01:00
|
|
|
let mut srv = srv.and_then(|test: usize| Ok(test));
|
|
|
|
|
|
|
|
let s = "HELLO".to_owned();
|
|
|
|
let res = rt.block_on(srv.call(&s)).unwrap();
|
|
|
|
assert_eq!(res, 1);
|
|
|
|
}
|
|
|
|
}
|