relentless 0.11.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
use core::fmt;
use core::marker::PhantomData;

use super::common::{RetryOp, execute_sync_loop};
use crate::compat::Duration;
use crate::error::RetryError;
#[cfg(feature = "alloc")]
use crate::policy::HookChain;
use crate::policy::time::ElapsedTracker;
use crate::policy::{
    AttemptHook, BeforeAttemptHook, ExecutionHooks, ExitHook, PolicyHandle, RetryPolicy,
};
use crate::predicate::Predicate;
use crate::state::{AttemptState, ExitState, RetryState};
use crate::stats::RetryStats;
use crate::stop::Stop;
use crate::wait::Wait;

/// Marker for the absence of an explicit sync sleep function.
#[doc(hidden)]
pub struct NoSyncSleep;

/// Blocking sleep abstraction for the sync retry execution engine.
///
/// A blanket implementation covers `FnMut(Duration)` closures, enabling
/// `.sleep(|dur| ...)` in the builder API. When the `std` feature is active,
/// [`NoSyncSleep`] defaults to [`std::thread::sleep`].
#[doc(hidden)]
pub trait SyncSleep {
    fn sleep(&mut self, dur: Duration);
}

impl<F> SyncSleep for F
where
    F: FnMut(Duration),
{
    fn sleep(&mut self, dur: Duration) {
        (self)(dur);
    }
}

#[cfg(feature = "std")]
impl SyncSleep for NoSyncSleep {
    fn sleep(&mut self, dur: Duration) {
        std::thread::sleep(dur);
    }
}

/// Sync retry execution object, generic over how the policy is stored.
///
/// A single engine drives both entry points:
/// - [`SyncRetry`] borrows a policy (`Policy = &RetryPolicy<S, W, P>`), created
///   by [`RetryPolicy::retry`].
/// - [`SyncRetryBuilder`](crate::SyncRetryBuilder) owns a policy
///   (`Policy = RetryPolicy<S, W, P>`), created by [`crate::RetryExt::retry`].
///
/// Methods that only make sense when the policy is owned (`stop`, `wait`,
/// `when`, `until`) are implemented separately on the owned alias.
#[allow(clippy::type_complexity)]
pub struct SyncRetryExec<Policy, BA, AA, OX, F, SleepFn, T, E> {
    policy: Policy,
    hooks: ExecutionHooks<BA, AA, OX>,
    op: F,
    sleeper: SleepFn,
    elapsed_tracker: ElapsedTracker,
    timeout: Option<Duration>,
    _marker: PhantomData<fn() -> (T, E)>,
}

/// Sync retry execution object created by [`RetryPolicy::retry`].
///
/// Configure hooks and `.sleep(...)`, then call `.call()`.
///
/// In `std` builds, calling `.sleep(...)` is optional because a default
/// blocking sleeper is available. In non-`std` builds, `.sleep(...)` is
/// required before `.call()` is available.
///
/// # Examples
///
/// ```
/// use relentless::{RetryPolicy, stop};
///
/// let policy = RetryPolicy::new().stop(stop::attempts(2));
/// let retry = policy
///     .retry(|_| Err::<(), _>("fail"))
///     .before_attempt(|_state| {})
///     .sleep(|_dur| {});
/// let _ = retry.call();
/// ```
pub type SyncRetry<'policy, S, W, P, BA, AA, OX, F, SleepFn, T, E> =
    SyncRetryExec<&'policy RetryPolicy<S, W, P>, BA, AA, OX, F, SleepFn, T, E>;

/// Sync retry execution wrapper that returns statistics.
///
/// Created by calling `.with_stats()` on a [`SyncRetryExec`].
#[allow(clippy::type_complexity)]
pub struct SyncRetryExecWithStats<Policy, BA, AA, OX, F, SleepFn, T, E> {
    inner: SyncRetryExec<Policy, BA, AA, OX, F, SleepFn, T, E>,
}

