From 4fe68ed7af5873cb9f5f36124256a2bc00414ae1 Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Tue, 27 Aug 2024 21:00:25 +1000 Subject: [PATCH 1/6] Add no_std flag, hide blocking retrys behind std flag --- backon/Cargo.toml | 26 ++++++++++++----------- backon/src/backoff/api.rs | 4 ++-- backon/src/backoff/constant.rs | 4 ++-- backon/src/backoff/exponential.rs | 4 ++-- backon/src/backoff/fibonacci.rs | 4 ++-- backon/src/blocking_retry.rs | 8 +++---- backon/src/blocking_retry_with_context.rs | 4 ++-- backon/src/lib.rs | 18 ++++++++++------ backon/src/retry.rs | 14 ++++++------ backon/src/retry_with_context.rs | 16 +++++++------- backon/src/sleep.rs | 2 +- 11 files changed, 56 insertions(+), 48 deletions(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 842758a..2d169e3 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -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" @@ -37,18 +39,18 @@ reqwest = "0.12" [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", ] } diff --git a/backon/src/backoff/api.rs b/backon/src/backoff/api.rs index 4791cc8..3f8994c 100644 --- a/backon/src/backoff/api.rs +++ b/backon/src/backoff/api.rs @@ -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 { diff --git a/backon/src/backoff/constant.rs b/backon/src/backoff/constant.rs index 46a8963..749c7bf 100644 --- a/backon/src/backoff/constant.rs +++ b/backon/src/backoff/constant.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; @@ -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; diff --git a/backon/src/backoff/exponential.rs b/backon/src/backoff/exponential.rs index a08ab66..a292452 100644 --- a/backon/src/backoff/exponential.rs +++ b/backon/src/backoff/exponential.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; @@ -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; diff --git a/backon/src/backoff/fibonacci.rs b/backon/src/backoff/fibonacci.rs index 81ed294..2137c0b 100644 --- a/backon/src/backoff/fibonacci.rs +++ b/backon/src/backoff/fibonacci.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; @@ -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; diff --git a/backon/src/blocking_retry.rs b/backon/src/blocking_retry.rs index 72552c0..8c69575 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -1,5 +1,5 @@ use std::thread; -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; use crate::Backoff; @@ -138,7 +138,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::BlockingRetryable; @@ -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 { loop { let result = (self.f)(); @@ -199,7 +199,7 @@ where #[cfg(test)] mod tests { use std::sync::Mutex; - use std::time::Duration; + use core::time::Duration; use super::*; use crate::ExponentialBuilder; diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index 5ce6d77..586a3c8 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -1,5 +1,5 @@ use std::thread; -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; use crate::Backoff; @@ -147,7 +147,7 @@ where #[cfg(test)] mod tests { - use std::time::Duration; + use core::time::Duration; use anyhow::anyhow; use std::sync::Mutex; diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 02cdb3c..a63b971 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -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 { //! Ok("hello, world!".to_string()) @@ -69,7 +69,7 @@ //! use anyhow::Result; //! use backon::BlockingRetryable; //! use backon::ExponentialBuilder; -//! use std::time::Duration; +//! use core::time::Duration; //! //! fn fetch() -> Result { //! Ok("hello, world!".to_string()) @@ -95,6 +95,10 @@ #![deny(missing_docs)] #![deny(unused_qualifications)] +#![no_std] +#[cfg(feature = "std")] +extern crate std; + mod backoff; pub use backoff::*; @@ -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")] +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; diff --git a/backon/src/retry.rs b/backon/src/retry.rs index 25a9795..617a527 100644 --- a/backon/src/retry.rs +++ b/backon/src/retry.rs @@ -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; @@ -201,7 +201,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::ExponentialBuilder; diff --git a/backon/src/retry_with_context.rs b/backon/src/retry_with_context.rs index 36a7375..7de7bec 100644 --- a/backon/src/retry_with_context.rs +++ b/backon/src/retry_with_context.rs @@ -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; @@ -241,7 +241,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::ExponentialBuilder; @@ -362,7 +362,7 @@ where #[cfg(test)] mod tests { - use std::time::Duration; + use core::time::Duration; use anyhow::{anyhow, Result}; use tokio::sync::Mutex; diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 1b4a926..b491b02 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -1,4 +1,4 @@ -use std::{ +use core::{ future::{Future, Ready}, time::Duration, }; From b0e2a75795f4311d27da673c67e39a06b1a24b46 Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Tue, 27 Aug 2024 21:01:21 +1000 Subject: [PATCH 2/6] Add /.idea to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4fffb2f..ab1dd7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/.idea \ No newline at end of file From 7f6960ca93174ffcb65334873e105d2bb42ab04c Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Tue, 27 Aug 2024 21:04:51 +1000 Subject: [PATCH 3/6] Cargo fmt --- backon/src/blocking_retry.rs | 4 ++-- backon/src/blocking_retry_with_context.rs | 2 +- backon/src/lib.rs | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/backon/src/blocking_retry.rs b/backon/src/blocking_retry.rs index 8c69575..56a80f4 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -1,5 +1,5 @@ -use std::thread; use core::time::Duration; +use std::thread; use crate::backoff::BackoffBuilder; use crate::Backoff; @@ -198,8 +198,8 @@ where #[cfg(test)] mod tests { - use std::sync::Mutex; use core::time::Duration; + use std::sync::Mutex; use super::*; use crate::ExponentialBuilder; diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index 586a3c8..f61f78f 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -1,5 +1,5 @@ -use std::thread; use core::time::Duration; +use std::thread; use crate::backoff::BackoffBuilder; use crate::Backoff; diff --git a/backon/src/lib.rs b/backon/src/lib.rs index a63b971..705eb24 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -94,7 +94,6 @@ #![deny(missing_docs)] #![deny(unused_qualifications)] - #![no_std] #[cfg(feature = "std")] extern crate std; From 2e766869fc9d7c3f9db930df32f0fa323100ed3e Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Tue, 27 Aug 2024 21:18:06 +1000 Subject: [PATCH 4/6] Fix tests no_std --- backon/Cargo.toml | 1 + backon/src/blocking_retry.rs | 15 +++++++++------ backon/src/blocking_retry_with_context.rs | 11 +++++------ backon/src/lib.rs | 1 + backon/src/retry.rs | 5 ++++- backon/src/retry_with_context.rs | 4 ++-- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 2d169e3..c486847 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -36,6 +36,7 @@ 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 = [ diff --git a/backon/src/blocking_retry.rs b/backon/src/blocking_retry.rs index 56a80f4..227ad9b 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -198,8 +198,11 @@ where #[cfg(test)] mod tests { + use alloc::string::ToString; + use alloc::vec; + use alloc::vec::Vec; use core::time::Duration; - use std::sync::Mutex; + use spin::Mutex; use super::*; use crate::ExponentialBuilder; @@ -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")) }; @@ -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(()) } @@ -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")) }; @@ -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(()) } diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index f61f78f..383fd34 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -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) { let mut ctx = self.ctx.take().expect("context must be valid"); loop { @@ -147,14 +147,13 @@ where #[cfg(test)] mod tests { - use core::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; diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 705eb24..79bd9be 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -96,6 +96,7 @@ #![deny(unused_qualifications)] #![no_std] #[cfg(feature = "std")] +extern crate alloc; extern crate std; mod backoff; diff --git a/backon/src/retry.rs b/backon/src/retry.rs index 617a527..da3e7d7 100644 --- a/backon/src/retry.rs +++ b/backon/src/retry.rs @@ -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")] diff --git a/backon/src/retry_with_context.rs b/backon/src/retry_with_context.rs index 7de7bec..53f7ba2 100644 --- a/backon/src/retry_with_context.rs +++ b/backon/src/retry_with_context.rs @@ -362,9 +362,9 @@ where #[cfg(test)] mod tests { - use core::time::Duration; - + use alloc::string::ToString; use anyhow::{anyhow, Result}; + use core::time::Duration; use tokio::sync::Mutex; #[cfg(target_arch = "wasm32")] From 0658a99980d935d69048e1882dadf33e6ecef54b Mon Sep 17 00:00:00 2001 From: adrian-kong Date: Sat, 31 Aug 2024 09:02:24 +1000 Subject: [PATCH 5/6] Use default sleep --- backon/Cargo.toml | 2 -- backon/src/blocking_retry.rs | 1 - backon/src/blocking_retry_with_context.rs | 7 +++---- backon/src/blocking_sleep.rs | 4 ++-- backon/src/lib.rs | 10 ++++------ backon/src/retry.rs | 5 +++-- 6 files changed, 12 insertions(+), 17 deletions(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index b9614f1..110db12 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -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" diff --git a/backon/src/blocking_retry.rs b/backon/src/blocking_retry.rs index 5be5589..8194f5e 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -1,5 +1,4 @@ use core::time::Duration; -use std::thread; use crate::backoff::BackoffBuilder; use crate::blocking_sleep::MaybeBlockingSleeper; diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index 0d1e5de..e66fa46 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -1,5 +1,4 @@ use core::time::Duration; -use std::thread; use crate::backoff::BackoffBuilder; use crate::blocking_sleep::MaybeBlockingSleeper; @@ -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; @@ -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(); @@ -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(()) } } diff --git a/backon/src/blocking_sleep.rs b/backon/src/blocking_sleep.rs index 72b8ebe..5f5c09d 100644 --- a/backon/src/blocking_sleep.rs +++ b/backon/src/blocking_sleep.rs @@ -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 { @@ -24,7 +24,7 @@ impl 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. /// diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 386e2c6..a318cd2 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -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::*; @@ -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; diff --git a/backon/src/retry.rs b/backon/src/retry.rs index 90d4da4..e084139 100644 --- a/backon/src/retry.rs +++ b/backon/src/retry.rs @@ -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")] @@ -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; From a70bf148afc4150f4f978d0c11ada39ab05fc359 Mon Sep 17 00:00:00 2001 From: adrian-kong <35755741+adrian-kong@users.noreply.github.com> Date: Sat, 31 Aug 2024 09:03:51 +1000 Subject: [PATCH 6/6] line break --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ab1dd7e..8eb581d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target /Cargo.lock -/.idea \ No newline at end of file +/.idea