1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 22:07:42 +02:00

use owned value for service factory config

This commit is contained in:
Nikolay Kim
2019-12-02 21:27:48 +06:00
parent 3385682e09
commit 9ed35cca7a
30 changed files with 112 additions and 143 deletions

View File

@ -134,6 +134,7 @@ where
impl<A, B> AndThenServiceFactory<A, B>
where
A: ServiceFactory,
A::Config: Clone,
B: ServiceFactory<
Config = A::Config,
Request = A::Response,
@ -142,7 +143,7 @@ where
>,
{
/// Create new `AndThenFactory` combinator
pub fn new(a: A, b: B) -> Self {
pub(crate) fn new(a: A, b: B) -> Self {
Self { a, b }
}
}
@ -150,6 +151,7 @@ where
impl<A, B> ServiceFactory for AndThenServiceFactory<A, B>
where
A: ServiceFactory,
A::Config: Clone,
B: ServiceFactory<
Config = A::Config,
Request = A::Response,
@ -166,8 +168,11 @@ where
type InitError = A::InitError;
type Future = AndThenServiceFactoryResponse<A, B>;
fn new_service(&self, cfg: &A::Config) -> Self::Future {
AndThenServiceFactoryResponse::new(self.a.new_service(cfg), self.b.new_service(cfg))
fn new_service(&self, cfg: A::Config) -> Self::Future {
AndThenServiceFactoryResponse::new(
self.a.new_service(cfg.clone()),
self.b.new_service(cfg),
)
}
}
@ -318,7 +323,7 @@ mod tests {
pipeline_factory(factory_fn(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
.and_then(move || ready(Ok(Srv2(cnt.clone()))));
let mut srv = new_srv.new_service(&()).await.unwrap();
let mut srv = new_srv.new_service(()).await.unwrap();
let res = srv.call("srv1").await;
assert!(res.is_ok());
assert_eq!(res.unwrap(), ("srv1", "srv2"));

View File

@ -117,7 +117,7 @@ where
type InitError = T::InitError;
type Future = ApplyServiceFactoryResponse<T, F, R, In, Out, Err>;
fn new_service(&self, cfg: &T::Config) -> Self::Future {
fn new_service(&self, cfg: T::Config) -> Self::Future {
ApplyServiceFactoryResponse::new(self.service.new_service(cfg), self.f.clone())
}
}
@ -226,7 +226,7 @@ mod tests {
},
));
let mut srv = new_srv.new_service(&()).await.unwrap();
let mut srv = new_srv.new_service(()).await.unwrap();
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));

View File

