relentless 0.9.0

Composable retry policies for fallible operations and polling.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use core::fmt;

#[cfg(feature = "alloc")]
use super::super::HookChain;
use super::super::execution::sync_exec::{NoSyncSleep, SyncRetryCore, SyncSleep};
use super::super::time::ElapsedTracker;
use super::super::{AttemptHook, BeforeAttemptHook, ExecutionHooks, ExitHook, RetryPolicy};
use crate::compat::Duration;
use crate::predicate::Predicate;
use crate::state::{AttemptState, ExitState, RetryState};
use crate::{
    RetryError, RetryStats, predicate,
    stop::{self, Stop},
    wait::{self, Wait},
};

/// Extension trait to start sync retries directly from a closure/function.
///
/// The operation takes no parameters. Use the free function [`crate::retry`]
/// when you need access to [`RetryState`].
pub trait RetryExt<T, E>: FnMut() -> Result<T, E> + Sized {
    /// Starts an owned sync retry builder from [`RetryPolicy::default()`].
    ///
    /// This means extension-based retries default to:
    /// - `stop::attempts(3)`
    /// - exponential backoff starting at 100ms
    /// - retry on any error
    ///
    /// In `std` builds, `.call()` works without `.sleep(...)`. The example
    /// below still configures `.sleep(...)` so it also works in non-`std`
    /// documentation tests.
    ///
    /// ```
    /// use relentless::RetryExt;
    ///
    /// let _ = (|| Ok::<(), &str>(()))
    ///     .retry()
    ///     .sleep(|_| {})
    ///     .call();
    /// ```
    fn retry(self) -> DefaultSyncRetryBuilder<Self, T, E>;
}

/// Adapts a no-argument closure to the [`RetryOp`] trait by discarding the
/// [`RetryState`] parameter that the execution engine always passes.
///
/// This is the bridge between the ext-trait API (`FnMut() -> Result`) and the
/// execution engine, which requires `FnMut(RetryState) -> Result`.
#[doc(hidden)]
pub struct StatelessOp<F>(F);

impl<T, E, F: FnMut() -> Result<T, E>> super::super::execution::common::RetryOp<T, E>
    for StatelessOp<F>
{
    fn call_op(&mut self, _state: RetryState) -> Result<T, E> {
        (self.0)()
    }
}

impl<T, E, F> RetryExt<T, E> for F
where
    F: FnMut() -> Result<T, E> + Sized,
{
    fn retry(self) -> DefaultSyncRetryBuilder<Self, T, E> {
        SyncRetryBuilder {
            inner: SyncRetryCore::new(
                RetryPolicy::default(),
                ExecutionHooks::new(),
                StatelessOp(self),
                NoSyncSleep,
                ElapsedTracker::new(None),
            ),
        }
    }
}

/// Alias for the default owned sync retry builder returned by [`RetryExt::retry`].
///
/// This hides the default stop, wait, predicate, hook, and sleeper
/// state from user-facing type signatures.
pub type DefaultSyncRetryBuilder<F, T, E> = SyncRetryBuilder<
    stop::StopAfterAttempts,
    wait::WaitExponential,
    predicate::PredicateAnyError,
    (),
    (),
    (),
    StatelessOp<F>,
    NoSyncSleep,
    T,
    E,
>;

/// Alias for the default owned sync retry builder-with-stats returned by
/// calling `.with_stats()` on [`RetryExt::retry`].
pub type DefaultSyncRetryBuilderWithStats<F, SleepFn, T, E> = SyncRetryBuilderWithStats<
    stop::StopAfterAttempts,
    wait::WaitExponential,
    predicate::PredicateAnyError,
    (),
    (),
    (),
    StatelessOp<F>,
    SleepFn,
    T,
    E,
>;

#[cfg(not(feature = "std"))]
#[doc(hidden)]
/// ```compile_fail
/// use relentless::RetryExt;
///
/// let _ = (|| Err::<(), &str>("fail"))
///     .retry()
///     .call();
/// ```
#[allow(dead_code)]
fn _sync_retry_builder_requires_sleep_in_no_std() {}

