From 636cef8868462c96cce50d1b5ea7d91a9e8dd0a2 Mon Sep 17 00:00:00 2001 From: fakeshadow <24548779@qq.com> Date: Fri, 22 Jan 2021 19:06:22 -0800 Subject: [PATCH] service trait takes shared self reference (#247) --- actix-router/CHANGES.md | 3 ++ actix-router/src/router.rs | 18 +++++++ actix-server/Cargo.toml | 1 + actix-server/src/service.rs | 4 +- actix-service/CHANGES.md | 6 +++ actix-service/src/and_then.rs | 60 +++++++++------------ actix-service/src/apply.rs | 32 ++++++------ actix-service/src/apply_cfg.rs | 78 +++++++++++++--------------- actix-service/src/boxed.rs | 4 +- actix-service/src/fn_service.rs | 32 ++++++------ actix-service/src/lib.rs | 38 +++++++------- actix-service/src/map.rs | 10 ++-- actix-service/src/map_err.rs | 14 ++--- actix-service/src/pipeline.rs | 4 +- actix-service/src/then.rs | 60 +++++++++------------ actix-service/src/transform.rs | 23 ++++---- actix-tls/src/accept/nativetls.rs | 4 +- actix-tls/src/accept/openssl.rs | 4 +- actix-tls/src/accept/rustls.rs | 4 +- actix-tls/src/connect/connector.rs | 2 +- actix-tls/src/connect/resolve.rs | 2 +- actix-tls/src/connect/service.rs | 4 +- actix-tls/src/connect/ssl/openssl.rs | 4 +- actix-tls/src/connect/ssl/rustls.rs | 2 +- actix-tls/tests/test_connect.rs | 17 +++--- actix-tracing/src/lib.rs | 4 +- actix-utils/src/timeout.rs | 10 ++-- 27 files changed, 225 insertions(+), 219 deletions(-) diff --git a/actix-router/CHANGES.md b/actix-router/CHANGES.md index df93e5be..581243fb 100644 --- a/actix-router/CHANGES.md +++ b/actix-router/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* Add `Router::recognize_checked` [#247] + +[#247]: https://github.com/actix/actix-net/pull/247 ## 0.2.6 - 2021-01-09 diff --git a/actix-router/src/router.rs b/actix-router/src/router.rs index bcbe61f9..aeb1aa36 100644 --- a/actix-router/src/router.rs +++ b/actix-router/src/router.rs @@ -45,6 +45,24 @@ impl Router { None } + pub fn recognize_checked( + &self, + resource: &mut R, + check: F, + ) -> Option<(&T, ResourceId)> + where + F: Fn(&R, &Option) -> bool, + R: Resource

, + P: ResourcePath, + { + for item in self.0.iter() { + if item.0.match_path_checked(resource, &check, &item.2) { + return Some((&item.1, ResourceId(item.0.id()))); + } + } + None + } + pub fn recognize_mut_checked( &mut self, resource: &mut R, diff --git a/actix-server/Cargo.toml b/actix-server/Cargo.toml index 25095039..c366124a 100755 --- a/actix-server/Cargo.toml +++ b/actix-server/Cargo.toml @@ -36,6 +36,7 @@ slab = "0.4" tokio = { version = "1", features = ["sync"] } [dev-dependencies] +actix-rt = "2.0.0-beta.2" bytes = "1" env_logger = "0.8" futures-util = { version = "0.3.7", default-features = false, features = ["sink"] } diff --git a/actix-server/src/service.rs b/actix-server/src/service.rs index 04b7dce8..63d2c1f5 100644 --- a/actix-server/src/service.rs +++ b/actix-server/src/service.rs @@ -58,11 +58,11 @@ where type Error = (); type Future = Ready>; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx).map_err(|_| ()) } - fn call(&mut self, (guard, req): (Option, MioStream)) -> Self::Future { + fn call(&self, (guard, req): (Option, MioStream)) -> Self::Future { ready(match FromStream::from_mio(req) { Ok(stream) => { let f = self.service.call(stream); diff --git a/actix-service/CHANGES.md b/actix-service/CHANGES.md index 4bf41f0e..4a99d60a 100644 --- a/actix-service/CHANGES.md +++ b/actix-service/CHANGES.md @@ -1,6 +1,12 @@ # Changes ## Unreleased - 2021-xx-xx +* `Service::poll_ready` and `Service::call` take `&self`. [#247] +* `apply_fn` and `apply_fn_factory` would take `Fn(Req, &Service)` function type [#247] +* `apply_cfg` and `apply_cfg_factory` would take `Fn(Req, &Service)` function type [#247] +* `fn_service` module would take `Fn(Req)` function type. [#247] + +[#247]: https://github.com/actix/actix-net/pull/247 ## 2.0.0-beta.3 - 2021-01-09 diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index fd24cb56..54a132da 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -1,12 +1,12 @@ -use alloc::rc::Rc; use core::{ - cell::RefCell, future::Future, marker::PhantomData, pin::Pin, task::{Context, Poll}, }; +use alloc::rc::Rc; +use futures_core::ready; use pin_project_lite::pin_project; use super::{Service, ServiceFactory}; @@ -15,7 +15,7 @@ use super::{Service, ServiceFactory}; /// of another service which completes successfully. /// /// This is created by the `Pipeline::and_then` method. -pub(crate) struct AndThenService(Rc>, PhantomData); +pub(crate) struct AndThenService(Rc<(A, B)>, PhantomData); impl AndThenService { /// Create new `AndThen` combinator @@ -24,7 +24,7 @@ impl AndThenService { A: Service, B: Service, { - Self(Rc::new(RefCell::new((a, b))), PhantomData) + Self(Rc::new((a, b)), PhantomData) } } @@ -43,20 +43,20 @@ where type Error = A::Error; type Future = AndThenServiceResponse; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - let mut srv = self.0.borrow_mut(); - let not_ready = !srv.0.poll_ready(cx)?.is_ready(); - if !srv.1.poll_ready(cx)?.is_ready() || not_ready { + fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { + let (a, b) = &*self.0; + let not_ready = !a.poll_ready(cx)?.is_ready(); + if !b.poll_ready(cx)?.is_ready() || not_ready { Poll::Pending } else { Poll::Ready(Ok(())) } } - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { AndThenServiceResponse { state: State::A { - fut: self.0.borrow_mut().0.call(req), + fut: self.0 .0.call(req), b: Some(self.0.clone()), }, } @@ -84,13 +84,12 @@ pin_project! { A { #[pin] fut: A::Future, - b: Option>>, + b: Option>, }, B { #[pin] fut: B::Future, }, - Empty, } } @@ -105,23 +104,14 @@ where let mut this = self.as_mut().project(); match this.state.as_mut().project() { - StateProj::A { fut, b } => match fut.poll(cx)? { - Poll::Ready(res) => { - let b = b.take().unwrap(); - this.state.set(State::Empty); // drop fut A - let fut = b.borrow_mut().1.call(res); - this.state.set(State::B { fut }); - self.poll(cx) - } - Poll::Pending => Poll::Pending, - }, - StateProj::B { fut } => fut.poll(cx).map(|r| { - this.state.set(State::Empty); - r - }), - StateProj::Empty => { - panic!("future must not be polled after it returned `Poll::Ready`") + StateProj::A { fut, b } => { + let res = ready!(fut.poll(cx))?; + let b = b.take().unwrap(); + let fut = b.1.call(res); + this.state.set(State::B { fut }); + self.poll(cx) } + StateProj::B { fut } => fut.poll(cx), } } } @@ -292,12 +282,12 @@ mod tests { type Error = (); type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { self.0.set(self.0.get() + 1); Poll::Ready(Ok(())) } - fn call(&mut self, req: &'static str) -> Self::Future { + fn call(&self, req: &'static str) -> Self::Future { ok(req) } } @@ -310,12 +300,12 @@ mod tests { type Error = (); type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { self.0.set(self.0.get() + 1); Poll::Ready(Ok(())) } - fn call(&mut self, req: &'static str) -> Self::Future { + fn call(&self, req: &'static str) -> Self::Future { ok((req, "srv2")) } } @@ -323,7 +313,7 @@ mod tests { #[actix_rt::test] async fn test_poll_ready() { let cnt = Rc::new(Cell::new(0)); - let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone())); + let srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone())); let res = lazy(|cx| srv.poll_ready(cx)).await; assert_eq!(res, Poll::Ready(Ok(()))); assert_eq!(cnt.get(), 2); @@ -332,7 +322,7 @@ mod tests { #[actix_rt::test] async fn test_call() { let cnt = Rc::new(Cell::new(0)); - let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt)); + let srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt)); let res = srv.call("srv1").await; assert!(res.is_ok()); assert_eq!(res.unwrap(), ("srv1", "srv2")); @@ -346,7 +336,7 @@ mod tests { pipeline_factory(fn_factory(move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))))) .and_then(move || ready(Ok(Srv2(cnt.clone())))); - let mut srv = new_srv.new_service(()).await.unwrap(); + let 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 9b0c4025..9a7e27d2 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -20,7 +20,7 @@ pub fn apply_fn( where I: IntoService, S: Service, - F: FnMut(Req, &mut S) -> Fut, + F: Fn(Req, &S) -> Fut, Fut: Future>, { Apply::new(service.into_service(), wrap_fn) @@ -36,7 +36,7 @@ pub fn apply_fn_factory( where I: IntoServiceFactory, SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut + Clone, + F: Fn(Req, &SF::Service) -> Fut + Clone, Fut: Future>, { ApplyFactory::new(service.into_factory(), f) @@ -57,7 +57,7 @@ where impl Apply where S: Service, - F: FnMut(Req, &mut S) -> Fut, + F: Fn(Req, &S) -> Fut, Fut: Future>, { /// Create new `Apply` combinator @@ -73,7 +73,7 @@ where impl Clone for Apply where S: Service + Clone, - F: FnMut(Req, &mut S) -> Fut + Clone, + F: Fn(Req, &S) -> Fut + Clone, Fut: Future>, { fn clone(&self) -> Self { @@ -88,7 +88,7 @@ where impl Service for Apply where S: Service, - F: FnMut(Req, &mut S) -> Fut, + F: Fn(Req, &S) -> Fut, Fut: Future>, { type Response = Res; @@ -97,8 +97,8 @@ where crate::forward_ready!(service); - fn call(&mut self, req: Req) -> Self::Future { - (self.wrap_fn)(req, &mut self.service) + fn call(&self, req: Req) -> Self::Future { + (self.wrap_fn)(req, &self.service) } } @@ -112,7 +112,7 @@ pub struct ApplyFactory { impl ApplyFactory where SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut + Clone, + F: Fn(Req, &SF::Service) -> Fut + Clone, Fut: Future>, { /// Create new `ApplyFactory` new service instance @@ -128,7 +128,7 @@ where impl Clone for ApplyFactory where SF: ServiceFactory + Clone, - F: FnMut(Req, &mut SF::Service) -> Fut + Clone, + F: Fn(Req, &SF::Service) -> Fut + Clone, Fut: Future>, { fn clone(&self) -> Self { @@ -144,7 +144,7 @@ impl ServiceFactory for ApplyFactory where SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut + Clone, + F: Fn(Req, &SF::Service) -> Fut + Clone, Fut: Future>, { type Response = Res; @@ -165,7 +165,7 @@ pin_project! { pub struct ApplyServiceFactoryResponse where SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut, + F: Fn(Req, &SF::Service) -> Fut, Fut: Future>, { #[pin] @@ -178,7 +178,7 @@ pin_project! { impl ApplyServiceFactoryResponse where SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut, + F: Fn(Req, &SF::Service) -> Fut, Fut: Future>, { fn new(fut: SF::Future, wrap_fn: F) -> Self { @@ -194,7 +194,7 @@ impl Future for ApplyServiceFactoryResponse where SF: ServiceFactory, - F: FnMut(Req, &mut SF::Service) -> Fut, + F: Fn(Req, &SF::Service) -> Fut, Fut: Future>, { type Output = Result, SF::InitError>; @@ -226,14 +226,14 @@ mod tests { crate::always_ready!(); - fn call(&mut self, _: ()) -> Self::Future { + fn call(&self, _: ()) -> Self::Future { ok(()) } } #[actix_rt::test] async fn test_call() { - let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| { + let srv = pipeline(apply_fn(Srv, |req: &'static str, srv| { let fut = srv.call(()); async move { fut.await.unwrap(); @@ -261,7 +261,7 @@ mod tests { }, )); - let mut srv = new_srv.new_service(()).await.unwrap(); + let 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 3e111231..276efc0f 100644 --- a/actix-service/src/apply_cfg.rs +++ b/actix-service/src/apply_cfg.rs @@ -1,17 +1,17 @@ -use alloc::rc::Rc; use core::{ - cell::RefCell, future::Future, marker::PhantomData, pin::Pin, task::{Context, Poll}, }; +use alloc::rc::Rc; +use futures_core::ready; use pin_project_lite::pin_project; use crate::{Service, ServiceFactory}; -/// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory. +/// Convert `Fn(Config, &Service1) -> Future` fn to a service factory. pub fn apply_cfg( srv: S1, f: F, @@ -26,17 +26,17 @@ pub fn apply_cfg( > + Clone where S1: Service, - F: FnMut(Cfg, &mut S1) -> Fut, + F: Fn(Cfg, &S1) -> Fut, Fut: Future>, S2: Service, { ApplyConfigService { - srv: Rc::new(RefCell::new((srv, f))), + srv: Rc::new((srv, f)), _phantom: PhantomData, } } -/// Convert `Fn(Config, &mut ServiceFactory1) -> Future` fn to a service factory. +/// Convert `Fn(Config, &ServiceFactory1) -> Future` fn to a service factory. /// /// Service1 get constructed from `T` factory. pub fn apply_cfg_factory( @@ -52,33 +52,33 @@ pub fn apply_cfg_factory( > + Clone where SF: ServiceFactory, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, SF::InitError: From, Fut: Future>, S: Service, { ApplyConfigServiceFactory { - srv: Rc::new(RefCell::new((factory, f))), + srv: Rc::new((factory, f)), _phantom: PhantomData, } } -/// Convert `Fn(Config, &mut Server) -> Future` fn to NewService\ +/// Convert `Fn(Config, &Server) -> Future` fn to NewService\ struct ApplyConfigService where S1: Service, - F: FnMut(Cfg, &mut S1) -> Fut, + F: Fn(Cfg, &S1) -> Fut, Fut: Future>, S2: Service, { - srv: Rc>, + srv: Rc<(S1, F)>, _phantom: PhantomData<(Cfg, Req, Fut, S2)>, } impl Clone for ApplyConfigService where S1: Service, - F: FnMut(Cfg, &mut S1) -> Fut, + F: Fn(Cfg, &S1) -> Fut, Fut: Future>, S2: Service, { @@ -94,20 +94,20 @@ impl ServiceFactory for ApplyConfigService where S1: Service, - F: FnMut(Cfg, &mut S1) -> Fut, + F: Fn(Cfg, &S1) -> Fut, Fut: Future>, S2: Service, { - type Config = Cfg; type Response = S2::Response; type Error = S2::Error; + type Config = Cfg; type Service = S2; type InitError = Err; type Future = Fut; fn new_service(&self, cfg: Cfg) -> Self::Future { - let (t, f) = &mut *self.srv.borrow_mut(); + let (t, f) = &*self.srv; f(cfg, t) } } @@ -116,18 +116,18 @@ where struct ApplyConfigServiceFactory where SF: ServiceFactory, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, Fut: Future>, S: Service, { - srv: Rc>, + srv: Rc<(SF, F)>, _phantom: PhantomData<(Cfg, Req, Fut, S)>, } impl Clone for ApplyConfigServiceFactory where SF: ServiceFactory, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, Fut: Future>, S: Service, { @@ -144,13 +144,13 @@ impl ServiceFactory where SF: ServiceFactory, SF::InitError: From, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, Fut: Future>, S: Service, { - type Config = Cfg; type Response = S::Response; type Error = S::Error; + type Config = Cfg; type Service = S; type InitError = SF::InitError; @@ -161,7 +161,7 @@ where cfg: Some(cfg), store: self.srv.clone(), state: State::A { - fut: self.srv.borrow().0.new_service(()), + fut: self.srv.0.new_service(()), }, } } @@ -172,12 +172,12 @@ pin_project! { where SF: ServiceFactory, SF::InitError: From, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, Fut: Future>, S: Service, { cfg: Option, - store: Rc>, + store: Rc<(SF, F)>, #[pin] state: State, } @@ -203,7 +203,7 @@ impl Future where SF: ServiceFactory, SF::InitError: From, - F: FnMut(Cfg, &mut SF::Service) -> Fut, + F: Fn(Cfg, &SF::Service) -> Fut, Fut: Future>, S: Service, { @@ -213,24 +213,20 @@ where let mut this = self.as_mut().project(); match this.state.as_mut().project() { - StateProj::A { fut } => match fut.poll(cx)? { - Poll::Pending => Poll::Pending, - Poll::Ready(svc) => { - this.state.set(State::B { svc }); - self.poll(cx) + StateProj::A { fut } => { + let svc = ready!(fut.poll(cx))?; + this.state.set(State::B { svc }); + self.poll(cx) + } + StateProj::B { svc } => { + ready!(svc.poll_ready(cx))?; + { + let (_, f) = &**this.store; + let fut = f(this.cfg.take().unwrap(), svc); + this.state.set(State::C { fut }); } - }, - StateProj::B { svc } => match svc.poll_ready(cx)? { - Poll::Ready(_) => { - { - let (_, f) = &mut *this.store.borrow_mut(); - let fut = f(this.cfg.take().unwrap(), svc); - this.state.set(State::C { fut }); - } - self.poll(cx) - } - Poll::Pending => Poll::Pending, - }, + self.poll(cx) + } StateProj::C { fut } => fut.poll(cx), } } diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index 5c4557df..6ad2eaf4 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -131,11 +131,11 @@ where type Error = Err; type Future = BoxFuture>; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { self.0.poll_ready(ctx) } - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { Box::pin(self.0.call(req)) } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 5f09c580..230f437b 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -7,7 +7,7 @@ pub fn fn_service( f: F, ) -> FnServiceFactory where - F: FnMut(Req) -> Fut + Clone, + F: Fn(Req) -> Fut + Clone, Fut: Future>, { FnServiceFactory::new(f) @@ -39,7 +39,7 @@ where /// }); /// /// // construct new service -/// let mut srv = factory.new_service(()).await?; +/// let srv = factory.new_service(()).await?; /// /// // now we can use `div` service /// let result = srv.call((10, 20)).await?; @@ -81,7 +81,7 @@ where /// }); /// /// // construct new service with config argument -/// let mut srv = factory.new_service(10).await?; +/// let srv = factory.new_service(10).await?; /// /// let result = srv.call(10).await?; /// assert_eq!(result, 100); @@ -132,7 +132,7 @@ where impl Service for FnService where - F: FnMut(Req) -> Fut, + F: Fn(Req) -> Fut, Fut: Future>, { type Response = Res; @@ -141,14 +141,14 @@ where crate::always_ready!(); - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { (self.f)(req) } } impl IntoService, Req> for F where - F: FnMut(Req) -> Fut, + F: Fn(Req) -> Fut, Fut: Future>, { fn into_service(self) -> FnService { @@ -158,7 +158,7 @@ where pub struct FnServiceFactory where - F: FnMut(Req) -> Fut, + F: Fn(Req) -> Fut, Fut: Future>, { f: F, @@ -167,7 +167,7 @@ where impl FnServiceFactory where - F: FnMut(Req) -> Fut + Clone, + F: Fn(Req) -> Fut + Clone, Fut: Future>, { fn new(f: F) -> Self { @@ -177,7 +177,7 @@ where impl Clone for FnServiceFactory where - F: FnMut(Req) -> Fut + Clone, + F: Fn(Req) -> Fut + Clone, Fut: Future>, { fn clone(&self) -> Self { @@ -187,7 +187,7 @@ where impl Service for FnServiceFactory where - F: FnMut(Req) -> Fut + Clone, + F: Fn(Req) -> Fut + Clone, Fut: Future>, { type Response = Res; @@ -196,7 +196,7 @@ where crate::always_ready!(); - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { (self.f)(req) } } @@ -204,7 +204,7 @@ where impl ServiceFactory for FnServiceFactory where - F: FnMut(Req) -> Fut + Clone, + F: Fn(Req) -> Fut + Clone, Fut: Future>, { type Response = Res; @@ -318,8 +318,8 @@ where { type Response = Srv::Response; type Error = Srv::Error; - type Service = Srv; type Config = Cfg; + type Service = Srv; type InitError = Err; type Future = Fut; @@ -364,7 +364,7 @@ mod tests { async fn test_fn_service() { let new_srv = fn_service(|()| ok::<_, ()>("srv")); - let mut srv = new_srv.new_service(()).await.unwrap(); + let srv = new_srv.new_service(()).await.unwrap(); let res = srv.call(()).await; assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); assert!(res.is_ok()); @@ -373,7 +373,7 @@ mod tests { #[actix_rt::test] async fn test_fn_service_service() { - let mut srv = fn_service(|()| ok::<_, ()>("srv")); + let srv = fn_service(|()| ok::<_, ()>("srv")); let res = srv.call(()).await; assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); @@ -387,7 +387,7 @@ mod tests { ok::<_, ()>(fn_service(move |()| ok::<_, ()>(("srv", cfg)))) }); - let mut srv = new_srv.new_service(1).await.unwrap(); + let srv = new_srv.new_service(1).await.unwrap(); let res = srv.call(()).await; assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(()))); assert!(res.is_ok()); diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index b774d0d9..7c3a271c 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -69,9 +69,9 @@ use self::ready::{err, ok, ready, Ready}; /// type Error = MyError; /// type Future = Pin>>>; /// -/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { ... } +/// fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { ... } /// -/// fn call(&mut self, req: Self::Request) -> Self::Future { ... } +/// fn call(&self, req: Self::Request) -> Self::Future { ... } /// } /// ``` /// @@ -104,7 +104,7 @@ pub trait Service { /// # Notes /// 1. `.poll_ready()` might be called on different task from actual service call. /// 1. In case of chained services, `.poll_ready()` get called for all services at once. - fn poll_ready(&mut self, ctx: &mut task::Context<'_>) -> Poll>; + fn poll_ready(&self, ctx: &mut task::Context<'_>) -> Poll>; /// Process the request and return the response asynchronously. /// @@ -115,7 +115,7 @@ pub trait Service { /// /// Calling `call` without calling `poll_ready` is permitted. The /// implementation must be resilient to this fact. - fn call(&mut self, req: Req) -> Self::Future; + fn call(&self, req: Req) -> Self::Future; } /// Factory for creating `Service`s. @@ -158,11 +158,11 @@ where type Error = S::Error; type Future = S::Future; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { (**self).poll_ready(ctx) } - fn call(&mut self, request: Req) -> S::Future { + fn call(&self, request: Req) -> S::Future { (**self).call(request) } } @@ -175,11 +175,11 @@ where type Error = S::Error; type Future = S::Future; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { (**self).poll_ready(ctx) } - fn call(&mut self, request: Req) -> S::Future { + fn call(&self, request: Req) -> S::Future { (**self).call(request) } } @@ -192,12 +192,12 @@ where type Error = S::Error; type Future = S::Future; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { - self.borrow_mut().poll_ready(ctx) + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { + self.borrow().poll_ready(ctx) } - fn call(&mut self, request: Req) -> S::Future { - self.borrow_mut().call(request) + fn call(&self, request: Req) -> S::Future { + self.borrow().call(request) } } @@ -209,12 +209,12 @@ where type Error = S::Error; type Future = S::Future; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { - self.borrow_mut().poll_ready(ctx) + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { + self.borrow().poll_ready(ctx) } - fn call(&mut self, request: Req) -> S::Future { - (&mut (**self).borrow_mut()).call(request) + fn call(&self, request: Req) -> S::Future { + self.borrow().call(request) } } @@ -311,8 +311,9 @@ pub mod dev { #[macro_export] macro_rules! always_ready { () => { + #[inline] fn poll_ready( - &mut self, + &self, _: &mut ::core::task::Context<'_>, ) -> ::core::task::Poll> { Poll::Ready(Ok(())) @@ -323,8 +324,9 @@ macro_rules! always_ready { #[macro_export] macro_rules! forward_ready { ($field:ident) => { + #[inline] fn poll_ready( - &mut self, + &self, cx: &mut ::core::task::Context<'_>, ) -> ::core::task::Poll> { self.$field diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index 0599a1d8..12fd4395 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -58,7 +58,7 @@ where crate::forward_ready!(service); - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { MapFuture::new(self.service.call(req), self.f.clone()) } } @@ -215,21 +215,21 @@ mod tests { crate::always_ready!(); - fn call(&mut self, _: ()) -> Self::Future { + fn call(&self, _: ()) -> Self::Future { ok(()) } } #[actix_rt::test] async fn test_poll_ready() { - let mut srv = Srv.map(|_| "ok"); + let srv = Srv.map(|_| "ok"); let res = lazy(|cx| srv.poll_ready(cx)).await; assert_eq!(res, Poll::Ready(Ok(()))); } #[actix_rt::test] async fn test_call() { - let mut srv = Srv.map(|_| "ok"); + let srv = Srv.map(|_| "ok"); let res = srv.call(()).await; assert!(res.is_ok()); assert_eq!(res.unwrap(), "ok"); @@ -238,7 +238,7 @@ mod tests { #[actix_rt::test] async fn test_new_service() { let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok"); - let mut srv = new_srv.new_service(&()).await.unwrap(); + let srv = new_srv.new_service(&()).await.unwrap(); let res = srv.call(()).await; assert!(res.is_ok()); assert_eq!(res.unwrap(), ("ok")); diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index 944056c2..ff25c4f7 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -57,11 +57,11 @@ where type Error = E; type Future = MapErrFuture; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx).map_err(&self.f) } - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { MapErrFuture::new(self.service.call(req), self.f.clone()) } } @@ -218,25 +218,25 @@ mod tests { type Error = (); type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { Poll::Ready(Err(())) } - fn call(&mut self, _: ()) -> Self::Future { + fn call(&self, _: ()) -> Self::Future { err(()) } } #[actix_rt::test] async fn test_poll_ready() { - let mut srv = Srv.map_err(|_| "error"); + let srv = Srv.map_err(|_| "error"); let res = lazy(|cx| srv.poll_ready(cx)).await; assert_eq!(res, Poll::Ready(Err("error"))); } #[actix_rt::test] async fn test_call() { - let mut srv = Srv.map_err(|_| "error"); + let srv = Srv.map_err(|_| "error"); let res = srv.call(()).await; assert!(res.is_err()); assert_eq!(res.err().unwrap(), "error"); @@ -245,7 +245,7 @@ mod tests { #[actix_rt::test] async fn test_new_service() { let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map_err(|_| "error"); - let mut srv = new_srv.new_service(&()).await.unwrap(); + let srv = new_srv.new_service(&()).await.unwrap(); let res = srv.call(()).await; assert!(res.is_err()); assert_eq!(res.err().unwrap(), "error"); diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index 580d7b4c..0ec43f0d 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -146,12 +146,12 @@ impl, Req> Service for Pipeline { type Future = S::Future; #[inline] - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx) } #[inline] - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { self.service.call(req) } } diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index 060ca9c7..9cd311ad 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -1,12 +1,12 @@ -use alloc::rc::Rc; use core::{ - cell::RefCell, future::Future, marker::PhantomData, pin::Pin, task::{Context, Poll}, }; +use alloc::rc::Rc; +use futures_core::ready; use pin_project_lite::pin_project; use super::{Service, ServiceFactory}; @@ -15,7 +15,7 @@ use super::{Service, ServiceFactory}; /// another service. /// /// This is created by the `Pipeline::then` method. -pub(crate) struct ThenService(Rc>, PhantomData); +pub(crate) struct ThenService(Rc<(A, B)>, PhantomData); impl ThenService { /// Create new `.then()` combinator @@ -24,7 +24,7 @@ impl ThenService { A: Service, B: Service, Error = A::Error>, { - Self(Rc::new(RefCell::new((a, b))), PhantomData) + Self(Rc::new((a, b)), PhantomData) } } @@ -43,20 +43,20 @@ where type Error = B::Error; type Future = ThenServiceResponse; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - let mut srv = self.0.borrow_mut(); - let not_ready = !srv.0.poll_ready(cx)?.is_ready(); - if !srv.1.poll_ready(cx)?.is_ready() || not_ready { + fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { + let (a, b) = &*self.0; + let not_ready = !a.poll_ready(cx)?.is_ready(); + if !b.poll_ready(cx)?.is_ready() || not_ready { Poll::Pending } else { Poll::Ready(Ok(())) } } - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { ThenServiceResponse { state: State::A { - fut: self.0.borrow_mut().0.call(req), + fut: self.0 .0.call(req), b: Some(self.0.clone()), }, } @@ -81,9 +81,8 @@ pin_project! { A: Service, B: Service>, { - A { #[pin] fut: A::Future, b: Option>> }, + A { #[pin] fut: A::Future, b: Option> }, B { #[pin] fut: B::Future }, - Empty, } } @@ -98,23 +97,14 @@ where let mut this = self.as_mut().project(); match this.state.as_mut().project() { - StateProj::A { fut, b } => match fut.poll(cx) { - Poll::Ready(res) => { - let b = b.take().unwrap(); - this.state.set(State::Empty); // drop fut A - let fut = b.borrow_mut().1.call(res); - this.state.set(State::B { fut }); - self.poll(cx) - } - Poll::Pending => Poll::Pending, - }, - StateProj::B { fut } => fut.poll(cx).map(|r| { - this.state.set(State::Empty); - r - }), - StateProj::Empty => { - panic!("future must not be polled after it returned `Poll::Ready`") + StateProj::A { fut, b } => { + let res = ready!(fut.poll(cx)); + let b = b.take().unwrap(); + let fut = b.1.call(res); + this.state.set(State::B { fut }); + self.poll(cx) } + StateProj::B { fut } => fut.poll(cx), } } } @@ -266,12 +256,12 @@ mod tests { type Error = (); type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { self.0.set(self.0.get() + 1); Poll::Ready(Ok(())) } - fn call(&mut self, req: Result<&'static str, &'static str>) -> Self::Future { + fn call(&self, req: Result<&'static str, &'static str>) -> Self::Future { match req { Ok(msg) => ok(msg), Err(_) => err(()), @@ -286,12 +276,12 @@ mod tests { type Error = (); type Future = Ready>; - fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, _: &mut Context<'_>) -> Poll> { self.0.set(self.0.get() + 1); Poll::Ready(Err(())) } - fn call(&mut self, req: Result<&'static str, ()>) -> Self::Future { + fn call(&self, req: Result<&'static str, ()>) -> Self::Future { match req { Ok(msg) => ok((msg, "ok")), Err(()) => ok(("srv2", "err")), @@ -302,7 +292,7 @@ mod tests { #[actix_rt::test] async fn test_poll_ready() { let cnt = Rc::new(Cell::new(0)); - let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone())); + let srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt.clone())); let res = lazy(|cx| srv.poll_ready(cx)).await; assert_eq!(res, Poll::Ready(Err(()))); assert_eq!(cnt.get(), 2); @@ -311,7 +301,7 @@ mod tests { #[actix_rt::test] async fn test_call() { let cnt = Rc::new(Cell::new(0)); - let mut srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt)); + let srv = pipeline(Srv1(cnt.clone())).then(Srv2(cnt)); let res = srv.call(Ok("srv1")).await; assert!(res.is_ok()); @@ -328,7 +318,7 @@ mod tests { let cnt2 = cnt.clone(); let blank = move || ready(Ok::<_, ()>(Srv1(cnt2.clone()))); let factory = pipeline_factory(blank).then(move || ready(Ok(Srv2(cnt.clone())))); - let mut srv = factory.new_service(&()).await.unwrap(); + let srv = factory.new_service(&()).await.unwrap(); let res = srv.call(Ok("srv1")).await; assert!(res.is_ok()); assert_eq!(res.unwrap(), ("srv1", "ok")); diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 7d707d98..d5cbcd88 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -1,4 +1,3 @@ -use alloc::{rc::Rc, sync::Arc}; use core::{ future::Future, marker::PhantomData, @@ -6,6 +5,8 @@ use core::{ task::{Context, Poll}, }; +use alloc::{rc::Rc, sync::Arc}; +use futures_core::ready; use pin_project_lite::pin_project; use crate::transform_err::TransformMapInitErr; @@ -47,7 +48,7 @@ where /// /// actix_service::forward_ready!(service); /// -/// fn call(&mut self, req: S::Request) -> Self::Future { +/// fn call(&self, req: S::Request) -> Self::Future { /// TimeoutServiceResponse { /// fut: self.service.call(req), /// sleep: Delay::new(clock::now() + self.timeout), @@ -127,8 +128,8 @@ where { type Response = T::Response; type Error = T::Error; - type InitError = T::InitError; type Transform = T::Transform; + type InitError = T::InitError; type Future = T::Future; fn new_transform(&self, service: S) -> T::Future { @@ -142,8 +143,8 @@ where { type Response = T::Response; type Error = T::Error; - type InitError = T::InitError; type Transform = T::Transform; + type InitError = T::InitError; type Future = T::Future; fn new_transform(&self, service: S) -> T::Future { @@ -229,14 +230,12 @@ where let mut this = self.as_mut().project(); match this.state.as_mut().project() { - ApplyTransformFutureStateProj::A { fut } => match fut.poll(cx)? { - Poll::Ready(srv) => { - let fut = this.store.0.new_transform(srv); - this.state.set(ApplyTransformFutureState::B { fut }); - self.poll(cx) - } - Poll::Pending => Poll::Pending, - }, + ApplyTransformFutureStateProj::A { fut } => { + let srv = ready!(fut.poll(cx))?; + let fut = this.store.0.new_transform(srv); + this.state.set(ApplyTransformFutureState::B { fut }); + self.poll(cx) + } ApplyTransformFutureStateProj::B { fut } => fut.poll(cx), } } diff --git a/actix-tls/src/accept/nativetls.rs b/actix-tls/src/accept/nativetls.rs index 9b2aeefb..73090de6 100644 --- a/actix-tls/src/accept/nativetls.rs +++ b/actix-tls/src/accept/nativetls.rs @@ -79,7 +79,7 @@ where type Error = Error; type Future = LocalBoxFuture<'static, Result, Error>>; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { if self.conns.available(cx) { Poll::Ready(Ok(())) } else { @@ -87,7 +87,7 @@ where } } - fn call(&mut self, io: T) -> Self::Future { + fn call(&self, io: T) -> Self::Future { let guard = self.conns.get(); let this = self.clone(); Box::pin(async move { diff --git a/actix-tls/src/accept/openssl.rs b/actix-tls/src/accept/openssl.rs index 5f2d2fc2..8ca88578 100644 --- a/actix-tls/src/accept/openssl.rs +++ b/actix-tls/src/accept/openssl.rs @@ -75,7 +75,7 @@ where type Error = SslError; type Future = AcceptorServiceResponse; - fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, ctx: &mut Context<'_>) -> Poll> { if self.conns.available(ctx) { Poll::Ready(Ok(())) } else { @@ -83,7 +83,7 @@ where } } - fn call(&mut self, io: T) -> Self::Future { + fn call(&self, io: T) -> Self::Future { let ssl_ctx = self.acceptor.context(); let ssl = Ssl::new(ssl_ctx).expect("Provided SSL acceptor was invalid."); AcceptorServiceResponse { diff --git a/actix-tls/src/accept/rustls.rs b/actix-tls/src/accept/rustls.rs index e7efaa3f..c65d4657 100644 --- a/actix-tls/src/accept/rustls.rs +++ b/actix-tls/src/accept/rustls.rs @@ -80,7 +80,7 @@ where type Error = io::Error; type Future = AcceptorServiceFut; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_ready(&self, cx: &mut Context<'_>) -> Poll> { if self.conns.available(cx) { Poll::Ready(Ok(())) } else { @@ -88,7 +88,7 @@ where } } - fn call(&mut self, req: T) -> Self::Future { + fn call(&self, req: T) -> Self::Future { AcceptorServiceFut { _guard: self.conns.get(), fut: self.acceptor.accept(req), diff --git a/actix-tls/src/connect/connector.rs b/actix-tls/src/connect/connector.rs index fe4ceec6..dc4fbc72 100755 --- a/actix-tls/src/connect/connector.rs +++ b/actix-tls/src/connect/connector.rs @@ -51,7 +51,7 @@ impl Service> for TcpConnector { actix_service::always_ready!(); - fn call(&mut self, req: Connect) -> Self::Future { + fn call(&self, req: Connect) -> Self::Future { let port = req.port(); let Connect { req, addr, .. } = req; diff --git a/actix-tls/src/connect/resolve.rs b/actix-tls/src/connect/resolve.rs index 211da387..a672b971 100755 --- a/actix-tls/src/connect/resolve.rs +++ b/actix-tls/src/connect/resolve.rs @@ -146,7 +146,7 @@ impl Service> for Resolver { actix_service::always_ready!(); - fn call(&mut self, req: Connect) -> Self::Future { + fn call(&self, req: Connect) -> Self::Future { if !req.addr.is_none() { ResolverFuture::Connected(Some(req)) } else if let Ok(ip) = req.host().parse() { diff --git a/actix-tls/src/connect/service.rs b/actix-tls/src/connect/service.rs index 6fa8a453..98765ca1 100755 --- a/actix-tls/src/connect/service.rs +++ b/actix-tls/src/connect/service.rs @@ -80,7 +80,7 @@ impl Service> for ConnectService { actix_service::always_ready!(); - fn call(&mut self, req: Connect) -> Self::Future { + fn call(&self, req: Connect) -> Self::Future { ConnectServiceResponse { fut: ConnectFuture::Resolve(self.resolver.call(req)), tcp: self.tcp, @@ -149,7 +149,7 @@ impl Service> for TcpConnectService { actix_service::always_ready!(); - fn call(&mut self, req: Connect) -> Self::Future { + fn call(&self, req: Connect) -> Self::Future { TcpConnectServiceResponse { fut: ConnectFuture::Resolve(self.resolver.call(req)), tcp: self.tcp, diff --git a/actix-tls/src/connect/ssl/openssl.rs b/actix-tls/src/connect/ssl/openssl.rs index d5d71edc..0006163d 100755 --- a/actix-tls/src/connect/ssl/openssl.rs +++ b/actix-tls/src/connect/ssl/openssl.rs @@ -84,7 +84,7 @@ where actix_service::always_ready!(); - fn call(&mut self, stream: Connection) -> Self::Future { + fn call(&self, stream: Connection) -> Self::Future { trace!("SSL Handshake start for: {:?}", stream.host()); let (io, stream) = stream.replace(()); let host = stream.host(); @@ -202,7 +202,7 @@ impl Service> for OpensslConnectService { actix_service::always_ready!(); - fn call(&mut self, req: Connect) -> Self::Future { + fn call(&self, req: Connect) -> Self::Future { OpensslConnectServiceResponse { fut1: Some(self.tcp.call(req)), fut2: None, diff --git a/actix-tls/src/connect/ssl/rustls.rs b/actix-tls/src/connect/ssl/rustls.rs index e12ab7ec..9d7623ee 100755 --- a/actix-tls/src/connect/ssl/rustls.rs +++ b/actix-tls/src/connect/ssl/rustls.rs @@ -84,7 +84,7 @@ where actix_service::always_ready!(); - fn call(&mut self, stream: Connection) -> Self::Future { + fn call(&self, stream: Connection) -> Self::Future { trace!("SSL Handshake start for: {:?}", stream.host()); let (io, stream) = stream.replace(()); let host = DNSNameRef::try_from_ascii_str(stream.host()) diff --git a/actix-tls/tests/test_connect.rs b/actix-tls/tests/test_connect.rs index 32d2ac0f..392c76c6 100755 --- a/actix-tls/tests/test_connect.rs +++ b/actix-tls/tests/test_connect.rs @@ -22,7 +22,7 @@ async fn test_string() { }) }); - let mut conn = actix_connect::default_connector(); + let conn = actix_connect::default_connector(); let addr = format!("localhost:{}", srv.port()); let con = conn.call(addr.into()).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); @@ -39,7 +39,7 @@ async fn test_rustls_string() { }) }); - let mut conn = actix_connect::default_connector(); + let conn = actix_connect::default_connector(); let addr = format!("localhost:{}", srv.port()); let con = conn.call(addr.into()).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); @@ -55,13 +55,14 @@ async fn test_static_str() { }) }); - let mut conn = actix_connect::default_connector(); + let conn = actix_connect::default_connector(); let con = conn.call(Connect::with("10", srv.addr())).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); let connect = Connect::new(srv.host().to_owned()); - let mut conn = actix_connect::default_connector(); + + let conn = actix_connect::default_connector(); let con = conn.call(connect).await; assert!(con.is_err()); } @@ -78,7 +79,7 @@ async fn test_new_service() { let factory = actix_connect::default_connector_factory(); - let mut conn = factory.new_service(()).await.unwrap(); + let 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()); } @@ -126,7 +127,7 @@ async fn test_custom_resolver() { let factory = actix_connect::new_connector_factory(resolver); - let mut conn = factory.new_service(()).await.unwrap(); + let 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()); } @@ -144,7 +145,7 @@ async fn test_openssl_uri() { }) }); - let mut conn = actix_connect::default_connector(); + let conn = actix_connect::default_connector(); let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); let con = conn.call(addr.into()).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); @@ -163,7 +164,7 @@ async fn test_rustls_uri() { }) }); - let mut conn = actix_connect::default_connector(); + let conn = actix_connect::default_connector(); let addr = http::Uri::try_from(format!("https://localhost:{}", srv.port())).unwrap(); let con = conn.call(addr.into()).await.unwrap(); assert_eq!(con.peer_addr().unwrap(), srv.addr()); diff --git a/actix-tracing/src/lib.rs b/actix-tracing/src/lib.rs index 77a16287..b34f40d6 100644 --- a/actix-tracing/src/lib.rs +++ b/actix-tracing/src/lib.rs @@ -37,7 +37,7 @@ where actix_service::forward_ready!(inner); - fn call(&mut self, req: Req) -> Self::Future { + fn call(&self, req: Req) -> Self::Future { let span = (self.make_span)(&req); let _enter = span.as_ref().map(|s| s.enter()); @@ -229,7 +229,7 @@ mod test { let span_svc = span!(Level::TRACE, "span_svc"); let trace_service_factory = trace(service_factory, |_: &&str| Some(span_svc.clone())); - let mut service = trace_service_factory.new_service(()).await.unwrap(); + let service = trace_service_factory.new_service(()).await.unwrap(); service.call("boo").await.unwrap(); let id = span_svc.id().unwrap().into_u64(); diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index a308b0db..9304e5f6 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -151,7 +151,7 @@ where actix_service::forward_ready!(service); - fn call(&mut self, request: Req) -> Self::Future { + fn call(&self, request: Req) -> Self::Future { TimeoutServiceResponse { fut: self.service.call(request), sleep: sleep(self.timeout), @@ -213,7 +213,7 @@ mod tests { actix_service::always_ready!(); - fn call(&mut self, _: ()) -> Self::Future { + fn call(&self, _: ()) -> Self::Future { let sleep = actix_rt::time::sleep(self.0); Box::pin(async move { sleep.await; @@ -227,7 +227,7 @@ mod tests { let resolution = Duration::from_millis(100); let wait_time = Duration::from_millis(50); - let mut timeout = TimeoutService::new(resolution, SleepService(wait_time)); + let timeout = TimeoutService::new(resolution, SleepService(wait_time)); assert_eq!(timeout.call(()).await, Ok(())); } @@ -236,7 +236,7 @@ mod tests { let resolution = Duration::from_millis(100); let wait_time = Duration::from_millis(500); - let mut timeout = TimeoutService::new(resolution, SleepService(wait_time)); + let timeout = TimeoutService::new(resolution, SleepService(wait_time)); assert_eq!(timeout.call(()).await, Err(TimeoutError::Timeout)); } @@ -249,7 +249,7 @@ mod tests { Timeout::new(resolution), fn_factory(|| async { Ok::<_, ()>(SleepService(wait_time)) }), ); - let mut srv = timeout.new_service(&()).await.unwrap(); + let srv = timeout.new_service(&()).await.unwrap(); assert_eq!(srv.call(()).await, Err(TimeoutError::Timeout)); }