@ -9,7 +9,7 @@ use crate::{Service, ServiceFactory};
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
pub fn apply_cfg<F, C, T, R, S, E>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
@ -28,8 +28,7 @@ pub fn apply_cfg_factory<F, C, T, R, S>(
f: F,
) -> ApplyConfigServiceFactory<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
@ -45,7 +44,7 @@ where
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService\
pub struct ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
@ -57,7 +56,7 @@ where
impl<F, C, T, R, S, E> Clone for ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
@ -73,7 +72,7 @@ where
impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
F: FnMut(C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>>,
S: Service,
@ -87,7 +86,7 @@ where
type InitError = E;
type Future = R;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: C) -> Self::Future {
unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
}
}
@ -95,8 +94,7 @@ where
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct ApplyConfigServiceFactory<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
@ -108,8 +106,7 @@ where
impl<F, C, T, R, S> Clone for ApplyConfigServiceFactory<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
@ -125,8 +122,7 @@ where
impl<F, C, T, R, S> ServiceFactory for ApplyConfigServiceFactory<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
@ -141,13 +137,13 @@ where
type InitError = T::InitError;
type Future = ApplyConfigServiceFactoryResponse<F, C, T, R, S>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: C) -> Self::Future {
ApplyConfigServiceFactoryResponse {
f: self.f.clone(),
cfg: cfg.clone(),
cfg: Some(cfg),
fut: None,
srv: None,
srv_fut: Some(self.srv.get_ref().new_service(&())),
srv_fut: Some(self.srv.get_ref().new_service(())),
_t: PhantomData,
}
}
@ -156,14 +152,13 @@ where
#[pin_project::pin_project]
pub struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
cfg: C,
cfg: Option<C>,
f: Cell<F>,
srv: Option<T::Service>,
#[pin]
@ -175,8 +170,7 @@ where
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
F: FnMut(C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>>,
@ -205,7 +199,7 @@ where
} else if let Some(srv) = this.srv {
match srv.poll_ready(cx)? {
Poll::Ready(_) => {
let fut = this.f.get_mut()(&this.cfg, srv);
let fut = this.f.get_mut()(this.cfg.take().unwrap(), srv);
this = self.as_mut().project();
this.fut.set(Some(fut));
continue;

View File

@ -69,7 +69,7 @@ where
type Future = BoxFuture<Self::Service, InitErr>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: C) -> Self::Future {
self.0.new_service(cfg)
}
}
@ -104,7 +104,7 @@ where
type Service = BoxService<Req, Res, Err>;
type Future = BoxFuture<Self::Service, Self::InitError>;
fn new_service(&self, cfg: &C) -> Self::Future {
fn new_service(&self, cfg: C) -> Self::Future {
Box::pin(
self.factory
.new_service(cfg)

View File

@ -38,7 +38,7 @@ where
/// Create `ServiceFactory` for function that can produce services with configuration
pub fn factory_fn_cfg<F, Fut, Cfg, Srv, Err>(f: F) -> FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
@ -146,7 +146,7 @@ where
type InitError = ();
type Future = Ready<Result<Self::Service, Self::InitError>>;
fn new_service(&self, _: &Cfg) -> Self::Future {
fn new_service(&self, _: Cfg) -> Self::Future {
ok(FnService::new(self.f.clone()))
}
}
@ -165,7 +165,7 @@ where
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
pub struct FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
@ -175,7 +175,7 @@ where
impl<F, Fut, Cfg, Srv, Err> FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
@ -186,7 +186,7 @@ where
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
F: Fn(Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
@ -199,7 +199,7 @@ where
type InitError = Err;
type Future = Fut;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
fn new_service(&self, cfg: Cfg) -> Self::Future {
(self.f)(cfg)
}
}
@ -240,7 +240,7 @@ where
type InitError = E;
type Future = R;
fn new_service(&self, _: &C) -> Self::Future {
fn new_service(&self, _: C) -> Self::Future {
(self.f)()
}
}

View File

@ -21,7 +21,7 @@ mod transform;
pub use self::apply::{apply_fn, apply_fn_factory};
pub use self::apply_cfg::{apply_cfg, apply_cfg_factory};
pub use self::fn_service::{factory_fn, factory_fn_cfg, service_fn, service_fn2};
pub use self::map_config::{map_config, unit_config, MappedConfig};
pub use self::map_config::{map_config, unit_config};
pub use self::pipeline::{pipeline, pipeline_factory, Pipeline, PipelineFactory};
pub use self::transform::{apply, Transform};
@ -131,7 +131,7 @@ pub trait ServiceFactory {
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
/// Create and return a new service value asynchronously.
fn new_service(&self, cfg: &Self::Config) -> Self::Future;
fn new_service(&self, cfg: Self::Config) -> Self::Future;
/// Map this service's output to a different type, returning a new service
/// of the resulting type.
@ -246,7 +246,7 @@ where
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self, cfg: &S::Config) -> S::Future {
fn new_service(&self, cfg: S::Config) -> S::Future {
self.as_ref().new_service(cfg)
}
}
@ -263,7 +263,7 @@ where
type InitError = S::InitError;
type Future = S::Future;
fn new_service(&self, cfg: &S::Config) -> S::Future {
fn new_service(&self, cfg: S::Config) -> S::Future {
self.as_ref().new_service(cfg)
}
}

View File

@ -151,7 +151,7 @@ where
type InitError = A::InitError;
type Future = MapServiceFuture<A, F, Res>;
fn new_service(&self, cfg: &A::Config) -> Self::Future {
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}

View File

@ -2,16 +2,11 @@ use std::marker::PhantomData;
use super::ServiceFactory;
pub enum MappedConfig<'a, T> {
Ref(&'a T),
Owned(T),
}
/// Adapt external config to a config for provided new service
pub fn map_config<T, F, C>(factory: T, f: F) -> MapConfig<T, F, C>
where
T: ServiceFactory,
F: Fn(&C) -> MappedConfig<T::Config>,
F: Fn(C) -> T::Config,
{
MapConfig::new(factory, f)
}
@ -36,7 +31,7 @@ impl<A, F, C> MapConfig<A, F, C> {
pub(crate) fn new(a: A, f: F) -> Self
where
A: ServiceFactory,
F: Fn(&C) -> MappedConfig<A::Config>,
F: Fn(C) -> A::Config,
{
Self {
a,
@ -63,7 +58,7 @@ where
impl<A, F, C> ServiceFactory for MapConfig<A, F, C>
where
A: ServiceFactory,
F: Fn(&C) -> MappedConfig<A::Config>,
F: Fn(C) -> A::Config,
{
type Request = A::Request;
type Response = A::Response;
@ -74,11 +69,8 @@ where
type InitError = A::InitError;
type Future = A::Future;
fn new_service(&self, cfg: &C) -> Self::Future {
match (self.f)(cfg) {
MappedConfig::Ref(cfg) => self.a.new_service(cfg),
MappedConfig::Owned(cfg) => self.a.new_service(&cfg),
}
fn new_service(&self, cfg: C) -> Self::Future {
self.a.new_service((self.f)(cfg))
}
}
@ -123,7 +115,7 @@ where
type InitError = A::InitError;
type Future = A::Future;
fn new_service(&self, _: &C) -> Self::Future {
self.a.new_service(&())
fn new_service(&self, _: C) -> Self::Future {
self.a.new_service(())
}
}

View File

@ -154,7 +154,7 @@ where
type InitError = A::InitError;
type Future = MapErrServiceFuture<A, F, E>;
fn new_service(&self, cfg: &A::Config) -> Self::Future {
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone())
}
}

View File

@ -55,7 +55,7 @@ where
type InitError = E;
type Future = MapInitErrFuture<A, F, E>;
fn new_service(&self, cfg: &A::Config) -> Self::Future {
fn new_service(&self, cfg: A::Config) -> Self::Future {
MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone())
}
}

View File

@ -145,6 +145,7 @@ impl<T: ServiceFactory> PipelineFactory<T> {
pub fn and_then<F, U>(self, factory: F) -> PipelineFactory<AndThenServiceFactory<T, U>>
where
Self: Sized,
T::Config: Clone,
F: IntoServiceFactory<U>,
U: ServiceFactory<
Config = T::Config,
@ -167,6 +168,7 @@ impl<T: ServiceFactory> PipelineFactory<T> {
pub fn then<F, U>(self, factory: F) -> PipelineFactory<ThenServiceFactory<T, U>>
where
Self: Sized,
T::Config: Clone,
F: IntoServiceFactory<U>,
U: ServiceFactory<
Config = T::Config,
@ -236,7 +238,7 @@ impl<T: ServiceFactory> ServiceFactory for PipelineFactory<T> {
type Future = T::Future;
#[inline]
fn new_service(&self, cfg: &T::Config) -> Self::Future {
fn new_service(&self, cfg: T::Config) -> Self::Future {
self.factory.new_service(cfg)
}
}

View File

@ -129,6 +129,7 @@ pub struct ThenServiceFactory<A, B> {
impl<A, B> ThenServiceFactory<A, B>
where
A: ServiceFactory,
A::Config: Clone,
B: ServiceFactory<
Config = A::Config,
Request = Result<A::Response, A::Error>,
@ -145,6 +146,7 @@ where
impl<A, B> ServiceFactory for ThenServiceFactory<A, B>
where
A: ServiceFactory,
A::Config: Clone,
B: ServiceFactory<
Config = A::Config,
Request = Result<A::Response, A::Error>,
@ -161,8 +163,11 @@ where
type InitError = A::InitError;
type Future = ThenServiceFactoryResponse<A, B>;
fn new_service(&self, cfg: &A::Config) -> Self::Future {
ThenServiceFactoryResponse::new(self.a.new_service(cfg), self.b.new_service(cfg))
fn new_service(&self, cfg: A::Config) -> Self::Future {
ThenServiceFactoryResponse::new(
self.a.new_service(cfg.clone()),
self.b.new_service(cfg),
)
}
}

View File

@ -125,7 +125,7 @@ where
type InitError = T::InitError;
type Future = ApplyTransformFuture<T, S>;
fn new_service(&self, cfg: &S::Config) -> Self::Future {
fn new_service(&self, cfg: S::Config) -> Self::Future {
ApplyTransformFuture {
t_cell: self.t.clone(),
fut_a: self.s.new_service(cfg),