From 62ffe5f3894723c00ab651e3d6816ee5b48966a0 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Sun, 5 Dec 2021 01:30:04 +0300 Subject: [PATCH] fix auto-traits for service types (#397) --- actix-service/CHANGES.md | 3 ++ actix-service/src/apply.rs | 6 ++-- actix-service/src/fn_service.rs | 44 +++++++++++++++++++++++++++--- actix-service/src/map.rs | 4 +-- actix-service/src/map_config.rs | 4 +-- actix-service/src/map_err.rs | 4 +-- actix-service/src/map_init_err.rs | 2 +- actix-service/src/pipeline.rs | 4 +-- actix-service/src/transform_err.rs | 2 +- 9 files changed, 56 insertions(+), 17 deletions(-) diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index c87e2470..bc7e69df 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* Service types can now be `Send` and `'static` regardless of request, response, and config types, etc.. [#397] + +[#397]: https://github.com/actix/actix-net/pull/397 ## 2.0.1 - 2021-10-11 diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index 2f798fd0..c77f4242 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -51,7 +51,7 @@ where { service: S, wrap_fn: F, - _phantom: PhantomData<(Req, In, Res, Err)>, + _phantom: PhantomData (In, Res, Err)>, } impl Apply @@ -106,7 +106,7 @@ where pub struct ApplyFactory { factory: SF, wrap_fn: F, - _phantom: PhantomData<(Req, In, Res, Err)>, + _phantom: PhantomData (In, Res, Err)>, } impl ApplyFactory @@ -171,7 +171,7 @@ pin_project! { #[pin] fut: SF::Future, wrap_fn: Option, - _phantom: PhantomData<(Req, Res)>, + _phantom: PhantomData Res>, } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index f83ef81f..ae22e39b 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -105,7 +105,7 @@ where Fut: Future>, { f: F, - _t: PhantomData, + _t: PhantomData, } impl FnService @@ -160,7 +160,7 @@ where Fut: Future>, { f: F, - _t: PhantomData<(Req, Cfg)>, + _t: PhantomData, } impl FnServiceFactory @@ -237,7 +237,7 @@ where Srv: Service, { f: F, - _t: PhantomData<(Fut, Cfg, Req, Srv, Err)>, + _t: PhantomData (Fut, Srv, Err)>, } impl FnServiceConfig @@ -293,7 +293,7 @@ where Fut: Future>, { f: F, - _t: PhantomData<(Cfg, Req)>, + _t: PhantomData, } impl FnServiceNoConfig @@ -391,4 +391,40 @@ mod tests { assert!(res.is_ok()); assert_eq!(res.unwrap(), ("srv", 1)); } + + #[actix_rt::test] + async fn test_auto_impl_send() { + use crate::{map_config, ServiceExt, ServiceFactoryExt}; + use alloc::rc::Rc; + + let srv_1 = fn_service(|_: Rc| ok::<_, Rc>(Rc::new(0u8))); + + let fac_1 = fn_factory_with_config(|_: Rc| { + ok::<_, Rc>(fn_service(|_: Rc| ok::<_, Rc>(Rc::new(0u8)))) + }); + + let fac_2 = fn_factory(|| { + ok::<_, Rc>(fn_service(|_: Rc| ok::<_, Rc>(Rc::new(0u8)))) + }); + + fn is_send(_: &T) {} + + is_send(&fac_1); + is_send(&map_config(fac_1.clone(), |_: Rc| Rc::new(0u8))); + is_send(&fac_1.clone().map_err(|_| Rc::new(0u8))); + is_send(&fac_1.clone().map(|_| Rc::new(0u8))); + is_send(&fac_1.clone().map_init_err(|_| Rc::new(0u8))); + // `and_then` is always !Send + // is_send(&fac_1.clone().and_then(fac_1.clone())); + is_send(&fac_1.new_service(Rc::new(0u8)).await.unwrap()); + + is_send(&fac_2); + is_send(&fac_2.new_service(Rc::new(0u8)).await.unwrap()); + + is_send(&srv_1); + is_send(&ServiceExt::map(srv_1.clone(), |_| Rc::new(0u8))); + is_send(&ServiceExt::map_err(srv_1.clone(), |_| Rc::new(0u8))); + // `and_then` is always !Send + // is_send(&ServiceExt::and_then(srv_1.clone(), srv_1.clone())); + } } diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index 12fd4395..985a5433 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -15,7 +15,7 @@ use super::{Service, ServiceFactory}; pub struct Map { service: A, f: F, - _t: PhantomData<(Req, Res)>, + _t: PhantomData Res>, } impl Map { @@ -107,7 +107,7 @@ where pub struct MapServiceFactory { a: A, f: F, - r: PhantomData<(Res, Req)>, + r: PhantomData Res>, } impl MapServiceFactory { diff --git a/actix-service/src/map_config.rs b/actix-service/src/map_config.rs index 1297f7a0..5b63e3ad 100644 --- a/actix-service/src/map_config.rs +++ b/actix-service/src/map_config.rs @@ -28,7 +28,7 @@ where pub struct MapConfig { factory: SF, cfg_mapper: F, - e: PhantomData<(Cfg, Req)>, + e: PhantomData, } impl MapConfig { @@ -82,7 +82,7 @@ where /// `unit_config()` config combinator pub struct UnitConfig { factory: SF, - _phantom: PhantomData<(Cfg, Req)>, + _phantom: PhantomData, } impl UnitConfig diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index aad0f421..3ce6f418 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -15,7 +15,7 @@ use super::{Service, ServiceFactory}; pub struct MapErr { service: S, mapper: F, - _t: PhantomData<(E, Req)>, + _t: PhantomData E>, } impl MapErr { @@ -111,7 +111,7 @@ where { a: SF, f: F, - e: PhantomData<(E, Req)>, + e: PhantomData E>, } impl MapErrServiceFactory diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index 9fc383aa..a8a22a78 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -13,7 +13,7 @@ use super::ServiceFactory; pub struct MapInitErr { a: A, f: F, - e: PhantomData<(Req, Err)>, + e: PhantomData Err>, } impl MapInitErr diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index 2c71a74b..2617d0ed 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -40,7 +40,7 @@ where /// Pipeline service - pipeline allows to compose multiple service into one service. pub(crate) struct Pipeline { service: S, - _phantom: PhantomData, + _phantom: PhantomData, } impl Pipeline @@ -162,7 +162,7 @@ impl, Req> Service for Pipeline { /// Pipeline factory pub(crate) struct PipelineFactory { factory: SF, - _phantom: PhantomData, + _phantom: PhantomData, } impl PipelineFactory diff --git a/actix-service/src/transform_err.rs b/actix-service/src/transform_err.rs index b4695d5c..b6225a38 100644 --- a/actix-service/src/transform_err.rs +++ b/actix-service/src/transform_err.rs @@ -14,7 +14,7 @@ use super::Transform; pub struct TransformMapInitErr { transform: T, mapper: F, - _phantom: PhantomData<(S, Req, E)>, + _phantom: PhantomData (S, E)>, } impl TransformMapInitErr {