diff --git a/.gitignore b/.gitignore index 4fffb2f..8eb581d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/.idea diff --git a/backon/Cargo.toml b/backon/Cargo.toml index e83feb9..110db12 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -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] @@ -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", ] } 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 cb901ce..8194f5e 100644 --- a/backon/src/blocking_retry.rs +++ b/backon/src/blocking_retry.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; use crate::blocking_sleep::MaybeBlockingSleeper; @@ -183,7 +183,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::BlockingRetryable; @@ -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 { loop { let result = (self.f)(); @@ -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; @@ -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")) }; @@ -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(()) } @@ -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")) }; @@ -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(()) } diff --git a/backon/src/blocking_retry_with_context.rs b/backon/src/blocking_retry_with_context.rs index 1bb1db1..e66fa46 100644 --- a/backon/src/blocking_retry_with_context.rs +++ b/backon/src/blocking_retry_with_context.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use core::time::Duration; use crate::backoff::BackoffBuilder; use crate::blocking_sleep::MaybeBlockingSleeper; @@ -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) { let mut ctx = self.ctx.take().expect("context must be valid"); loop { @@ -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; @@ -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 917be53..a318cd2 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -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 { //! Ok("hello, world!".to_string()) @@ -84,7 +84,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()) @@ -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::*; @@ -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; diff --git a/backon/src/retry.rs b/backon/src/retry.rs index b70e5fc..e084139 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::sleep::MaybeSleeper; @@ -202,7 +202,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::ExponentialBuilder; @@ -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")] @@ -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; diff --git a/backon/src/retry_with_context.rs b/backon/src/retry_with_context.rs index 4452fbd..2eba93d 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::sleep::MaybeSleeper; @@ -242,7 +242,7 @@ where /// # Examples /// /// ```no_run - /// use std::time::Duration; + /// use core::time::Duration; /// /// use anyhow::Result; /// use backon::ExponentialBuilder; @@ -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")] diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 0aaeb94..4a5ab46 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -1,4 +1,4 @@ -use std::{ +use core::{ future::{Future, Ready}, time::Duration, };