pub struct FilterLayer<F, S, T, R, 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§
Trait Implementations§
Source§impl<F, S, R, E, T> Clone for FilterLayer<F, S, T, R, E>
impl<F, S, R, E, T> Clone for FilterLayer<F, S, T, R, E>
Source§impl<F, S, I, T, R, E> Layer<I> for FilterLayer<F, S, T, R, E>
impl<F, S, I, T, R, E> Layer<I> for FilterLayer<F, S, T, R, E>
Auto Trait Implementations§
impl<F, S, T, R, E> Freeze for FilterLayer<F, S, T, R, E>
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>
impl<F, S, T, R, E> Sync for FilterLayer<F, S, T, R, E>
impl<F, S, T, R, E> Unpin for FilterLayer<F, S, T, R, E>
impl<F, S, T, R, E> UnwindSafe for FilterLayer<F, S, T, R, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more