/// Owned sync retry builder created from [`RetryExt::retry`].
///
/// # Examples
///
/// ```
/// use core::time::Duration;
/// use relentless::{RetryExt, stop};
///
/// let retry = (|| Ok::<u32, &str>(1))
///     .retry()
///     .stop(stop::attempts(2))
///     .sleep(|_dur: Duration| {});
///
/// let _ = retry;
/// ```
#[allow(clippy::type_complexity)]
pub struct SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E> {
    inner: SyncRetryCore<RetryPolicy<S, W, P>, BA, AA, OX, F, SleepFn, T, E>,
}

impl<S, W, P, F, T, E> SyncRetryBuilder<S, W, P, (), (), (), F, NoSyncSleep, T, E> {
    pub(crate) fn from_policy(policy: RetryPolicy<S, W, P>, op: F) -> Self {
        SyncRetryBuilder {
            inner: SyncRetryCore::new(
                policy,
                ExecutionHooks::new(),
                op,
                NoSyncSleep,
                ElapsedTracker::new(None),
            ),
        }
    }
}

impl<S, W, P, BA, AA, OX, F, SleepFn, T, E> fmt::Debug
    for SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SyncRetryBuilder").finish_non_exhaustive()
    }
}

/// Owned sync retry builder wrapper that returns statistics.
///
/// # Examples
///
/// ```
/// use core::time::Duration;
/// use relentless::RetryExt;
///
/// let retry = (|| Ok::<u32, &str>(1))
///     .retry()
///     .sleep(|_dur: Duration| {})
///     .with_stats();
///
/// let _ = retry;
/// ```
pub struct SyncRetryBuilderWithStats<S, W, P, BA, AA, OX, F, SleepFn, T, E> {
    inner: SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E>,
}

impl<S, W, P, BA, AA, OX, F, SleepFn, T, E> fmt::Debug
    for SyncRetryBuilderWithStats<S, W, P, BA, AA, OX, F, SleepFn, T, E>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SyncRetryBuilderWithStats")
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "alloc")]
type SyncBuilderWithBeforeHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> =
    SyncRetryBuilder<S, W, P, HookChain<BA, Hook>, AA, OX, F, SleepFn, T, E>;

#[cfg(feature = "alloc")]
type SyncBuilderWithAfterHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> =
    SyncRetryBuilder<S, W, P, BA, HookChain<AA, Hook>, OX, F, SleepFn, T, E>;

#[cfg(feature = "alloc")]
type SyncBuilderWithOnExitHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> =
    SyncRetryBuilder<S, W, P, BA, AA, HookChain<OX, Hook>, F, SleepFn, T, E>;

