-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into adrian/no_std
- Loading branch information
Showing
9 changed files
with
269 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::time::Duration; | ||
|
||
/// A sleeper is used sleep for a specified duration. | ||
pub trait BlockingSleeper: 'static { | ||
/// sleep for a specified duration. | ||
fn sleep(&self, dur: Duration); | ||
} | ||
|
||
/// A stub trait allowing non-[`BlockingSleeper`] types to be used as a generic parameter in [`BlockingRetry`][crate::BlockingRetry]. | ||
/// It does not provide actual functionality. | ||
#[doc(hidden)] | ||
pub trait MaybeBlockingSleeper: 'static {} | ||
|
||
/// All `BlockingSleeper` will implement `MaybeBlockingSleeper`, but not vice versa. | ||
impl<T: BlockingSleeper + ?Sized> MaybeBlockingSleeper for T {} | ||
|
||
/// All `Fn(Duration)` implements `Sleeper`. | ||
impl<F: Fn(Duration) + 'static> BlockingSleeper for F { | ||
fn sleep(&self, dur: Duration) { | ||
self(dur) | ||
} | ||
} | ||
|
||
/// 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")))] | ||
pub type DefaultBlockingSleeper = PleaseEnableAFeatureOrProvideACustomSleeper; | ||
/// The default implementation of `Sleeper` while feature `std-blocking-sleep` enabled. | ||
/// | ||
/// it uses [`std::thread::sleep`]. | ||
#[cfg(feature = "std-blocking-sleep")] | ||
pub type DefaultBlockingSleeper = StdSleeper; | ||
|
||
/// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one. | ||
/// | ||
/// Users should enable a feature of this crate that provides a valid [`Sleeper`] implementation when this type appears in compilation errors. Alternatively, a custom [`Sleeper`] implementation should be provided where necessary, such as in [`crate::Retry::sleeper`]. | ||
#[doc(hidden)] | ||
#[derive(Clone, Copy, Debug, Default)] | ||
pub struct PleaseEnableAFeatureOrProvideACustomSleeper; | ||
|
||
/// Implement `MaybeSleeper` but not `Sleeper`. | ||
impl MaybeBlockingSleeper for PleaseEnableAFeatureOrProvideACustomSleeper {} | ||
|
||
/// The implementation of `StdSleeper` uses [`std::thread::sleep`]. | ||
#[cfg(feature = "std-blocking-sleep")] | ||
#[derive(Clone, Copy, Debug, Default)] | ||
pub struct StdSleeper; | ||
|
||
#[cfg(feature = "std-blocking-sleep")] | ||
impl BlockingSleeper for StdSleeper { | ||
fn sleep(&self, dur: Duration) { | ||
std::thread::sleep(dur) | ||
} | ||
} |
Oops, something went wrong.