Struct FilterLayer

Source
pub struct FilterLayer<F, S, T, R, E>
where F: Filter<T>, S: Service<T, Response = R, Error = E>,
{ /* private fields */ }
Expand description

A Tower layer that executes the provided service only if the given filter returns true. Otherwise it falls through to the inner server.

§Example

use tower_fallthrough_filter::{Filter, FilterLayer};
use tower::{Service, Layer};

#[derive(Debug, Clone)]
struct MyFilter;

impl Filter<bool> for MyFilter {
    fn matches(&self, data: &bool) -> bool {
        *data
    }
}

#[derive(Debug, Clone)]
struct StringService(String);

impl Service<bool> for StringService {
    type Response = String;
    type Error = std::convert::Infallible;
    type Future = std::future::Ready::<Result<Self::Response, Self::Error>>;

    fn poll_ready(
        &mut self,
        _: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        std::task::Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: bool) -> Self::Future {
        std::future::ready(Ok(self.0.clone()))
    }
}

#[tokio::main]
async fn main() {
    let service_a = StringService("A".to_string());
    let service_b = StringService("B".to_string());
    let filter = MyFilter;

    let mut middleware = FilterLayer::new(filter, service_a).layer(service_b);

    assert_eq!(middleware.call(true).await, Ok("A".to_string()));
    assert_eq!(middleware.call(false).await, Ok("B".to_string()));
}

Implementations§

Source§

impl<F: Filter<T>, S: Service<T>, T> FilterLayer<F, S, T, S::Response, S::Error>

Source

pub fn new(filter: F, service: S) -> Self

Creates a new FilterLayer given a Service and a Filter.

NOTE: The Service and the Filter have to operate on the same type T.

Trait Implementations§

Source§

impl<F, S, R, E, T> Clone for FilterLayer<F, S, T, R, E>
where F: Filter<T> + Clone, S: Service<T, Response = R, Error = E> + 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, S, T: Debug, R: Debug, E: Debug> Debug for FilterLayer<F, S, T, R, E>
where F: Filter<T> + Debug, S: Service<T, Response = R, Error = E> + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F, S, I, T, R, E> Layer<I> for FilterLayer<F, S, T, R, E>
where F: Filter<T> + Clone, S: Service<T, Response = R, Error = E> + Clone, I: Service<T, Response = R, Error = E> + Clone,

Source§

type Service = FilterService<F, S, I, T, R, E>

The wrapped service
Source§

fn layer(&self, inner_service: I) -> Self::Service

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

Auto Trait Implementations§

§

impl<F, S, T, R, E> Freeze for FilterLayer<F, S, T, R, E>
where F: Freeze, S: Freeze,

§

impl<F, S, T, R, E> RefUnwindSafe for FilterLayer<F, S, T, R, E>

§

impl<F, S, T, R, E> Send for FilterLayer<F, S, T, R, E>
where F: Send, S: Send, T: Send, R: Send, E: Send,

§

impl<F, S, T, R, E> Sync for FilterLayer<F, S, T, R, E>
where F: Sync, S: Sync, T: Sync, R: Sync, E: Sync,

§

impl<F, S, T, R, E> Unpin for FilterLayer<F, S, T, R, E>
where F: Unpin, S: Unpin, T: Unpin, R: Unpin, E: Unpin,

§

impl<F, S, T, R, E> UnwindSafe for FilterLayer<F, S, T, R, 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.