mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-30 17:44:34 +01:00
add fn service helpers
This commit is contained in:
parent
a0e2d926e6
commit
d2bd9134aa
@ -4,8 +4,8 @@ use std::net::{IpAddr, SocketAddr};
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
use actix_service::{NewService, Service};
|
use actix_service::{fn_factory, NewService, Service};
|
||||||
use futures::future::{ok, Either, FutureResult};
|
use futures::future::{ok, Either};
|
||||||
use futures::{try_ready, Async, Future, Poll};
|
use futures::{try_ready, Async, Future, Poll};
|
||||||
use tokio_tcp::{ConnectFuture, TcpStream};
|
use tokio_tcp::{ConnectFuture, TcpStream};
|
||||||
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
|
||||||
@ -183,7 +183,7 @@ impl Connector {
|
|||||||
Error = ConnectorError,
|
Error = ConnectorError,
|
||||||
InitError = E,
|
InitError = E,
|
||||||
> + Clone {
|
> + Clone {
|
||||||
move |_: &()| -> FutureResult<Connector, E> { ok(Connector::new(cfg.clone(), opts)) }
|
fn_factory(move || ok(Connector::new(cfg.clone(), opts)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-service"
|
name = "actix-service"
|
||||||
version = "0.3.0"
|
version = "0.2.2"
|
||||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||||
description = "Actix Service"
|
description = "Actix Service"
|
||||||
keywords = ["network", "framework", "async", "futures"]
|
keywords = ["network", "framework", "async", "futures"]
|
||||||
|
@ -5,6 +5,35 @@ use futures::{Async, IntoFuture, Poll};
|
|||||||
|
|
||||||
use super::{IntoNewService, IntoService, NewService, Service};
|
use super::{IntoNewService, IntoService, NewService, Service};
|
||||||
|
|
||||||
|
/// Create `NewService` for function that can act as Service
|
||||||
|
pub fn fn_service<F, Req, Out>(f: F) -> FnNewService<F, Req, Out, ()>
|
||||||
|
where
|
||||||
|
F: FnMut(Req) -> Out + Clone,
|
||||||
|
Out: IntoFuture,
|
||||||
|
{
|
||||||
|
FnNewService::new(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `NewService` for function that can produce services
|
||||||
|
pub fn fn_factory<F, R, S, E>(f: F) -> FnNewServiceNoConfig<F, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn() -> R,
|
||||||
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
FnNewServiceNoConfig::new(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create `NewService` for function that can produce services with configuration
|
||||||
|
pub fn fn_cfg_factory<F, C, R, S, E>(f: F) -> FnNewServiceConfig<F, C, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> R,
|
||||||
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
FnNewServiceConfig::new(f)
|
||||||
|
}
|
||||||
|
|
||||||
pub struct FnService<F, Req, Out>
|
pub struct FnService<F, Req, Out>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Out,
|
||||||
@ -66,15 +95,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `NewService` for function that can act as Service
|
|
||||||
pub fn fn_nservice<F, Req, Out>(f: F) -> FnNewService<F, Req, Out, ()>
|
|
||||||
where
|
|
||||||
F: FnMut(Req) -> Out + Clone,
|
|
||||||
Out: IntoFuture,
|
|
||||||
{
|
|
||||||
FnNewService::new(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FnNewService<F, Req, Out, Cfg>
|
pub struct FnNewService<F, Req, Out, Cfg>
|
||||||
where
|
where
|
||||||
F: FnMut(Req) -> Out,
|
F: FnMut(Req) -> Out,
|
||||||
@ -125,7 +145,7 @@ where
|
|||||||
/// Converter for `Fn() -> Future<Service>` fn
|
/// Converter for `Fn() -> Future<Service>` fn
|
||||||
pub struct FnNewServiceNoConfig<F, R, S, E>
|
pub struct FnNewServiceNoConfig<F, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -134,7 +154,7 @@ where
|
|||||||
|
|
||||||
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
|
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -145,7 +165,7 @@ where
|
|||||||
|
|
||||||
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
|
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -175,7 +195,7 @@ where
|
|||||||
|
|
||||||
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
|
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
|
||||||
where
|
where
|
||||||
F: Fn() -> R + Clone,
|
F: Fn() -> R,
|
||||||
R: IntoFuture<Item = S, Error = E>,
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
S: Service,
|
S: Service,
|
||||||
{
|
{
|
||||||
@ -183,3 +203,66 @@ where
|
|||||||
FnNewServiceNoConfig::new(self)
|
FnNewServiceNoConfig::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||||
|
pub struct FnNewServiceConfig<F, C, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> R,
|
||||||
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
f: F,
|
||||||
|
_t: PhantomData<(C, R, S, E)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, C, R, S, E> FnNewServiceConfig<F, C, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> R,
|
||||||
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
pub fn new(f: F) -> Self {
|
||||||
|
FnNewServiceConfig { f, _t: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, C, R, S, E> NewService<C> for FnNewServiceConfig<F, C, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> 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 InitError = E;
|
||||||
|
type Future = R::Future;
|
||||||
|
|
||||||
|
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||||
|
(self.f)(cfg).into_future()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F, C, R, S, E> Clone for FnNewServiceConfig<F, C, R, S, E>
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> 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<FnNewServiceConfig<F, C, R, S, E>, C> for F
|
||||||
|
where
|
||||||
|
F: Fn(&C) -> R,
|
||||||
|
R: IntoFuture<Item = S, Error = E>,
|
||||||
|
S: Service,
|
||||||
|
{
|
||||||
|
fn into_new_service(self) -> FnNewServiceConfig<F, C, R, S, E> {
|
||||||
|
FnNewServiceConfig::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,7 +24,7 @@ use self::and_then_apply::{AndThenTransform, AndThenTransformNewService};
|
|||||||
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
use self::and_then_apply_fn::{AndThenApply, AndThenApplyNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{Apply, ApplyNewService};
|
||||||
pub use self::blank::{Blank, BlankNewService};
|
pub use self::blank::{Blank, BlankNewService};
|
||||||
pub use self::fn_service::{fn_nservice, FnNewService, FnService};
|
pub use self::fn_service::{fn_cfg_factory, fn_factory, fn_service, FnService};
|
||||||
pub use self::fn_transform::{FnNewTransform, FnTransform};
|
pub use self::fn_transform::{FnNewTransform, FnTransform};
|
||||||
pub use self::from_err::{FromErr, FromErrNewService};
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
pub use self::map::{Map, MapNewService};
|
pub use self::map::{Map, MapNewService};
|
||||||
@ -370,24 +370,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, R, E, S, C> NewService<C> for F
|
|
||||||
where
|
|
||||||
F: Fn(&C) -> 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 InitError = E;
|
|
||||||
type Future = R::Future;
|
|
||||||
|
|
||||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
|
||||||
(*self)(cfg).into_future()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S, C> NewService<C> for Rc<S>
|
impl<S, C> NewService<C> for Rc<S>
|
||||||
where
|
where
|
||||||
S: NewService<C>,
|
S: NewService<C>,
|
||||||
|
Loading…
Reference in New Issue
Block a user