Skip to content

Commit

Permalink
docs: Add docs for dummy sleeper related
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbai committed Aug 30, 2024
1 parent 69a5e64 commit d270d0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
1 change: 0 additions & 1 deletion backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ mod sleep;
pub use sleep::DefaultSleeper;
#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))]
pub use sleep::GlooTimersSleep;
pub(crate) use sleep::PleaseEnableAFeatureForSleeper;
pub use sleep::Sleeper;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))]
pub use sleep::TokioSleeper;
Expand Down
7 changes: 0 additions & 7 deletions backon/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,6 @@ where
type Output = Result<T, E>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[cfg(debug_assertions)]
if std::any::TypeId::of::<SF>()
== std::any::TypeId::of::<crate::PleaseEnableAFeatureForSleeper>()
{
panic!("BackON: No sleeper has been configured. Please enable the features or provide a custom implementation.")
}

// Safety: This is safe because we don't move the `Retry` struct itself,
// only its internal state.
//
Expand Down
7 changes: 0 additions & 7 deletions backon/src/retry_with_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,6 @@ where
type Output = (Ctx, Result<T, E>);

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[cfg(debug_assertions)]
if std::any::TypeId::of::<SF>()
== std::any::TypeId::of::<crate::PleaseEnableAFeatureForSleeper>()
{
panic!("BackON: No sleeper has been configured. Please enable the features or provide a custom implementation.")
}

// Safety: This is safe because we don't move the `Retry` struct itself,
// only its internal state.
//
Expand Down
19 changes: 15 additions & 4 deletions backon/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::{
time::Duration,
};

#[cfg(doc)]
use crate::Retry;

/// A sleeper is used to generate a future that completes after a specified duration.
pub trait Sleeper: 'static {
/// The future returned by the `sleep` method.
Expand All @@ -12,14 +15,16 @@ pub trait Sleeper: 'static {
fn sleep(&self, dur: Duration) -> Self::Sleep;
}

/// A stub trait allowing non-[`Sleeper`] types to be used as a generic parameter in [`Retry`].
/// It does not provide actual functionality.
#[doc(hidden)]
pub trait MayBeDefaultSleeper: 'static {
type Sleep: Future<Output = ()>;
}

/// The default implementation of `Sleeper` is a no-op when no features are enabled.
///
/// It will panic on `debug` profile and do nothing on `release` profile.
/// The default implementation of `Sleeper` when no features are enabled.
/// It will fail to compile if a containing [`Retry`] is `.await`ed without calling [`Retry::sleep`]
/// to provide a valid sleeper.
#[cfg(all(not(feature = "tokio-sleep"), not(feature = "gloo-timers-sleep")))]
pub type DefaultSleeper = PleaseEnableAFeatureForSleeper;
/// The default implementation of `Sleeper` while feature `tokio-sleep` enabled.
Expand All @@ -33,7 +38,13 @@ pub type DefaultSleeper = TokioSleeper;
#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))]
pub type DefaultSleeper = GlooTimersSleep;

/// The no-op implementation of `Sleeper` that does nothing.
/// A stub type that does not implement [`Sleeper`] and hence will fail to compile if used as a
/// sleeper.
///
/// Users are expected to enable a feature of this crate that provides a valid implementation of
/// [`Sleeper`] when they see this type appearing in compilation errors. Otherwise, a custom [`Sleeper`]
/// implementation should be provided where needed, such as [`Retry::sleeper`].
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Default)]
pub struct PleaseEnableAFeatureForSleeper;

Expand Down

0 comments on commit d270d0e

Please sign in to comment.