Trait Pattern

Source
pub trait Pattern: Clone + Copy {
    // Required method
    fn eval(&self, input: &[u8]) -> Option<usize>;

    // Provided methods
    fn then<P>(self, other: P) -> Sequence<Self, P>
       where Self: Sized,
             P: Pattern { ... }
    fn or<P>(self, other: P) -> Choice<Self, P>
       where Self: Sized,
             P: Pattern { ... }
}
Expand description

Expresses that the implementing type may be used to match a byte slice.

Required Methods§

Source

fn eval(&self, input: &[u8]) -> Option<usize>

Evaluates the pattern against a byte slice. If the pattern matches, the length of matching slice should be returned. Otherwise, None should be returned.

It is assumed that the pattern begins matching from the start of the slice.

Provided Methods§

Source

fn then<P>(self, other: P) -> Sequence<Self, P>
where Self: Sized, P: Pattern,

Returns a new pattern that will match if self and other match in sequence.

use bparse::Pattern;

let pattern = "a".then("b");
assert_eq!(pattern.eval(b"abc"), Some(2));
Source

fn or<P>(self, other: P) -> Choice<Self, P>
where Self: Sized, P: Pattern,

Returns a new pattern that will match if either self or other match.

use bparse::Pattern;

let pattern = "a".or("b");
assert_eq!(pattern.eval(b"ba"), Some(1));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Pattern for &str

Source§

fn eval(&self, input: &[u8]) -> Option<usize>

Source§

impl Pattern for &[u8]

Source§

fn eval(&self, input: &[u8]) -> Option<usize>

Source§

impl Pattern for u8

Source§

fn eval(&self, input: &[u8]) -> Option<usize>

Source§

impl<P> Pattern for &P
where P: Pattern,

Source§

fn eval(&self, input: &[u8]) -> Option<usize>

Implementors§

Source§

impl Pattern for ByteLookupTable

Source§

impl Pattern for Codepoint

Source§

impl Pattern for ElementRange

Source§

impl Pattern for Eof

Source§

impl Pattern for One

Source§

impl Pattern for Prefix<'_>

Source§

impl<P1, P2> Pattern for Choice<P1, P2>
where P1: Pattern, P2: Pattern,

Source§

impl<P1, P2> Pattern for Sequence<P1, P2>
where P1: Pattern, P2: Pattern,

Source§

impl<P> Pattern for Not<P>
where P: Pattern,

Source§

impl<P> Pattern for Repetition<P>
where P: Pattern,