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
impl PrefilterBuilder
Sourcepub fn compute_prefilter<M: Matcher>(
&mut self,
matcher: M,
) -> Option<MatchPrefilter>
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
impl Debug for PrefilterBuilder
Auto Trait Implementations§
impl Freeze for PrefilterBuilder
impl RefUnwindSafe for PrefilterBuilder
impl Send for PrefilterBuilder
impl Sync for PrefilterBuilder
impl Unpin for PrefilterBuilder
impl UnsafeUnpin for PrefilterBuilder
impl UnwindSafe for PrefilterBuilder
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