From b12a3e3ae961a9b719184cc53307e9ae34339d26 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 16 Jun 2020 01:38:34 +0900 Subject: [PATCH] Remove uses of pin_project::project attribute (#458) pin-project will deprecate the project attribute due to some unfixable limitations. Refs: https://github.com/taiki-e/pin-project/issues/225 --- tower-test/Cargo.toml | 2 +- tower/Cargo.toml | 2 +- tower/src/buffer/future.rs | 12 +++++------- tower/src/filter/future.rs | 10 ++++------ tower/src/hedge/delay.rs | 10 ++++------ tower/src/load_shed/future.rs | 10 ++++------ tower/src/reconnect/future.rs | 10 ++++------ tower/src/retry/future.rs | 12 +++++------- tower/src/util/either.rs | 10 ++++------ tower/src/util/oneshot.rs | 12 +++++------- 10 files changed, 37 insertions(+), 53 deletions(-) diff --git a/tower-test/Cargo.toml b/tower-test/Cargo.toml index a854495..1e1db2f 100644 --- a/tower-test/Cargo.toml +++ b/tower-test/Cargo.toml @@ -27,7 +27,7 @@ tokio = { version = "0.2", features = ["sync"]} tower-layer = { version = "0.3", path = "../tower-layer" } tokio-test = "0.2" tower-service = { version = "0.3" } -pin-project = "0.4" +pin-project = "0.4.17" [dev-dependencies] tokio = { version = "0.2", features = ["macros"] } diff --git a/tower/Cargo.toml b/tower/Cargo.toml index 5a293e6..534280a 100644 --- a/tower/Cargo.toml +++ b/tower/Cargo.toml @@ -45,7 +45,7 @@ util = ["futures-util"] [dependencies] futures-core = "0.3" -pin-project = "0.4" +pin-project = "0.4.17" tower-layer = { version = "0.3", path = "../tower-layer" } tower-service = { version = "0.3" } tracing = "0.1.2" diff --git a/tower/src/buffer/future.rs b/tower/src/buffer/future.rs index 6b5ae64..6aeff36 100644 --- a/tower/src/buffer/future.rs +++ b/tower/src/buffer/future.rs @@ -2,7 +2,7 @@ use super::{error::Closed, message}; use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{ future::Future, pin::Pin, @@ -17,7 +17,7 @@ pub struct ResponseFuture { state: ResponseState, } -#[pin_project] +#[pin_project(project = ResponseStateProj)] #[derive(Debug)] enum ResponseState { Failed(Option), @@ -46,22 +46,20 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.project(); loop { - #[project] match this.state.as_mut().project() { - ResponseState::Failed(e) => { + ResponseStateProj::Failed(e) => { return Poll::Ready(Err(e.take().expect("polled after error"))); } - ResponseState::Rx(rx) => match ready!(rx.poll(cx)) { + ResponseStateProj::Rx(rx) => match ready!(rx.poll(cx)) { Ok(Ok(f)) => this.state.set(ResponseState::Poll(f)), Ok(Err(e)) => return Poll::Ready(Err(e.into())), Err(_) => return Poll::Ready(Err(Closed::new().into())), }, - ResponseState::Poll(fut) => return fut.poll(cx).map_err(Into::into), + ResponseStateProj::Poll(fut) => return fut.poll(cx).map_err(Into::into), } } } diff --git a/tower/src/filter/future.rs b/tower/src/filter/future.rs index 271fc4b..d4b3aa8 100644 --- a/tower/src/filter/future.rs +++ b/tower/src/filter/future.rs @@ -2,7 +2,7 @@ use super::error::Error; use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{ future::Future, pin::Pin, @@ -29,7 +29,7 @@ where service: S, } -#[pin_project] +#[pin_project(project = StateProj)] #[derive(Debug)] enum State { Check(Option), @@ -59,14 +59,12 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.project(); loop { - #[project] match this.state.as_mut().project() { - State::Check(request) => { + StateProj::Check(request) => { let request = request .take() .expect("we either give it back or leave State::Check once we take"); @@ -83,7 +81,7 @@ where } } } - State::WaitResponse(response) => { + StateProj::WaitResponse(response) => { return Poll::Ready(ready!(response.poll(cx)).map_err(Error::inner)); } } diff --git a/tower/src/hedge/delay.rs b/tower/src/hedge/delay.rs index e789d83..a457940 100644 --- a/tower/src/hedge/delay.rs +++ b/tower/src/hedge/delay.rs @@ -1,5 +1,5 @@ use futures_util::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::time::Duration; use std::{ future::Future, @@ -29,7 +29,7 @@ pub struct ResponseFuture { state: State, } -#[pin_project] +#[pin_project(project = StateProj)] #[derive(Debug)] enum State { Delaying(#[pin] tokio::time::Delay, Option), @@ -84,20 +84,18 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.project(); loop { - #[project] match this.state.as_mut().project() { - State::Delaying(delay, req) => { + StateProj::Delaying(delay, req) => { ready!(delay.poll(cx)); let req = req.take().expect("Missing request in delay"); let fut = this.service.call(req); this.state.set(State::Called(fut)); } - State::Called(fut) => { + StateProj::Called(fut) => { return fut.poll(cx).map_err(Into::into); } }; diff --git a/tower/src/load_shed/future.rs b/tower/src/load_shed/future.rs index b0d08a1..3ab27a5 100644 --- a/tower/src/load_shed/future.rs +++ b/tower/src/load_shed/future.rs @@ -6,7 +6,7 @@ use std::pin::Pin; use std::task::{Context, Poll}; use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use super::error::Overloaded; @@ -17,7 +17,7 @@ pub struct ResponseFuture { state: ResponseState, } -#[pin_project] +#[pin_project(project = ResponseStateProj)] enum ResponseState { Called(#[pin] F), Overloaded, @@ -44,12 +44,10 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - #[project] match self.project().state.project() { - ResponseState::Called(fut) => Poll::Ready(ready!(fut.poll(cx)).map_err(Into::into)), - ResponseState::Overloaded => Poll::Ready(Err(Overloaded::new().into())), + ResponseStateProj::Called(fut) => Poll::Ready(ready!(fut.poll(cx)).map_err(Into::into)), + ResponseStateProj::Overloaded => Poll::Ready(Err(Overloaded::new().into())), } } } diff --git a/tower/src/reconnect/future.rs b/tower/src/reconnect/future.rs index 2208207..4e13aa4 100644 --- a/tower/src/reconnect/future.rs +++ b/tower/src/reconnect/future.rs @@ -1,4 +1,4 @@ -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{ future::Future, pin::Pin, @@ -13,7 +13,7 @@ pub struct ResponseFuture { inner: Inner, } -#[pin_project] +#[pin_project(project = InnerProj)] #[derive(Debug)] enum Inner { Future(#[pin] F), @@ -42,13 +42,11 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let me = self.project(); - #[project] match me.inner.project() { - Inner::Future(fut) => fut.poll(cx).map_err(Into::into), - Inner::Error(e) => { + InnerProj::Future(fut) => fut.poll(cx).map_err(Into::into), + InnerProj::Error(e) => { let e = e.take().expect("Polled after ready.").into(); Poll::Ready(Err(e)) } diff --git a/tower/src/retry/future.rs b/tower/src/retry/future.rs index 77377c5..5db0ea9 100644 --- a/tower/src/retry/future.rs +++ b/tower/src/retry/future.rs @@ -2,7 +2,7 @@ use super::{Policy, Retry}; use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; @@ -23,7 +23,7 @@ where state: State, } -#[pin_project] +#[pin_project(project = StateProj)] #[derive(Debug)] enum State { /// Polling the future from `Service::call` @@ -59,14 +59,12 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.project(); loop { - #[project] match this.state.as_mut().project() { - State::Called(future) => { + StateProj::Called(future) => { let result = ready!(future.poll(cx)); if let Some(ref req) = this.request { match this.retry.policy.retry(req, result.as_ref()) { @@ -80,7 +78,7 @@ where return Poll::Ready(result); } } - State::Checking(future) => { + StateProj::Checking(future) => { this.retry .as_mut() .project() @@ -88,7 +86,7 @@ where .set(ready!(future.poll(cx))); this.state.set(State::Retrying); } - State::Retrying => { + StateProj::Retrying => { // NOTE: we assume here that // // this.retry.poll_ready() diff --git a/tower/src/util/either.rs b/tower/src/util/either.rs index 7069ea2..e53d954 100644 --- a/tower/src/util/either.rs +++ b/tower/src/util/either.rs @@ -3,7 +3,7 @@ //! See `Either` documentation for more details. use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{ future::Future, pin::Pin, @@ -16,7 +16,7 @@ use tower_service::Service; /// Both services must be of the same request, response, and error types. /// `Either` is useful for handling conditional branching in service middleware /// to different inner service types. -#[pin_project] +#[pin_project(project = EitherProj)] #[derive(Clone, Debug)] pub enum Either { /// One type of backing `Service`. @@ -64,12 +64,10 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - #[project] match self.project() { - Either::A(fut) => Poll::Ready(Ok(ready!(fut.poll(cx)).map_err(Into::into)?)), - Either::B(fut) => Poll::Ready(Ok(ready!(fut.poll(cx)).map_err(Into::into)?)), + EitherProj::A(fut) => Poll::Ready(Ok(ready!(fut.poll(cx)).map_err(Into::into)?)), + EitherProj::B(fut) => Poll::Ready(Ok(ready!(fut.poll(cx)).map_err(Into::into)?)), } } } diff --git a/tower/src/util/oneshot.rs b/tower/src/util/oneshot.rs index b91e157..0f73a4a 100644 --- a/tower/src/util/oneshot.rs +++ b/tower/src/util/oneshot.rs @@ -1,5 +1,5 @@ use futures_core::ready; -use pin_project::{pin_project, project}; +use pin_project::pin_project; use std::{ fmt, future::Future, @@ -18,7 +18,7 @@ pub struct Oneshot, Req> { state: State, } -#[pin_project] +#[pin_project(project = StateProj)] enum State, Req> { NotReady(S, Option), Called(#[pin] S::Future), @@ -62,23 +62,21 @@ where { type Output = Result; - #[project] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let mut this = self.project(); loop { - #[project] match this.state.as_mut().project() { - State::NotReady(svc, req) => { + StateProj::NotReady(svc, req) => { let _ = ready!(svc.poll_ready(cx))?; let f = svc.call(req.take().expect("already called")); this.state.set(State::Called(f)); } - State::Called(fut) => { + StateProj::Called(fut) => { let res = ready!(fut.poll(cx))?; this.state.set(State::Done); return Poll::Ready(Ok(res)); } - State::Done => panic!("polled after complete"), + StateProj::Done => panic!("polled after complete"), } } }