Trait PolicyExt

trait PolicyExt

An extension trait for Policy that provides additional adapters.

Required Methods

fn and<P, B, E>(self: Self, other: P) -> And<Self, P>
where
    Self: Policy<B, E> + Sized,
    P: Policy<B, E>

Create a new Policy that returns Action::Follow only if self and other return Action::Follow.

[clone_body][Policy::clone_body] method of the returned Policy tries to clone the body with both policies.

Example

use bytes::Bytes;
use http_body_util::Full;
use tower_http::follow_redirect::policy::{self, clone_body_fn, Limited, PolicyExt};

enum MyBody {
    Bytes(Bytes),
    Full(Full<Bytes>),
}

let policy = Limited::default().and::<_, _, ()>(clone_body_fn(|body| {
    if let MyBody::Bytes(buf) = body {
        Some(MyBody::Bytes(buf.clone()))
    } else {
        None
    }
}));
fn or<P, B, E>(self: Self, other: P) -> Or<Self, P>
where
    Self: Policy<B, E> + Sized,
    P: Policy<B, E>

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow.

[clone_body][Policy::clone_body] method of the returned Policy tries to clone the body with both policies.

Example

use tower_http::follow_redirect::policy::{self, Action, Limited, PolicyExt};

#[derive(Clone)]
enum MyError {
    TooManyRedirects,
    // ...
}

let policy = Limited::default().or::<_, (), _>(Err(MyError::TooManyRedirects));

Implementors