bparse

Trait Pattern

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

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

Expresses that the implementing type may be used to match a slice with elements of type T.

Required Methods§

Source

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

Evaluates the pattern against a slice of Ts. 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<T>,

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

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<u8> for &str

Source§

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

Source§

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

Source§

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

Implementors§

Source§

impl Pattern<u8> for ByteLookupTable

Source§

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

Source§

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

Source§

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

Source§

impl<T> Pattern<T> for ElementRange<T>
where T: PartialOrd + Copy,

Source§

impl<T> Pattern<T> for One

Source§

impl<T> Pattern<T> for Prefix<'_, T>
where T: PartialEq + Copy,

Source§

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