/// Sync retry execution-with-stats object created by `.with_stats()` on
/// [`SyncRetry`].
///
/// # Examples
///
/// ```
/// use relentless::{RetryPolicy, stop};
///
/// let policy = RetryPolicy::new().stop(stop::attempts(1));
/// let (_result, _stats) = policy
///     .retry(|_| Ok::<u32, &str>(1))
///     .sleep(|_dur| {})
///     .with_stats()
///     .call();
/// ```
pub type SyncRetryWithStats<'policy, S, W, P, BA, AA, OX, F, SleepFn, T, E> =
    SyncRetryExecWithStats<&'policy RetryPolicy<S, W, P>, BA, AA, OX, F, SleepFn, T, E>;

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

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

#[cfg(not(feature = "std"))]
#[doc(hidden)]
/// ```compile_fail
/// use relentless::{RetryPolicy, stop};
///
/// let policy = RetryPolicy::new().stop(stop::attempts(1));
/// let _ = policy.retry(|_| Err::<(), _>("fail")).call();
/// ```
#[allow(dead_code)]
fn _sync_call_requires_sleep_in_no_std() {}

impl<Policy, BA, AA, OX, F, SleepFn, T, E> SyncRetryExec<Policy, BA, AA, OX, F, SleepFn, T, E> {
    pub(crate) fn new(
        policy: Policy,
        hooks: ExecutionHooks<BA, AA, OX>,
        op: F,
        sleeper: SleepFn,
        elapsed_tracker: ElapsedTracker,
    ) -> Self {
        Self {
            policy,
            hooks,
            op,
            sleeper,
            elapsed_tracker,
            timeout: None,
            _marker: PhantomData,
        }
    }

    fn map_hooks<NewBA, NewAA, NewOX>(
        self,
        map: impl FnOnce(ExecutionHooks<BA, AA, OX>) -> ExecutionHooks<NewBA, NewAA, NewOX>,
    ) -> SyncRetryExec<Policy, NewBA, NewAA, NewOX, F, SleepFn, T, E> {
        SyncRetryExec {
            policy: self.policy,
            hooks: map(self.hooks),
            op: self.op,
            sleeper: self.sleeper,
            elapsed_tracker: self.elapsed_tracker,
            timeout: self.timeout,
            _marker: PhantomData,
        }
    }

    pub(crate) fn map_policy<NewPolicy>(
        self,
        map: impl FnOnce(Policy) -> NewPolicy,
    ) -> SyncRetryExec<NewPolicy, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryExec {
            policy: map(self.policy),
            hooks: self.hooks,
            op: self.op,
            sleeper: self.sleeper,
            elapsed_tracker: self.elapsed_tracker,
            timeout: self.timeout,
            _marker: PhantomData,
        }
    }

    /// Configures a custom elapsed clock for elapsed-based stop conditions.
    #[must_use]
    pub fn elapsed_clock(mut self, clock: fn() -> Duration) -> Self {
        self.elapsed_tracker = ElapsedTracker::new(Some(clock));
        self
    }

    /// 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(mut self, clock: impl Fn() -> Duration + 'static) -> Self {
        self.elapsed_tracker = ElapsedTracker::new_boxed(crate::compat::Box::new(clock));
        self
    }

    /// Sets a wall-clock budget for the entire retry execution.
    ///
    /// This is a **boundary check, not a preemptive timeout.** It is evaluated
    /// between attempts: the next inter-attempt sleep is clamped so the loop
    /// terminates close to the deadline, and the loop stops once the elapsed
    /// time exceeds `dur`. It **cannot** interrupt an operation or a sleep that
    /// is already in progress.
    ///
    /// For a hard wall-clock cancellation that can preempt in-flight work, wrap
    /// the retry future in your async runtime's timeout (e.g.
    /// `tokio::time::timeout`); see the `async-cancel` example.
    #[must_use]
    pub fn timeout(mut self, dur: Duration) -> Self {
        self.timeout = Some(dur);
        self
    }

    fn execute<const COLLECT_STATS: bool>(
        mut self,
    ) -> (Result<T, RetryError<T, E>>, Option<RetryStats>)
    where
        Policy: PolicyHandle,
        Policy::Stop: Stop,
        Policy::Wait: Wait,
        Policy::Predicate: Predicate<T, E>,
        BA: BeforeAttemptHook,
        AA: AttemptHook<T, E>,
        OX: ExitHook<T, E>,
        F: RetryOp<T, E>,
        SleepFn: SyncSleep,
    {
        let policy = self.policy.policy_ref();
        execute_sync_loop::<
            Policy::Stop,
            Policy::Wait,
            Policy::Predicate,
            BA,
            AA,
            OX,
            F,
            SleepFn,
            T,
            E,
            COLLECT_STATS,
        >(
            policy,
            &mut self.hooks,
            &mut self.op,
            &mut self.sleeper,
            &self.elapsed_tracker,
            self.timeout,
        )
    }

    /// Wraps this execution object 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) -> SyncRetryExecWithStats<Policy, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryExecWithStats { inner: self }
    }

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

