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

Add no_std flag, hide blocking retrys behind std flag #125

Merged
merged 7 commits into from
Aug 31, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/.idea
25 changes: 13 additions & 12 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ repository.workspace = true
[package.metadata.docs.rs]
all-features = true
targets = [
"x86_64-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"wasm32-unknown-unknown",
"x86_64-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"wasm32-unknown-unknown",
]

[features]
Expand All @@ -37,21 +37,22 @@ gloo-timers = { version = "0.3", optional = true }
[dev-dependencies]
anyhow = "1"
reqwest = "0.12"
spin = "0.9.8"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
tokio = { version = "1", features = [
"macros",
"rt",
"sync",
"macros",
"rt",
"sync",
], default-features = false }
wasm-bindgen-test = "0.3"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
sqlx = { version = "0.8.0", features = ["runtime-tokio", "sqlite"] }
tokio = { version = "1", features = [
"time",
"rt",
"macros",
"sync",
"rt-multi-thread",
"time",
"rt",
"macros",
"sync",
"rt-multi-thread",
] }
4 changes: 2 additions & 2 deletions backon/src/backoff/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::Debug;
use std::time::Duration;
use core::fmt::Debug;
use core::time::Duration;

/// BackoffBuilder is utilized to construct a new backoff.
pub trait BackoffBuilder: Debug + Send + Sync + Unpin {
Expand Down
4 changes: 2 additions & 2 deletions backon/src/backoff/constant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::backoff::BackoffBuilder;

Expand Down Expand Up @@ -121,7 +121,7 @@ impl Iterator for ConstantBackoff {

#[cfg(test)]
mod tests {
use std::time::Duration;
use core::time::Duration;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down
4 changes: 2 additions & 2 deletions backon/src/backoff/exponential.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::backoff::BackoffBuilder;

Expand Down Expand Up @@ -182,7 +182,7 @@ pub(crate) fn saturating_mul(d: Duration, rhs: f32) -> Duration {

#[cfg(test)]
mod tests {
use std::time::Duration;
use core::time::Duration;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down
4 changes: 2 additions & 2 deletions backon/src/backoff/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::backoff::BackoffBuilder;

Expand Down Expand Up @@ -164,7 +164,7 @@ impl Iterator for FibonacciBackoff {

#[cfg(test)]
mod tests {
use std::time::Duration;
use core::time::Duration;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
Expand Down
23 changes: 13 additions & 10 deletions backon/src/blocking_retry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::blocking_sleep::MaybeBlockingSleeper;
Expand Down Expand Up @@ -183,7 +183,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::BlockingRetryable;
Expand Down Expand Up @@ -229,7 +229,7 @@ where
{
/// Call the retried function.
///
/// TODO: implement [`std::ops::FnOnce`] after it stable.
/// TODO: implement [`FnOnce`] after it stable.
pub fn call(mut self) -> Result<T, E> {
loop {
let result = (self.f)();
Expand All @@ -255,8 +255,11 @@ where
}
#[cfg(test)]
mod tests {
use std::sync::Mutex;
use std::time::Duration;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use core::time::Duration;
use spin::Mutex;

use super::*;
use crate::ExponentialBuilder;
Expand All @@ -281,7 +284,7 @@ mod tests {
let error_times = Mutex::new(0);

let f = || {
let mut x = error_times.lock().unwrap();
let mut x = error_times.lock();
*x += 1;
Err::<(), anyhow::Error>(anyhow::anyhow!("not retryable"))
};
Expand All @@ -297,7 +300,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(())
}

Expand All @@ -306,8 +309,8 @@ mod tests {
let error_times = Mutex::new(0);

let f = || {
println!("I have been called!");
let mut x = error_times.lock().unwrap();
// println!("I have been called!");
let mut x = error_times.lock();
*x += 1;
Err::<(), anyhow::Error>(anyhow::anyhow!("retryable"))
};
Expand All @@ -323,7 +326,7 @@ mod tests {
assert_eq!("retryable", result.unwrap_err().to_string());
// `f` always returns error "retryable", so it should be executed
// 4 times (retry 3 times).
assert_eq!(*error_times.lock().unwrap(), 4);
assert_eq!(*error_times.lock(), 4);
Ok(())
}

Expand Down
17 changes: 8 additions & 9 deletions backon/src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::blocking_sleep::MaybeBlockingSleeper;
Expand Down Expand Up @@ -152,7 +152,7 @@ where
{
/// Call the retried function.
///
/// TODO: implement [`std::ops::FnOnce`] after it stable.
/// TODO: implement [`FnOnce`] after it stable.
pub fn call(mut self) -> (Ctx, Result<T, E>) {
let mut ctx = self.ctx.take().expect("context must be valid");
loop {
Expand Down Expand Up @@ -182,14 +182,13 @@ where

#[cfg(test)]
mod tests {
use std::time::Duration;

use anyhow::anyhow;
use std::sync::Mutex;

use super::*;
use crate::ExponentialBuilder;
use alloc::string::ToString;
use anyhow::anyhow;
use anyhow::Result;
use core::time::Duration;
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")))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh this was not compiling for me

#[cfg(not(feature = "std-blocking-sleep"))]
pub type DefaultBlockingSleeper = PleaseEnableAFeatureOrProvideACustomSleeper;
/// The default implementation of `Sleeper` while feature `std-blocking-sleep` enabled.
///
Expand Down
16 changes: 10 additions & 6 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! use anyhow::Result;
//! use backon::ExponentialBuilder;
//! use backon::Retryable;
//! use std::time::Duration;
//! use core::time::Duration;
//!
//! async fn fetch() -> Result<String> {
//! Ok("hello, world!".to_string())
Expand Down Expand Up @@ -84,7 +84,7 @@
//! use anyhow::Result;
//! use backon::BlockingRetryable;
//! use backon::ExponentialBuilder;
//! use std::time::Duration;
//! use core::time::Duration;
//!
//! fn fetch() -> Result<String> {
//! Ok("hello, world!".to_string())
Expand All @@ -109,6 +109,12 @@

#![deny(missing_docs)]
#![deny(unused_qualifications)]
#![no_std]

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

extern crate alloc;

mod backoff;
pub use backoff::*;
Expand All @@ -130,12 +136,10 @@ pub use sleep::Sleeper;
pub use sleep::TokioSleeper;

mod blocking_retry;
pub use blocking_retry::BlockingRetry;
pub use blocking_retry::BlockingRetryable;
pub use blocking_retry::{BlockingRetry, BlockingRetryable};

mod blocking_retry_with_context;
pub use blocking_retry_with_context::BlockingRetryWithContext;
pub use blocking_retry_with_context::BlockingRetryableWithContext;
pub use blocking_retry_with_context::{BlockingRetryWithContext, BlockingRetryableWithContext};

mod blocking_sleep;
pub use blocking_sleep::BlockingSleeper;
Expand Down
22 changes: 13 additions & 9 deletions backon/src/retry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::future::Future;
use std::pin::Pin;
use std::task::ready;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;
use core::future::Future;
use core::pin::Pin;
use core::task::ready;
use core::task::Context;
use core::task::Poll;
use core::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::sleep::MaybeSleeper;
Expand Down Expand Up @@ -202,7 +202,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::ExponentialBuilder;
Expand Down Expand Up @@ -321,7 +321,10 @@ where
#[cfg(test)]
#[cfg(any(feature = "tokio-sleep", feature = "gloo-timers-sleep"))]
mod default_sleeper_tests {
use std::time::Duration;
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use core::time::Duration;
use tokio::sync::Mutex;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -429,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
18 changes: 9 additions & 9 deletions backon/src/retry_with_context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::future::Future;
use std::pin::Pin;
use std::task::ready;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;
use core::future::Future;
use core::pin::Pin;
use core::task::ready;
use core::task::Context;
use core::task::Poll;
use core::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::sleep::MaybeSleeper;
Expand Down Expand Up @@ -242,7 +242,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::ExponentialBuilder;
Expand Down Expand Up @@ -364,9 +364,9 @@ where
#[cfg(test)]
#[cfg(any(feature = "tokio-sleep", feature = "gloo-timers-sleep"))]
mod tests {
use std::time::Duration;

use alloc::string::ToString;
use anyhow::{anyhow, Result};
use core::time::Duration;
use tokio::sync::Mutex;

#[cfg(target_arch = "wasm32")]
Expand Down
2 changes: 1 addition & 1 deletion backon/src/sleep.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{
use core::{
future::{Future, Ready},
time::Duration,
};
Expand Down