pub struct Config<D> {
pub attempts: usize,
pub delay: D,
pub on_retry: Option<Box<dyn FnMut(usize, Duration) + 'static>>,
}Expand description
Configuration for the retry function.
§Example
use std::time::Duration;
use retry::Config;
use retry::delay::Fixed;
let config: Config<_> = Config::new(Fixed::new(Duration::from_secs(1)))
.with_attempts(5)
.with_on_retry(|attempt, delay| {
eprintln!("Attempt {attempt} failed, retrying in {delay:?}");
});Fields§
§attempts: usizeNumber of attempts. 0 means retry forever (until success).
Includes the initial call. E.g. attempts: 5 = 1 initial + 4 retries.
delay: DThe delay strategy used between retries.
on_retry: Option<Box<dyn FnMut(usize, Duration) + 'static>>Optional callback invoked before each retry. Receives the attempt number (1-based) and the delay duration.
Implementations§
Source§impl<D> Config<D>
impl<D> Config<D>
Sourcepub const fn new(delay: D) -> Self
pub const fn new(delay: D) -> Self
Creates a new config with infinite retries and the given delay strategy.
Sourcepub const fn with_attempts(self, attempts: usize) -> Self
pub const fn with_attempts(self, attempts: usize) -> Self
Sets the maximum number of attempts.
0 means infinite retries. An attempt count of 5 means
1 initial call + up to 4 retries.
Sourcepub fn with_on_retry(self, f: impl FnMut(usize, Duration) + 'static) -> Self
pub fn with_on_retry(self, f: impl FnMut(usize, Duration) + 'static) -> Self
Sets a callback that is invoked before each retry.
The callback receives the attempt number (1-based) and the delay that will be waited before retrying.
Auto Trait Implementations§
impl<D> !RefUnwindSafe for Config<D>
impl<D> !Send for Config<D>
impl<D> !Sync for Config<D>
impl<D> !UnwindSafe for Config<D>
impl<D> Freeze for Config<D>where
D: Freeze,
impl<D> Unpin for Config<D>where
D: Unpin,
impl<D> UnsafeUnpin for Config<D>where
D: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more