Struct Attempt

Source
pub struct Attempt<F> { /* private fields */ }
Expand description

This type provides an API for retrying failable functions.

See the documentation for this type’s methods for detailed examples and the module documentation for an overview example.

Implementations§

Source§

impl<F> Attempt<F>

Source

pub fn to(func: F) -> Attempt<F>

Constructs a new Attempt which, when executed with either Attempt::run or [Attempt::run_async], will run the provided function func until it returns Ok or one of the limits are exceeded.

The Attempt is constructed with the default configuration:

  • No time delay between attempts (thread will not sleep)
  • A default delay growth magnitude of 1.25 (25% increase each attempt)
  • A cap on maximum tries of 10 These defaults are in place to hopefully prevent any accidental infinite loops.
Source

pub fn infinitely<T, E>(func: F) -> T
where F: Fn() -> Result<T, E>,

Shortcut function to construct and run an Attempt that will retry a function infinitely until an Ok value is produced.

Other than removing the limit on maximum attempts, this function uses the default configuration outlined in the documentation for Attempt::to. Using this function is honestly a terrible idea, especially for production code, but it may be useful for prototyping, idk.

Source

pub fn no_max_tries(self) -> Self

Removes the limit on the maximum number of calls to the function that will be made before propagating an Err.

Please keep in mind that this setting can result in infinite loops and/or getting banned from a third-party API.

Source

pub fn max_tries(self, max_tries: usize) -> Self

Sets the maximum bound for the maximum number of calls to the function that will be made before propagating an Err.

Must be greater than 0 (checked by assertion).

Source

pub fn no_delay(self) -> Self

Removes the delay between each call to the function.

Source

pub fn delay(self, delay: Duration) -> Self

Sets the duration of the delay between each call to the function.

For synchronous functions, the delay is implemented using std::thread::sleep. For async functions, the delay uses [tokio::time::sleep].

Source

pub fn delay_growth_magnitude(self, magnitude: f32) -> Self

Sets the magnitude which the delay will be multiplied by after each epoch following the second try. For example, say our magnitude is 2.0, and our delay is 1 second. If the first call to the function fails, Attempt will wait 1 second before executing the function again. If that call also fails, Attempt will wait 2 seconds before executing the function a third time, and so on.

Source

pub fn run<T, E>(self) -> Result<T, E>
where F: Fn() -> Result<T, E>,

Auto Trait Implementations§

§

impl<F> Freeze for Attempt<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Attempt<F>
where F: RefUnwindSafe,

§

impl<F> Send for Attempt<F>
where F: Send,

§

impl<F> Sync for Attempt<F>
where F: Sync,

§

impl<F> Unpin for Attempt<F>
where F: Unpin,

§

impl<F> UnwindSafe for Attempt<F>
where F: UnwindSafe,

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.