/rust/registry/src/index.crates.io-1949cf8c6b5b557f/backon-1.6.0/src/blocking_sleep.rs
Line | Count | Source |
1 | | use core::time::Duration; |
2 | | |
3 | | /// A sleeper is used sleep for a specified duration. |
4 | | pub trait BlockingSleeper: 'static { |
5 | | /// sleep for a specified duration. |
6 | | fn sleep(&self, dur: Duration); |
7 | | } |
8 | | |
9 | | /// A stub trait allowing non-[`BlockingSleeper`] types to be used as a generic parameter in [`BlockingRetry`][crate::BlockingRetry]. |
10 | | /// It does not provide actual functionality. |
11 | | #[doc(hidden)] |
12 | | pub trait MaybeBlockingSleeper: 'static {} |
13 | | |
14 | | /// All `BlockingSleeper` will implement `MaybeBlockingSleeper`, but not vice versa. |
15 | | impl<T: BlockingSleeper + ?Sized> MaybeBlockingSleeper for T {} |
16 | | |
17 | | /// All `Fn(Duration)` implements `Sleeper`. |
18 | | impl<F: Fn(Duration) + 'static> BlockingSleeper for F { |
19 | 0 | fn sleep(&self, dur: Duration) { |
20 | 0 | self(dur) |
21 | 0 | } |
22 | | } |
23 | | |
24 | | /// The default implementation of `Sleeper` when no features are enabled. |
25 | | /// |
26 | | /// 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. |
27 | | #[cfg(not(feature = "std-blocking-sleep"))] |
28 | | pub type DefaultBlockingSleeper = PleaseEnableAFeatureOrProvideACustomSleeper; |
29 | | /// The default implementation of `Sleeper` while feature `std-blocking-sleep` enabled. |
30 | | /// |
31 | | /// it uses [`std::thread::sleep`]. |
32 | | #[cfg(feature = "std-blocking-sleep")] |
33 | | pub type DefaultBlockingSleeper = StdSleeper; |
34 | | |
35 | | /// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one. |
36 | | /// |
37 | | /// 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`]. |
38 | | #[doc(hidden)] |
39 | | #[allow(dead_code)] |
40 | | #[derive(Clone, Copy, Debug, Default)] |
41 | | pub struct PleaseEnableAFeatureOrProvideACustomSleeper; |
42 | | |
43 | | /// Implement `MaybeSleeper` but not `Sleeper`. |
44 | | impl MaybeBlockingSleeper for PleaseEnableAFeatureOrProvideACustomSleeper {} |
45 | | |
46 | | /// The implementation of `StdSleeper` uses [`std::thread::sleep`]. |
47 | | #[cfg(feature = "std-blocking-sleep")] |
48 | | #[derive(Clone, Copy, Debug, Default)] |
49 | | pub struct StdSleeper; |
50 | | |
51 | | #[cfg(feature = "std-blocking-sleep")] |
52 | | impl BlockingSleeper for StdSleeper { |
53 | 0 | fn sleep(&self, dur: Duration) { |
54 | 0 | std::thread::sleep(dur) |
55 | 0 | } |
56 | | } |