Skip to content

Commit

Permalink
Use default sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-kong committed Aug 30, 2024
1 parent a40313b commit 0658a99
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
2 changes: 0 additions & 2 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ default = ["std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep"]
std-blocking-sleep = []
gloo-timers-sleep = ["dep:gloo-timers", "gloo-timers?/futures"]
tokio-sleep = ["dep:tokio", "tokio?/time"]
default = ["std"]
std = []

[dependencies]
fastrand = "2"
Expand Down
1 change: 0 additions & 1 deletion backon/src/blocking_retry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::time::Duration;
use std::thread;

use crate::backoff::BackoffBuilder;
use crate::blocking_sleep::MaybeBlockingSleeper;
Expand Down
7 changes: 3 additions & 4 deletions backon/src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::time::Duration;
use std::thread;

use crate::backoff::BackoffBuilder;
use crate::blocking_sleep::MaybeBlockingSleeper;
Expand Down Expand Up @@ -189,7 +188,7 @@ mod tests {
use anyhow::anyhow;
use anyhow::Result;
use core::time::Duration;
use std::sync::Mutex;
use spin::Mutex;

struct Test;

Expand All @@ -209,7 +208,7 @@ mod tests {

let (_, result) = {
|mut v: Test| {
let mut x = error_times.lock().unwrap();
let mut x = error_times.lock();
*x += 1;

let res = v.hello();
Expand All @@ -226,7 +225,7 @@ mod tests {
assert_eq!("not retryable", result.unwrap_err().to_string());
// `f` always returns error "not retryable", so it should be executed
// only once.
assert_eq!(*error_times.lock().unwrap(), 1);
assert_eq!(*error_times.lock(), 1);
Ok(())
}
}
4 changes: 2 additions & 2 deletions backon/src/blocking_sleep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

/// A sleeper is used sleep for a specified duration.
pub trait BlockingSleeper: 'static {
Expand All @@ -24,7 +24,7 @@ impl<F: Fn(Duration) + 'static> BlockingSleeper for F {
/// The default implementation of `Sleeper` when no features are enabled.
///
/// It will fail to compile if a containing [`Retry`][crate::Retry] is `.await`ed without calling [`Retry::sleep`][crate::Retry::sleep] to provide a valid sleeper.
#[cfg(all(not(feature = "tokio-sleep"), not(feature = "gloo-timers-sleep")))]
#[cfg(not(feature = "std-blocking-sleep"))]
pub type DefaultBlockingSleeper = PleaseEnableAFeatureOrProvideACustomSleeper;
/// The default implementation of `Sleeper` while feature `std-blocking-sleep` enabled.
///
Expand Down
10 changes: 4 additions & 6 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@
#![deny(missing_docs)]
#![deny(unused_qualifications)]
#![no_std]
#[cfg(feature = "std")]
extern crate alloc;

#[cfg(feature = "std-blocking-sleep")]
extern crate std;

extern crate alloc;

mod backoff;
pub use backoff::*;

Expand All @@ -133,14 +135,10 @@ pub use sleep::Sleeper;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))]
pub use sleep::TokioSleeper;

#[cfg(feature = "std")]
mod blocking_retry;
#[cfg(feature = "std")]
pub use blocking_retry::{BlockingRetry, BlockingRetryable};

#[cfg(feature = "std")]
mod blocking_retry_with_context;
#[cfg(feature = "std")]
pub use blocking_retry_with_context::{BlockingRetryWithContext, BlockingRetryableWithContext};

mod blocking_sleep;
Expand Down
5 changes: 3 additions & 2 deletions backon/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod default_sleeper_tests {
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use core::{future::ready, time::Duration};
use core::time::Duration;
use tokio::sync::Mutex;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -432,7 +432,8 @@ mod default_sleeper_tests {

#[cfg(test)]
mod custom_sleeper_tests {
use std::{future::ready, time::Duration};
use alloc::string::ToString;
use core::{future::ready, time::Duration};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down

0 comments on commit 0658a99

Please sign in to comment.