From 9ed35cca7abbe0cb216333958a6614db058f58d3 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 2 Dec 2019 21:27:48 +0600 Subject: [PATCH] use owned value for service factory config --- actix-connect/src/connector.rs | 2 +- actix-connect/src/resolver.rs | 2 +- actix-connect/src/service.rs | 2 +- actix-connect/src/ssl/openssl.rs | 4 +-- actix-connect/tests/test_connect.rs | 2 +- actix-ioframe/src/dispatcher.rs | 22 ++++++++++------- actix-ioframe/src/item.rs | 22 ++++++----------- actix-ioframe/src/lib.rs | 2 -- actix-ioframe/src/service.rs | 22 ++++++++++------- actix-ioframe/src/state.rs | 30 ----------------------- actix-ioframe/tests/test_server.rs | 1 + actix-server/src/config.rs | 6 ++--- actix-server/src/service.rs | 2 +- actix-service/src/and_then.rs | 13 +++++++--- actix-service/src/apply.rs | 4 +-- actix-service/src/apply_cfg.rs | 38 ++++++++++++----------------- actix-service/src/boxed.rs | 4 +-- actix-service/src/fn_service.rs | 14 +++++------ actix-service/src/lib.rs | 8 +++--- actix-service/src/map.rs | 2 +- actix-service/src/map_config.rs | 22 ++++++----------- actix-service/src/map_err.rs | 2 +- actix-service/src/map_init_err.rs | 2 +- actix-service/src/pipeline.rs | 4 ++- actix-service/src/then.rs | 9 +++++-- actix-service/src/transform.rs | 2 +- actix-tls/src/openssl.rs | 2 +- actix-utils/src/either.rs | 6 +++-- actix-utils/src/keepalive.rs | 2 +- actix-utils/src/time.rs | 2 +- 30 files changed, 112 insertions(+), 143 deletions(-) delete mode 100644 actix-ioframe/src/state.rs diff --git a/actix-connect/src/connector.rs b/actix-connect/src/connector.rs index fa78b8ac..c0cf6ab8 100644 --- a/actix-connect/src/connector.rs +++ b/actix-connect/src/connector.rs @@ -49,7 +49,7 @@ impl ServiceFactory for TcpConnectorFactory { type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(self.service()) } } diff --git a/actix-connect/src/resolver.rs b/actix-connect/src/resolver.rs index 58349817..fbadcc94 100644 --- a/actix-connect/src/resolver.rs +++ b/actix-connect/src/resolver.rs @@ -63,7 +63,7 @@ impl ServiceFactory for ResolverFactory { type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(self.service()) } } diff --git a/actix-connect/src/service.rs b/actix-connect/src/service.rs index e613d909..4ea8dd88 100644 --- a/actix-connect/src/service.rs +++ b/actix-connect/src/service.rs @@ -79,7 +79,7 @@ impl ServiceFactory for ConnectServiceFactory { type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(self.service()) } } diff --git a/actix-connect/src/ssl/openssl.rs b/actix-connect/src/ssl/openssl.rs index bae1cb56..83a4a9b3 100644 --- a/actix-connect/src/ssl/openssl.rs +++ b/actix-connect/src/ssl/openssl.rs @@ -66,7 +66,7 @@ where type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(OpensslConnectorService { connector: self.connector.clone(), _t: PhantomData, @@ -201,7 +201,7 @@ impl ServiceFactory for OpensslConnectServiceFactory { type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(self.service()) } } diff --git a/actix-connect/tests/test_connect.rs b/actix-connect/tests/test_connect.rs index 6e052fdd..41ebd7a5 100644 --- a/actix-connect/tests/test_connect.rs +++ b/actix-connect/tests/test_connect.rs @@ -89,7 +89,7 @@ async fn test_new_service() { let factory = actix_connect::new_connector_factory(resolver); - let mut conn = factory.new_service(&()).await.unwrap(); + let mut conn = factory.new_service(()).await.unwrap(); let con = conn.call(Connect::with("10", srv.addr())).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); } diff --git a/actix-ioframe/src/dispatcher.rs b/actix-ioframe/src/dispatcher.rs index 677c2816..ae8a3298 100644 --- a/actix-ioframe/src/dispatcher.rs +++ b/actix-ioframe/src/dispatcher.rs @@ -17,7 +17,6 @@ use crate::cell::Cell; use crate::error::ServiceError; use crate::item::Item; use crate::sink::Sink; -use crate::state::State; type Request = Item; type Response = ::Item; @@ -33,6 +32,7 @@ pub(crate) enum FramedMessage { #[pin_project::pin_project] pub(crate) struct FramedDispatcher where + St: Clone, S: Service, Response = Option>>, S::Error: 'static, S::Future: 'static, @@ -43,7 +43,7 @@ where { service: S, sink: Sink<::Item>, - state: State, + state: St, dispatch_state: FramedState, framed: Framed, rx: Option::Item>>>, @@ -53,6 +53,7 @@ where impl FramedDispatcher where + St: Clone, S: Service, Response = Option>>, S::Error: 'static, S::Future: 'static, @@ -63,7 +64,7 @@ where { pub(crate) fn new>( framed: Framed, - state: State, + state: St, service: F, rx: mpsc::Receiver::Item>>, sink: Sink<::Item>, @@ -124,6 +125,7 @@ struct FramedDispatcherInner { impl FramedDispatcher where + St: Clone, S: Service, Response = Option>>, S::Error: 'static, S::Future: 'static, @@ -156,7 +158,7 @@ where fn poll( cx: &mut Context, srv: &mut S, - state: &mut State, + state: &mut St, sink: &mut Sink<::Item>, framed: &mut Framed, dispatch_state: &mut FramedState, @@ -165,6 +167,7 @@ fn poll( disconnect: &mut Option>, ) -> Poll>> where + St: Clone, S: Service, Response = Option>>, S::Error: 'static, S::Future: 'static, @@ -199,7 +202,7 @@ where || framed.is_write_buf_empty()) { if let Some(ref disconnect) = disconnect { - (&*disconnect)(&mut *state.get_mut(), true); + (&*disconnect)(&mut *state, true); } Poll::Ready(Err(err)) } else { @@ -224,19 +227,19 @@ where let _ = tx.send(()); } if let Some(ref disconnect) = disconnect { - (&*disconnect)(&mut *state.get_mut(), false); + (&*disconnect)(&mut *state, false); } Poll::Ready(Ok(())) } FramedState::FramedError(err) => { if let Some(ref disconnect) = disconnect { - (&*disconnect)(&mut *state.get_mut(), true); + (&*disconnect)(&mut *state, true); } Poll::Ready(Err(err)) } FramedState::Stopping => { if let Some(ref disconnect) = disconnect { - (&*disconnect)(&mut *state.get_mut(), false); + (&*disconnect)(&mut *state, false); } Poll::Ready(Ok(())) } @@ -246,13 +249,14 @@ where fn poll_read( cx: &mut Context, srv: &mut S, - state: &mut State, + state: &mut St, sink: &mut Sink<::Item>, framed: &mut Framed, dispatch_state: &mut FramedState, inner: &mut Cell::Item, S::Error>>, ) -> bool where + St: Clone, S: Service, Response = Option>>, S::Error: 'static, S::Future: 'static, diff --git a/actix-ioframe/src/item.rs b/actix-ioframe/src/item.rs index b8d4ae31..ac78d5aa 100644 --- a/actix-ioframe/src/item.rs +++ b/actix-ioframe/src/item.rs @@ -1,14 +1,12 @@ -use std::cell::{Ref, RefMut}; use std::fmt; use std::ops::{Deref, DerefMut}; use actix_codec::{Decoder, Encoder}; use crate::sink::Sink; -use crate::state::State; pub struct Item { - state: State, + state: St, sink: Sink<::Item>, item: ::Item, } @@ -18,7 +16,7 @@ where Codec: Encoder + Decoder, { pub(crate) fn new( - state: State, + state: St, sink: Sink<::Item>, item: ::Item, ) -> Self { @@ -26,13 +24,13 @@ where } #[inline] - pub fn state(&self) -> Ref { - self.state.get_ref() + pub fn state(&self) -> &St { + &self.state } #[inline] - pub fn state_mut(&mut self) -> RefMut { - self.state.get_mut() + pub fn state_mut(&mut self) -> &mut St { + &mut self.state } #[inline] @@ -46,13 +44,7 @@ where } #[inline] - pub fn into_parts( - self, - ) -> ( - State, - Sink<::Item>, - ::Item, - ) { + pub fn into_parts(self) -> (St, Sink<::Item>, ::Item) { (self.state, self.sink, self.item) } } diff --git a/actix-ioframe/src/lib.rs b/actix-ioframe/src/lib.rs index 5e25be2c..fabfaa8d 100644 --- a/actix-ioframe/src/lib.rs +++ b/actix-ioframe/src/lib.rs @@ -5,11 +5,9 @@ mod error; mod item; mod service; mod sink; -mod state; pub use self::connect::{Connect, ConnectResult}; pub use self::error::ServiceError; pub use self::item::Item; pub use self::service::{Builder, NewServiceBuilder, ServiceBuilder}; pub use self::sink::Sink; -pub use self::state::State; diff --git a/actix-ioframe/src/service.rs b/actix-ioframe/src/service.rs index 0700f8b3..b915d0bf 100644 --- a/actix-ioframe/src/service.rs +++ b/actix-ioframe/src/service.rs @@ -14,7 +14,6 @@ use crate::connect::{Connect, ConnectResult}; use crate::dispatcher::FramedDispatcher; use crate::error::ServiceError; use crate::item::Item; -use crate::state::State; type RequestItem = Item; type ResponseItem = Option<::Item>; @@ -23,7 +22,7 @@ type ResponseItem = Option<::Item>; /// for building instances for framed services. pub struct Builder(PhantomData<(St, Codec)>); -impl Builder { +impl Builder { pub fn new() -> Builder { Builder(PhantomData) } @@ -73,7 +72,7 @@ pub struct ServiceBuilder { impl ServiceBuilder where - St: 'static, + St: Clone, C: Service, Response = ConnectResult>, C::Error: 'static, Io: AsyncRead + AsyncWrite, @@ -121,7 +120,7 @@ pub struct NewServiceBuilder { impl NewServiceBuilder where - St: 'static, + St: Clone, Io: AsyncRead + AsyncWrite, C: ServiceFactory< Config = (), @@ -174,7 +173,7 @@ pub struct FramedService { impl ServiceFactory for FramedService where - St: 'static, + St: Clone + 'static, Io: AsyncRead + AsyncWrite, C: ServiceFactory< Config = (), @@ -203,13 +202,13 @@ where type Service = FramedServiceImpl; type Future = LocalBoxFuture<'static, Result>; - fn new_service(&self, _: &Cfg) -> Self::Future { + fn new_service(&self, _: Cfg) -> Self::Future { let handler = self.handler.clone(); let disconnect = self.disconnect.clone(); // create connect service and then create service impl self.connect - .new_service(&()) + .new_service(()) .map(move |result| { result.map(move |connect| FramedServiceImpl { connect, @@ -231,6 +230,7 @@ pub struct FramedServiceImpl { impl Service for FramedServiceImpl where + St: Clone, Io: AsyncRead + AsyncWrite, C: Service, Response = ConnectResult>, C::Error: 'static, @@ -269,6 +269,7 @@ where #[pin_project::pin_project] pub struct FramedServiceImplResponse where + St: Clone, C: Service, Response = ConnectResult>, C::Error: 'static, T: ServiceFactory< @@ -290,6 +291,7 @@ where impl Future for FramedServiceImplResponse where + St: Clone, C: Service, Response = ConnectResult>, C::Error: 'static, T: ServiceFactory< @@ -325,6 +327,7 @@ where #[pin_project::pin_project] enum FramedServiceImplResponseInner where + St: Clone, C: Service, Response = ConnectResult>, C::Error: 'static, T: ServiceFactory< @@ -351,6 +354,7 @@ where impl FramedServiceImplResponseInner where + St: Clone, C: Service, Response = ConnectResult>, C::Error: 'static, T: ServiceFactory< @@ -380,7 +384,7 @@ where match fut.poll(cx) { Poll::Ready(Ok(res)) => { Either::Left(FramedServiceImplResponseInner::Handler( - handler.new_service(&res.state), + handler.new_service(res.state.clone()), Some(res), disconnect.take(), )) @@ -396,7 +400,7 @@ where Either::Left(FramedServiceImplResponseInner::Dispatcher( FramedDispatcher::new( res.framed, - State::new(res.state), + res.state, handler, res.rx, res.sink, diff --git a/actix-ioframe/src/state.rs b/actix-ioframe/src/state.rs deleted file mode 100644 index 7349b864..00000000 --- a/actix-ioframe/src/state.rs +++ /dev/null @@ -1,30 +0,0 @@ -use std::cell::{Ref, RefCell, RefMut}; -use std::rc::Rc; - -/// Connection state -/// -/// Connection state is an arbitrary data attached to the each incoming message. -#[derive(Debug)] -pub struct State(Rc>); - -impl State { - pub(crate) fn new(st: T) -> Self { - State(Rc::new(RefCell::new(st))) - } - - #[inline] - pub fn get_ref(&self) -> Ref { - self.0.borrow() - } - - #[inline] - pub fn get_mut(&mut self) -> RefMut { - self.0.borrow_mut() - } -} - -impl Clone for State { - fn clone(&self) -> Self { - State(self.0.clone()) - } -} diff --git a/actix-ioframe/tests/test_server.rs b/actix-ioframe/tests/test_server.rs index e8992314..7a0e55c4 100644 --- a/actix-ioframe/tests/test_server.rs +++ b/actix-ioframe/tests/test_server.rs @@ -10,6 +10,7 @@ use futures::future::ok; use actix_ioframe::{Builder, Connect}; +#[derive(Clone)] struct State; #[actix_rt::test] diff --git a/actix-server/src/config.rs b/actix-server/src/config.rs index 1ab6b9da..999a712d 100644 --- a/actix-server/src/config.rs +++ b/actix-server/src/config.rs @@ -121,7 +121,7 @@ impl InternalServiceFactory for ConfiguredService { } let mut res = vec![]; for (token, ns) in services.into_iter() { - let newserv = ns.new_service(&()); + let newserv = ns.new_service(()); match newserv.await { Ok(serv) => { res.push((token, serv)); @@ -250,8 +250,8 @@ where type Service = BoxedServerService; type Future = LocalBoxFuture<'static, Result>; - fn new_service(&self, cfg: &()) -> Self::Future { - let fut = self.inner.new_service(cfg); + fn new_service(&self, _: ()) -> Self::Future { + let fut = self.inner.new_service(()); async move { return match fut.await { Ok(s) => Ok(Box::new(StreamService::new(s)) as BoxedServerService), diff --git a/actix-server/src/service.rs b/actix-server/src/service.rs index e883c375..3729689d 100644 --- a/actix-server/src/service.rs +++ b/actix-server/src/service.rs @@ -150,7 +150,7 @@ where let token = self.token; self.inner .create() - .new_service(&()) + .new_service(()) .map_err(|_| ()) .map_ok(move |inner| { let service: BoxedServerService = Box::new(StreamService::new(inner)); diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index 3e148040..a22acde3 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -134,6 +134,7 @@ where impl AndThenServiceFactory 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 ServiceFactory for AndThenServiceFactory 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; - 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")); diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index fdb6aac0..9d87cb55 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -117,7 +117,7 @@ where type InitError = T::InitError; type Future = ApplyServiceFactoryResponse; - 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(()))); diff --git a/actix-service/src/apply_cfg.rs b/actix-service/src/apply_cfg.rs index 7e461567..a0567761 100644 --- a/actix-service/src/apply_cfg.rs +++ b/actix-service/src/apply_cfg.rs @@ -9,7 +9,7 @@ use crate::{Service, ServiceFactory}; /// Convert `Fn(&Config, &mut Service) -> Future` fn to a NewService pub fn apply_cfg(srv: T, f: F) -> ApplyConfigService where - F: FnMut(&C, &mut T) -> R, + F: FnMut(C, &mut T) -> R, T: Service, R: Future>, S: Service, @@ -28,8 +28,7 @@ pub fn apply_cfg_factory( f: F, ) -> ApplyConfigServiceFactory where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, T::InitError: From, R: Future>, @@ -45,7 +44,7 @@ where /// Convert `Fn(&Config) -> Future` fn to NewService\ pub struct ApplyConfigService where - F: FnMut(&C, &mut T) -> R, + F: FnMut(C, &mut T) -> R, T: Service, R: Future>, S: Service, @@ -57,7 +56,7 @@ where impl Clone for ApplyConfigService where - F: FnMut(&C, &mut T) -> R, + F: FnMut(C, &mut T) -> R, T: Service, R: Future>, S: Service, @@ -73,7 +72,7 @@ where impl ServiceFactory for ApplyConfigService where - F: FnMut(&C, &mut T) -> R, + F: FnMut(C, &mut T) -> R, T: Service, R: Future>, 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` fn to NewService pub struct ApplyConfigServiceFactory where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, R: Future>, S: Service, @@ -108,8 +106,7 @@ where impl Clone for ApplyConfigServiceFactory where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, R: Future>, S: Service, @@ -125,8 +122,7 @@ where impl ServiceFactory for ApplyConfigServiceFactory where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, T::InitError: From, R: Future>, @@ -141,13 +137,13 @@ where type InitError = T::InitError; type Future = ApplyConfigServiceFactoryResponse; - 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 where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, T::InitError: From, R: Future>, S: Service, { - cfg: C, + cfg: Option, f: Cell, srv: Option, #[pin] @@ -175,8 +170,7 @@ where impl Future for ApplyConfigServiceFactoryResponse where - C: Clone, - F: FnMut(&C, &mut T::Service) -> R, + F: FnMut(C, &mut T::Service) -> R, T: ServiceFactory, T::InitError: From, R: Future>, @@ -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; diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index 13c60a73..b3df048b 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -69,7 +69,7 @@ where type Future = BoxFuture; - 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; type Future = BoxFuture; - fn new_service(&self, cfg: &C) -> Self::Future { + fn new_service(&self, cfg: C) -> Self::Future { Box::pin( self.factory .new_service(cfg) diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 7737b77b..33b9c720 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -38,7 +38,7 @@ where /// Create `ServiceFactory` for function that can produce services with configuration pub fn factory_fn_cfg(f: F) -> FnServiceConfig where - F: Fn(&Cfg) -> Fut, + F: Fn(Cfg) -> Fut, Fut: Future>, Srv: Service, { @@ -146,7 +146,7 @@ where type InitError = (); type Future = Ready>; - 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` fn to NewService pub struct FnServiceConfig where - F: Fn(&Cfg) -> Fut, + F: Fn(Cfg) -> Fut, Fut: Future>, Srv: Service, { @@ -175,7 +175,7 @@ where impl FnServiceConfig where - F: Fn(&Cfg) -> Fut, + F: Fn(Cfg) -> Fut, Fut: Future>, Srv: Service, { @@ -186,7 +186,7 @@ where impl ServiceFactory for FnServiceConfig where - F: Fn(&Cfg) -> Fut, + F: Fn(Cfg) -> Fut, Fut: Future>, 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)() } } diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 84410534..619c78ee 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -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>; /// 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) } } diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index 2f55a38a..f7275e94 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -151,7 +151,7 @@ where type InitError = A::InitError; type Future = MapServiceFuture; - 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()) } } diff --git a/actix-service/src/map_config.rs b/actix-service/src/map_config.rs index 101a5eaf..a2997f54 100644 --- a/actix-service/src/map_config.rs +++ b/actix-service/src/map_config.rs @@ -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(factory: T, f: F) -> MapConfig where T: ServiceFactory, - F: Fn(&C) -> MappedConfig, + F: Fn(C) -> T::Config, { MapConfig::new(factory, f) } @@ -36,7 +31,7 @@ impl MapConfig { pub(crate) fn new(a: A, f: F) -> Self where A: ServiceFactory, - F: Fn(&C) -> MappedConfig, + F: Fn(C) -> A::Config, { Self { a, @@ -63,7 +58,7 @@ where impl ServiceFactory for MapConfig where A: ServiceFactory, - F: Fn(&C) -> MappedConfig, + 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(()) } } diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index 87ccd32f..b7696704 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -154,7 +154,7 @@ where type InitError = A::InitError; type Future = MapErrServiceFuture; - 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()) } } diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index 04c89ed8..b1eec072 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -55,7 +55,7 @@ where type InitError = E; type Future = MapInitErrFuture; - 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()) } } diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index d28ca51d..6e7e945b 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -145,6 +145,7 @@ impl PipelineFactory { pub fn and_then(self, factory: F) -> PipelineFactory> where Self: Sized, + T::Config: Clone, F: IntoServiceFactory, U: ServiceFactory< Config = T::Config, @@ -167,6 +168,7 @@ impl PipelineFactory { pub fn then(self, factory: F) -> PipelineFactory> where Self: Sized, + T::Config: Clone, F: IntoServiceFactory, U: ServiceFactory< Config = T::Config, @@ -236,7 +238,7 @@ impl ServiceFactory for PipelineFactory { 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) } } diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index b32bd168..85293f58 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -129,6 +129,7 @@ pub struct ThenServiceFactory { impl ThenServiceFactory where A: ServiceFactory, + A::Config: Clone, B: ServiceFactory< Config = A::Config, Request = Result, @@ -145,6 +146,7 @@ where impl ServiceFactory for ThenServiceFactory where A: ServiceFactory, + A::Config: Clone, B: ServiceFactory< Config = A::Config, Request = Result, @@ -161,8 +163,11 @@ where type InitError = A::InitError; type Future = ThenServiceFactoryResponse; - 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), + ) } } diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index e4b790eb..e431faf7 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -125,7 +125,7 @@ where type InitError = T::InitError; type Future = ApplyTransformFuture; - 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), diff --git a/actix-tls/src/openssl.rs b/actix-tls/src/openssl.rs index 8e6641fd..90160417 100644 --- a/actix-tls/src/openssl.rs +++ b/actix-tls/src/openssl.rs @@ -49,7 +49,7 @@ impl ServiceFactory for Acceptor type InitError = (); type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { MAX_CONN_COUNTER.with(|conns| { ok(AcceptorService { acceptor: self.acceptor.clone(), diff --git a/actix-utils/src/either.rs b/actix-utils/src/either.rs index 21075526..2f1b2ec5 100644 --- a/actix-utils/src/either.rs +++ b/actix-utils/src/either.rs @@ -63,6 +63,7 @@ impl Either { pub fn new(left: A, right: B) -> Either where A: ServiceFactory, + A::Config: Clone, B: ServiceFactory< Config = A::Config, Response = A::Response, @@ -77,6 +78,7 @@ impl Either { impl ServiceFactory for Either where A: ServiceFactory, + A::Config: Clone, B: ServiceFactory< Config = A::Config, Response = A::Response, @@ -92,11 +94,11 @@ where type Service = EitherService; type Future = EitherNewService; - fn new_service(&self, cfg: &A::Config) -> Self::Future { + fn new_service(&self, cfg: A::Config) -> Self::Future { EitherNewService { left: None, right: None, - left_fut: self.left.new_service(cfg), + left_fut: self.left.new_service(cfg.clone()), right_fut: self.right.new_service(cfg), } } diff --git a/actix-utils/src/keepalive.rs b/actix-utils/src/keepalive.rs index e113f818..675efa73 100644 --- a/actix-utils/src/keepalive.rs +++ b/actix-utils/src/keepalive.rs @@ -58,7 +58,7 @@ where type Service = KeepAliveService; type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(KeepAliveService::new( self.ka, self.time.timer(), diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index d832d4d2..01de3160 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -51,7 +51,7 @@ impl ServiceFactory for LowResTime { type Service = LowResTimeService; type Future = Ready>; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: ()) -> Self::Future { ok(self.timer()) } }