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 4 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
adrian-kong marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 15 additions & 12 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ 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]
gloo-timers-sleep = ["dep:gloo-timers", "gloo-timers?/futures"]
tokio-sleep = ["dep:tokio", "tokio?/time"]
default = ["std"]
std = []

[dependencies]
fastrand = "2.0.0"
Expand All @@ -34,21 +36,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,5 +1,5 @@
use core::time::Duration;
use std::thread;
use std::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::Backoff;
Expand Down Expand Up @@ -138,7 +138,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::BlockingRetryable;
Expand Down Expand Up @@ -171,7 +171,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 @@ -198,8 +198,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 @@ -224,7 +227,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 @@ -240,7 +243,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 @@ -249,8 +252,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 @@ -266,7 +269,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
13 changes: 6 additions & 7 deletions backon/src/blocking_retry_with_context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::time::Duration;
use std::thread;
use std::time::Duration;

use crate::backoff::BackoffBuilder;
use crate::Backoff;
Expand Down Expand Up @@ -117,7 +117,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 @@ -147,14 +147,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 std::sync::Mutex;

struct Test;

Expand Down
18 changes: 12 additions & 6 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -69,7 +69,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 @@ -94,6 +94,10 @@

#![deny(missing_docs)]
#![deny(unused_qualifications)]
#![no_std]
#[cfg(feature = "std")]
extern crate alloc;
extern crate std;

mod backoff;
pub use backoff::*;
Expand All @@ -114,13 +118,15 @@ pub use sleep::Sleeper;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))]
pub use sleep::TokioSleeper;

#[cfg(feature = "std")]
mod blocking_retry;
pub use blocking_retry::BlockingRetry;
pub use blocking_retry::BlockingRetryable;
#[cfg(feature = "std")]
adrian-kong marked this conversation as resolved.
Show resolved Hide resolved
pub use blocking_retry::{BlockingRetry, BlockingRetryable};

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

#[cfg(docsrs)]
pub mod docs;
19 changes: 11 additions & 8 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::Backoff;
Expand Down Expand Up @@ -201,7 +201,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::ExponentialBuilder;
Expand Down Expand Up @@ -320,7 +320,10 @@ where
#[cfg(test)]
#[cfg(any(feature = "tokio-sleep", feature = "gloo-timers-sleep"))]
mod tests {
use std::{future::ready, time::Duration};
use alloc::string::ToString;
use alloc::vec;
use alloc::vec::Vec;
use core::{future::ready, time::Duration};
use tokio::sync::Mutex;

#[cfg(target_arch = "wasm32")]
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::Backoff;
Expand Down Expand Up @@ -241,7 +241,7 @@ where
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use core::time::Duration;
///
/// use anyhow::Result;
/// use backon::ExponentialBuilder;
Expand Down Expand Up @@ -362,9 +362,9 @@ where

#[cfg(test)]
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