impl<S, W, P, BA, AA, OX, F, SleepFn, T, E>
    SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E>
{
    fn map_hooks<NewBA, NewAA, NewOX>(
        self,
        map: impl FnOnce(ExecutionHooks<BA, AA, OX>) -> ExecutionHooks<NewBA, NewAA, NewOX>,
    ) -> SyncRetryBuilder<S, W, P, NewBA, NewAA, NewOX, F, SleepFn, T, E> {
        SyncRetryBuilder {
            inner: self.inner.map_hooks(map),
        }
    }

    /// Sets the stop condition for the retry policy.
    #[must_use]
    pub fn stop<NewStop>(
        self,
        stop: NewStop,
    ) -> SyncRetryBuilder<NewStop, W, P, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryBuilder {
            inner: self.inner.map_policy(|policy| policy.stop(stop)),
        }
    }

    /// Sets the wait strategy used between retry attempts.
    #[must_use]
    pub fn wait<NewWait>(
        self,
        wait: NewWait,
    ) -> SyncRetryBuilder<S, NewWait, P, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryBuilder {
            inner: self.inner.map_policy(|policy| policy.wait(wait)),
        }
    }

    /// Sets the predicate that decides whether a failed attempt should be retried.
    #[must_use]
    pub fn when<NewPredicate>(
        self,
        predicate: NewPredicate,
    ) -> SyncRetryBuilder<S, W, NewPredicate, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryBuilder {
            inner: self.inner.map_policy(|policy| policy.when(predicate)),
        }
    }

    /// Sets a predicate that retries *until* `p.should_retry()` returns `true`.
    ///
    /// Wraps `p` in [`PredicateUntil`](crate::predicate::PredicateUntil).
    /// Natural for polling: `.until(ok(|s| s.is_ready()))`.
    #[must_use]
    pub fn until<NewPredicate>(
        self,
        predicate: NewPredicate,
    ) -> SyncRetryBuilder<S, W, predicate::PredicateUntil<NewPredicate>, BA, AA, OX, F, SleepFn, T, E>
    {
        SyncRetryBuilder {
            inner: self.inner.map_policy(|policy| policy.until(predicate)),
        }
    }

    /// Configures a custom elapsed clock for elapsed-based stop conditions.
    #[must_use]
    pub fn elapsed_clock(self, clock: fn() -> Duration) -> Self {
        SyncRetryBuilder {
            inner: self.inner.set_elapsed_clock(clock),
        }
    }

    /// Configures a custom elapsed clock from a boxed closure.
    ///
    /// This variant supports closures with captures for test clocks and
    /// runtime state. Requires the `alloc` feature.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn elapsed_clock_fn(self, clock: impl Fn() -> Duration + 'static) -> Self {
        SyncRetryBuilder {
            inner: self
                .inner
                .set_elapsed_clock_fn(crate::compat::Box::new(clock)),
        }
    }

    /// Sets a wall-clock deadline for the entire retry execution.
    ///
    /// Timeout combines two behaviors:
    /// 1. Implicitly ORs `stop::elapsed(dur)` into the effective stop strategy.
    /// 2. Clamps each sleep delay to the remaining budget.
    ///
    /// With `std`, the Instant clock is used automatically. Without `std`,
    /// requires `.elapsed_clock()` or `.elapsed_clock_fn()` to be configured;
    /// otherwise timeout has no effect.
    #[must_use]
    pub fn timeout(self, dur: Duration) -> Self {
        SyncRetryBuilder {
            inner: self.inner.set_timeout(dur),
        }
    }

    /// Sets the blocking sleep function used between retry attempts.
    #[must_use]
    pub fn sleep<NewSleep>(
        self,
        sleeper: NewSleep,
    ) -> SyncRetryBuilder<S, W, P, BA, AA, OX, F, NewSleep, T, E> {
        SyncRetryBuilder {
            inner: self.inner.with_sleeper(sleeper),
        }
    }
}

impl_alloc_hook_chain! {
    impl[S, W, P, BA, AA, OX, F, SleepFn, T, E]
    SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E> =>
    before_attempt -> { SyncBuilderWithBeforeHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> },
    after_attempt -> { SyncBuilderWithAfterHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> },
    on_exit -> { SyncBuilderWithOnExitHook<S, W, P, BA, AA, OX, F, SleepFn, T, E, Hook> },
}

#[cfg(not(feature = "alloc"))]
impl<S, W, P, AA, OX, F, SleepFn, T, E> SyncRetryBuilder<S, W, P, (), AA, OX, F, SleepFn, T, E> {
    /// Sets the before-attempt hook.
    ///
    /// Without `alloc`, only one hook per slot is supported; calling this
    /// twice is a compile error.
    ///
    /// ```compile_fail
    /// use relentless::{RetryExt, stop};
    ///
    /// let _ = (|| Err::<(), _>("fail"))
    ///     .retry()
    ///     .stop(stop::attempts(1))
    ///     .before_attempt(|_state| {})
    ///     .before_attempt(|_state| {});
    /// ```
    #[must_use]
    pub fn before_attempt<Hook>(
        self,
        hook: Hook,
    ) -> SyncRetryBuilder<S, W, P, Hook, AA, OX, F, SleepFn, T, E>
    where
        Hook: FnMut(&RetryState),
    {
        self.map_hooks(|hooks| hooks.set_before_attempt(hook))
    }
}

