Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uses of pin_project::project attribute #458

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tower-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion tower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions tower/src/buffer/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,7 +17,7 @@ pub struct ResponseFuture<T> {
state: ResponseState<T>,
}

#[pin_project]
#[pin_project(project = ResponseStateProj)]
#[derive(Debug)]
enum ResponseState<T> {
Failed(Option<crate::BoxError>),
Expand Down Expand Up @@ -46,22 +46,20 @@ where
{
type Output = Result<T, crate::BoxError>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions tower/src/filter/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,7 +29,7 @@ where
service: S,
}

#[pin_project]
#[pin_project(project = StateProj)]
#[derive(Debug)]
enum State<Request, U> {
Check(Option<Request>),
Expand Down Expand Up @@ -59,14 +59,12 @@ where
{
type Output = Result<S::Response, Error>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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");
Expand All @@ -83,7 +81,7 @@ where
}
}
}
State::WaitResponse(response) => {
StateProj::WaitResponse(response) => {
return Poll::Ready(ready!(response.poll(cx)).map_err(Error::inner));
}
}
Expand Down
10 changes: 4 additions & 6 deletions tower/src/hedge/delay.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct ResponseFuture<Request, S, F> {
state: State<Request, F>,
}

#[pin_project]
#[pin_project(project = StateProj)]
#[derive(Debug)]
enum State<Request, F> {
Delaying(#[pin] tokio::time::Delay, Option<Request>),
Expand Down Expand Up @@ -84,20 +84,18 @@ where
{
type Output = Result<T, crate::BoxError>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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);
}
};
Expand Down
10 changes: 4 additions & 6 deletions tower/src/load_shed/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,7 +17,7 @@ pub struct ResponseFuture<F> {
state: ResponseState<F>,
}

#[pin_project]
#[pin_project(project = ResponseStateProj)]
enum ResponseState<F> {
Called(#[pin] F),
Overloaded,
Expand All @@ -44,12 +44,10 @@ where
{
type Output = Result<T, crate::BoxError>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[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())),
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions tower/src/reconnect/future.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pin_project::{pin_project, project};
use pin_project::pin_project;
use std::{
future::Future,
pin::Pin,
Expand All @@ -13,7 +13,7 @@ pub struct ResponseFuture<F, E> {
inner: Inner<F, E>,
}

#[pin_project]
#[pin_project(project = InnerProj)]
#[derive(Debug)]
enum Inner<F, E> {
Future(#[pin] F),
Expand Down Expand Up @@ -42,13 +42,11 @@ where
{
type Output = Result<T, crate::BoxError>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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))
}
Expand Down
12 changes: 5 additions & 7 deletions tower/src/retry/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -23,7 +23,7 @@ where
state: State<S::Future, P::Future>,
}

#[pin_project]
#[pin_project(project = StateProj)]
#[derive(Debug)]
enum State<F, P> {
/// Polling the future from `Service::call`
Expand Down Expand Up @@ -59,14 +59,12 @@ where
{
type Output = Result<S::Response, S::Error>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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()) {
Expand All @@ -80,15 +78,15 @@ where
return Poll::Ready(result);
}
}
State::Checking(future) => {
StateProj::Checking(future) => {
this.retry
.as_mut()
.project()
.policy
.set(ready!(future.poll(cx)));
this.state.set(State::Retrying);
}
State::Retrying => {
StateProj::Retrying => {
// NOTE: we assume here that
//
// this.retry.poll_ready()
Expand Down
10 changes: 4 additions & 6 deletions tower/src/util/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<A, B> {
/// One type of backing `Service`.
Expand Down Expand Up @@ -64,12 +64,10 @@ where
{
type Output = Result<T, crate::BoxError>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[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)?)),
}
}
}
12 changes: 5 additions & 7 deletions tower/src/util/oneshot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use futures_core::ready;
use pin_project::{pin_project, project};
use pin_project::pin_project;
use std::{
fmt,
future::Future,
Expand All @@ -18,7 +18,7 @@ pub struct Oneshot<S: Service<Req>, Req> {
state: State<S, Req>,
}

#[pin_project]
#[pin_project(project = StateProj)]
enum State<S: Service<Req>, Req> {
NotReady(S, Option<Req>),
Called(#[pin] S::Future),
Expand Down Expand Up @@ -62,23 +62,21 @@ where
{
type Output = Result<S::Response, S::Error>;

#[project]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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"),
}
}
}
Expand Down