Skip to content

Commit

Permalink
Merge pull request #45 from jplatte/pin-project-lite
Browse files Browse the repository at this point in the history
Replace pin-project with pin-project-lite
  • Loading branch information
ihrwein authored Nov 18, 2021
2 parents 7198500 + 71a6f5f commit 06ae595
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ travis-ci = { repository = "ihrwein/backoff" }
async_std_1 = { package = "async-std", version = "1.9", optional = true }
futures-core = { version = "0.3.8", default-features = false, optional = true }
instant = "0.1"
pin-project = { version = "1.0", optional = true }
pin-project-lite = { version = "0.2.7", optional = true }
rand = "0.8"
getrandom = "0.2"
tokio_1 = { package = "tokio", version = "1.0", features = ["time"], optional = true }
Expand All @@ -34,7 +34,7 @@ futures-executor = "0.3"
[features]
default = []
wasm-bindgen = ["instant/wasm-bindgen", "getrandom/js"]
futures = ["futures-core", "pin-project"]
futures = ["futures-core", "pin-project-lite"]
tokio = ["futures", "tokio_1"]
async-std = ["futures", "async_std_1"]

Expand Down
57 changes: 32 additions & 25 deletions src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use futures_core::ready;
use pin_project::pin_project;
use pin_project_lite::pin_project;

use crate::{backoff::Backoff, error::Error};

Expand Down Expand Up @@ -105,28 +105,29 @@ where
Retry::new(rt_sleeper(), backoff, notify, operation)
}

/// Retry implementation.
#[pin_project]
pub struct Retry<S: Sleeper, B, N, Fn, Fut> {
/// The [`Sleeper`] that we generate the `delay` futures from.
sleeper: S,
pin_project! {
/// Retry implementation.
pub struct Retry<S: Sleeper, B, N, Fn, Fut> {
// The [`Sleeper`] that we generate the `delay` futures from.
sleeper: S,

/// [`Backoff`] implementation to count next [`Retry::delay`] with.
backoff: B,
// [`Backoff`] implementation to count next [`Retry::delay`] with.
backoff: B,

/// [`Future`] which delays execution before next [`Retry::operation`] invocation.
#[pin]
delay: OptionPinned<S::Sleep>,
// [`Future`] which delays execution before next [`Retry::operation`] invocation.
#[pin]
delay: OptionPinned<S::Sleep>,

/// Operation to be retried. It must return [`Future`].
operation: Fn,
// Operation to be retried. It must return [`Future`].
operation: Fn,

/// [`Future`] being resolved once [`Retry::operation`] is completed.
#[pin]
fut: Fut,
// [`Future`] being resolved once [`Retry::operation`] is completed.
#[pin]
fut: Fut,

/// [`Notify`] implementation to track [`Retry`] ticks.
notify: N,
// [`Notify`] implementation to track [`Retry`] ticks.
notify: N,
}
}

impl<S, B, N, Fn, Fut, I, E> Retry<S, B, N, Fn, Fut>
Expand All @@ -148,10 +149,15 @@ where
}
}

#[pin_project(project = OptionProj)]
enum OptionPinned<T> {
Some(#[pin] T),
None,
pin_project! {
#[project = OptionProj]
enum OptionPinned<T> {
Some {
#[pin]
inner: T,
},
None,
}
}

impl<S, B, N, Fn, Fut, I, E> Future for Retry<S, B, N, Fn, Fut>
Expand All @@ -168,7 +174,7 @@ where
let mut this = self.project();

loop {
if let OptionProj::Some(delay) = this.delay.as_mut().project() {
if let OptionProj::Some { inner: delay } = this.delay.as_mut().project() {
ready!(delay.poll(cx));
this.delay.set(OptionPinned::None);
}
Expand All @@ -180,8 +186,9 @@ where
match duration.or_else(|| this.backoff.next_backoff()) {
Some(duration) => {
this.notify.notify(e, duration);
this.delay
.set(OptionPinned::Some(this.sleeper.sleep(duration)));
this.delay.set(OptionPinned::Some {
inner: this.sleeper.sleep(duration),
});
this.fut.set((this.operation)());
}
None => return Poll::Ready(Err(e)),
Expand Down

0 comments on commit 06ae595

Please sign in to comment.