#[cfg(not(feature = "alloc"))]
impl<S, W, P, BA, OX, F, SleepFn, T, E> SyncRetryBuilder<S, W, P, BA, (), OX, F, SleepFn, T, E> {
    /// Sets the after-attempt hook.
    ///
    /// Without `alloc`, only one hook per slot is supported; calling this
    /// twice is a compile error.
    ///
    /// ```compile_fail
    /// use relentless::{RetryExt, stop};
    ///
    /// let _ = (|| Err::<(), _>("fail"))
    ///     .retry()
    ///     .stop(stop::attempts(1))
    ///     .after_attempt(|_state| {})
    ///     .after_attempt(|_state| {});
    /// ```
    #[must_use]
    pub fn after_attempt<Hook>(
        self,
        hook: Hook,
    ) -> SyncRetryBuilder<S, W, P, BA, Hook, OX, F, SleepFn, T, E>
    where
        Hook: for<'a> FnMut(&AttemptState<'a, T, E>),
    {
        self.map_hooks(|hooks| hooks.set_after_attempt(hook))
    }
}

#[cfg(not(feature = "alloc"))]
impl<S, W, P, BA, AA, F, SleepFn, T, E> SyncRetryBuilder<S, W, P, BA, AA, (), F, SleepFn, T, E> {
    /// Sets the on-exit hook.
    ///
    /// Without `alloc`, only one hook per slot is supported; calling this
    /// twice is a compile error.
    ///
    /// ```compile_fail
    /// use relentless::{RetryExt, stop};
    ///
    /// let _ = (|| Err::<(), _>("fail"))
    ///     .retry()
    ///     .stop(stop::attempts(1))
    ///     .on_exit(|_state| {})
    ///     .on_exit(|_state| {});
    /// ```
    #[must_use]
    pub fn on_exit<Hook>(
        self,
        hook: Hook,
    ) -> SyncRetryBuilder<S, W, P, BA, AA, Hook, F, SleepFn, T, E>
    where
        Hook: for<'a> FnMut(&ExitState<'a, T, E>),
    {
        self.map_hooks(|hooks| hooks.set_on_exit(hook))
    }
}

use super::super::execution::common::RetryOp;

#[allow(private_bounds)]
impl<S, W, P, BA, AA, OX, F, SleepFn, T, E> SyncRetryBuilder<S, W, P, BA, AA, OX, F, SleepFn, T, E>
where
    S: Stop,
    W: Wait,
    P: Predicate<T, E>,
    BA: BeforeAttemptHook,
    AA: AttemptHook<T, E>,
    OX: ExitHook<T, E>,
    F: RetryOp<T, E>,
    SleepFn: SyncSleep,
{
    /// Executes the retry loop and returns the final result.
    ///
    /// # Errors
    ///
    /// Returns [`RetryError`] if all attempts are exhausted or a non-retryable
    /// error is encountered.
    pub fn call(self) -> Result<T, RetryError<T, E>> {
        self.execute::<false>().0
    }

    /// Wraps this builder to also return [`RetryStats`] on completion.
    ///
    /// Does not execute the retry loop; call `.call()` on the returned wrapper.
    #[must_use]
    pub fn with_stats(self) -> SyncRetryBuilderWithStats<S, W, P, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryBuilderWithStats { inner: self }
    }

    fn execute<const COLLECT_STATS: bool>(
        self,
    ) -> (Result<T, RetryError<T, E>>, Option<RetryStats>) {
        self.inner.execute::<S, W, P, COLLECT_STATS>()
    }
}

#[allow(private_bounds)]
impl<S, W, P, BA, AA, OX, F, SleepFn, T, E>
    SyncRetryBuilderWithStats<S, W, P, BA, AA, OX, F, SleepFn, T, E>
where
    S: Stop,
    W: Wait,
    P: Predicate<T, E>,
    BA: BeforeAttemptHook,
    AA: AttemptHook<T, E>,
    OX: ExitHook<T, E>,
    F: RetryOp<T, E>,
    SleepFn: SyncSleep,
{
    /// Executes the retry loop and returns both the result and collected stats.
    ///
    /// # Panics
    ///
    /// Panics if stats collection fails internally (should not happen in
    /// practice).
    pub fn call(self) -> (Result<T, RetryError<T, E>>, RetryStats) {
        let (result, stats) = self.inner.execute::<true>();
        (
            result,
            stats.expect("sync retry builder completed without stats"),
        )
    }
}