Skip to main content

FallbackLayer

Struct FallbackLayer 

Source
pub struct FallbackLayer<Req, Res, E> { /* private fields */ }
Expand description

A Tower layer that applies fallback behavior to a service.

See the module-level documentation for usage examples.

Implementations§

Source§

impl<Req, Res, E> FallbackLayer<Req, Res, E>

Source

pub fn builder() -> FallbackConfigBuilder<Req, Res, E>

Creates a new builder for configuring a fallback layer.

Source

pub fn value_fn<F>(f: F) -> Self
where F: Fn() -> Res + Send + Sync + 'static,

Creates a fallback layer that generates a value using a function.

Unlike value, this doesn’t require Res: Clone since the function generates a fresh value for each fallback.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, MyResponse, MyError>::value_fn(|| {
    MyResponse { data: vec![0; 1024] }
});
Source§

impl<Req, Res, E> FallbackLayer<Req, Res, E>
where Res: Clone,

Source

pub fn value(value: Res) -> Self

Creates a fallback layer that returns a static value on failure.

Note: This requires Res: Clone. If your response type doesn’t implement Clone, use value_fn instead.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, String, MyError>::value("default".to_string());
Source

pub fn from_error<F>(f: F) -> Self
where F: Fn(&E) -> Res + Send + Sync + 'static,

Creates a fallback layer that computes a response from the error.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, String, MyError>::from_error(|e| {
    format!("Error: {}", e.msg)
});
Source

pub fn from_request_error<F>(f: F) -> Self
where F: Fn(&Req, &E) -> Res + Send + Sync + 'static,

Creates a fallback layer that computes a response from request and error.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, String, MyError>::from_request_error(|req, _err| {
    format!("Fallback for request: {}", req)
});
Source

pub fn service<S, Fut>(service: S) -> Self
where S: Fn(Req) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Res, E>> + Send + 'static,

Creates a fallback layer that routes to a backup service.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, String, MyError>::service(|req: String| async move {
    Ok::<_, MyError>(format!("backup: {}", req))
});
Source

pub fn exception<F>(f: F) -> Self
where F: Fn(E) -> E + Send + Sync + 'static,

Creates a fallback layer that transforms errors.

Note: This still returns an error, just a transformed one.

§Example
use tower_resilience_fallback::FallbackLayer;

let layer = FallbackLayer::<String, String, MyError>::exception(|e| {
    MyError { code: 500 }
});

Trait Implementations§

Source§

impl<Req, Res, E> Clone for FallbackLayer<Req, Res, E>
where Res: 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<S, Req, Res, E> Layer<S> for FallbackLayer<Req, Res, E>
where Res: Clone,

Source§

type Service = Fallback<S, Req, Res, E>

The wrapped service
Source§

fn layer(&self, service: S) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

Auto Trait Implementations§

§

impl<Req, Res, E> Freeze for FallbackLayer<Req, Res, E>

§

impl<Req, Res, E> !RefUnwindSafe for FallbackLayer<Req, Res, E>

§

impl<Req, Res, E> Send for FallbackLayer<Req, Res, E>
where Res: Sync + Send,

§

impl<Req, Res, E> Sync for FallbackLayer<Req, Res, E>
where Res: Sync + Send,

§

impl<Req, Res, E> Unpin for FallbackLayer<Req, Res, E>

§

impl<Req, Res, E> UnsafeUnpin for FallbackLayer<Req, Res, E>

§

impl<Req, Res, E> !UnwindSafe for FallbackLayer<Req, Res, E>

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.