Skip to main content

Config

Struct Config 

Source
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: usize

Number of attempts. 0 means retry forever (until success). Includes the initial call. E.g. attempts: 5 = 1 initial + 4 retries.

§delay: D

The 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>

Source

pub const fn new(delay: D) -> Self

Creates a new config with infinite retries and the given delay strategy.

Source

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.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.