Skip to main content

PrefilterBuilder

Struct PrefilterBuilder 

Source
pub struct PrefilterBuilder { /* private fields */ }
Expand description

A builder for prefilters.

Use when either:

  • Simply extracting the possible prefilters from individual matchers, without building a router prefilter for many possible matchers
  • When more control, additional logging, etc is required than simply calling RouterPrefilter::insert

§Examples

use router_prefilter::PrefilterBuilder;
use router_prefilter::matchers::{Matcher, MatcherVisitor};

struct Route(&'static str);

impl Matcher for Route {
    fn visit(&self, visitor: &mut MatcherVisitor) {
        visitor.visit_match_starts_with(self.0);
    }
}

let mut builder = PrefilterBuilder::new();
let prefilter = builder.compute_prefilter(Route("/api"));
assert!(prefilter.is_some());

let no_prefilter = builder.compute_prefilter(Route(""));
assert!(no_prefilter.is_none());

Implementations§

Source§

impl PrefilterBuilder

Source

pub fn new() -> Self

Create a new prefilter builder

Source

pub fn compute_prefilter<M: Matcher>( &mut self, matcher: M, ) -> Option<MatchPrefilter>

Compute a prefilter (if possible) for the given matcher.

Returns None if no literal prefixes can be extracted from the matcher.

§Panics

Panics if the matcher’s Matcher::visit implementation leaves unbalanced nesting (more calls to MatcherVisitor::visit_nested_start than MatcherVisitor::visit_nested_finish, or vice versa).

use router_prefilter::PrefilterBuilder;
use router_prefilter::matchers::{Matcher, MatcherVisitor};

struct UnbalancedMatcher;

impl Matcher for UnbalancedMatcher {
    fn visit(&self, visitor: &mut MatcherVisitor) {
        visitor.visit_nested_start();
        visitor.visit_match_starts_with("/api");
        // missing visit_nested_finish
    }
}

let mut builder = PrefilterBuilder::new();
let _ = builder.compute_prefilter(UnbalancedMatcher); // panics
§Examples
use router_prefilter::PrefilterBuilder;
use router_prefilter::matchers::{Matcher, MatcherVisitor};

struct Route(&'static str);

impl Matcher for Route {
    fn visit(&self, visitor: &mut MatcherVisitor) {
        visitor.visit_match_starts_with(self.0);
    }
}

let mut builder = PrefilterBuilder::new();

let prefilter = builder.compute_prefilter(Route("/api")).unwrap();
let prefixes: Vec<_> = prefilter.prefixes().collect();
assert_eq!(prefixes, vec![b"/api".as_slice()]);

Trait Implementations§

Source§

impl Debug for PrefilterBuilder

Source§

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

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

impl Default for PrefilterBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, 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.