impl<Policy, BA, AA, OX, F, T, E> SyncRetryExec<Policy, BA, AA, OX, F, NoSyncSleep, T, E> {
    /// Sets the blocking sleep function used between retry attempts.
    #[must_use]
    pub fn sleep<SleepFn>(
        self,
        sleeper: SleepFn,
    ) -> SyncRetryExec<Policy, BA, AA, OX, F, SleepFn, T, E> {
        SyncRetryExec {
            policy: self.policy,
            hooks: self.hooks,
            op: self.op,
            sleeper,
            elapsed_tracker: self.elapsed_tracker,
            timeout: self.timeout,
            _marker: PhantomData,
        }
    }
}

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

impl_alloc_hook_chain! {
    impl[Policy, BA, AA, OX, F, SleepFn, T, E]
    SyncRetryExec<Policy, BA, AA, OX, F, SleepFn, T, E> =>
    before_attempt -> { SyncRetryExec<Policy, HookChain<BA, Hook>, AA, OX, F, SleepFn, T, E> },
    after_attempt -> { SyncRetryExec<Policy, BA, HookChain<AA, Hook>, OX, F, SleepFn, T, E> },
    on_exit -> { SyncRetryExec<Policy, BA, AA, HookChain<OX, Hook>, F, SleepFn, T, E> },
}

#[cfg(not(feature = "alloc"))]
impl<Policy, AA, OX, F, SleepFn, T, E> SyncRetryExec<Policy, (), 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 (the `BA` type parameter would already be
    /// non-`()`).
    ///
    /// ```compile_fail
    /// use relentless::{RetryPolicy, stop};
    ///
    /// let policy = RetryPolicy::new().stop(stop::attempts(1));
    /// let _ = policy
    ///     .retry(|_| Err::<(), _>("fail"))
    ///     .before_attempt(|_state| {})
    ///     .before_attempt(|_state| {});
    /// ```
    #[must_use]
    pub fn before_attempt<Hook>(
        self,
        hook: Hook,
    ) -> SyncRetryExec<Policy, 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<Policy, BA, OX, F, SleepFn, T, E> SyncRetryExec<Policy, 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::{RetryPolicy, stop};
    ///
    /// let policy = RetryPolicy::new().stop(stop::attempts(1));
    /// let _ = policy
    ///     .retry(|_| Err::<(), _>("fail"))
    ///     .after_attempt(|_state| {})
    ///     .after_attempt(|_state| {});
    /// ```
    #[must_use]
    pub fn after_attempt<Hook>(
        self,
        hook: Hook,
    ) -> SyncRetryExec<Policy, 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<Policy, BA, AA, F, SleepFn, T, E> SyncRetryExec<Policy, 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::{RetryPolicy, stop};
    ///
    /// let policy = RetryPolicy::new().stop(stop::attempts(1));
    /// let _ = policy
    ///     .retry(|_| Err::<(), _>("fail"))
    ///     .on_exit(|_state| {})
    ///     .on_exit(|_state| {});
    /// ```
    #[must_use]
    pub fn on_exit<Hook>(self, hook: Hook) -> SyncRetryExec<Policy, 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))
    }
}

impl<S, W, P> RetryPolicy<S, W, P>
where
    S: Stop,
    W: Wait,
{
    /// Creates a synchronous retry execution for the given operation.
    #[must_use]
    pub fn retry<T, E, F>(&self, op: F) -> SyncRetry<'_, S, W, P, (), (), (), F, NoSyncSleep, T, E>
    where
        F: FnMut(RetryState) -> Result<T, E>,
    {
        SyncRetryExec::new(
            self,
            ExecutionHooks::new(),
            op,
            NoSyncSleep,
            ElapsedTracker::new(None),
        )
    }
}