Skip to main content

Fallback

Struct Fallback 

Source
pub struct Fallback<F, T> { /* private fields */ }
Expand description

A policy that returns a fallback value when the operation fails.

This is useful for graceful degradation - instead of propagating an error, you can return a sensible default value.

§Examples

use do_over::{policy::Policy, fallback::Fallback, error::DoOverError};

// Simple fallback with a static value
let policy = Fallback::new(|| "cached_data".to_string());

// Fallback that computes a value
let policy = Fallback::new(|| {
    vec!["item1".to_string(), "item2".to_string()]
});

Implementations§

Source§

impl<F, T> Fallback<F, T>
where F: Fn() -> T + Send + Sync, T: Send,

Source

pub fn new(fallback_fn: F) -> Self

Create a new fallback policy.

§Arguments
  • fallback_fn - A function that produces the fallback value
§Examples
use do_over::fallback::Fallback;

// Return empty vec on failure
let policy = Fallback::new(|| Vec::<String>::new());

// Return a default struct
let policy = Fallback::new(|| "default".to_string());

Trait Implementations§

Source§

impl<F, T> Clone for Fallback<F, T>
where F: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F, T, E> Policy<E> for Fallback<F, T>
where F: Fn() -> T + Send + Sync, T: Send + Sync + Clone, E: Send + Sync,

Source§

fn execute<'life0, 'async_trait, Func, Fut, R>( &'life0 self, f: Func, ) -> Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'async_trait>>
where Func: Fn() -> Fut + Send + Sync + 'async_trait, Fut: Future<Output = Result<R, E>> + Send + 'async_trait, R: Send + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Execute an async operation with this policy’s resilience behavior. Read more

Auto Trait Implementations§

§

impl<F, T> Freeze for Fallback<F, T>
where F: Freeze,

§

impl<F, T> RefUnwindSafe for Fallback<F, T>

§

impl<F, T> Send for Fallback<F, T>
where F: Send, T: Send,

§

impl<F, T> Sync for Fallback<F, T>
where F: Sync, T: Sync,

§

impl<F, T> Unpin for Fallback<F, T>
where F: Unpin, T: Unpin,

§

impl<F, T> UnwindSafe for Fallback<F, T>
where F: UnwindSafe, T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.