Skip to main content

Matcher

Struct Matcher 

Source
pub struct Matcher<I, A> { /* private fields */ }
Expand description

Ordered pattern matcher: first matching arm wins.

Implementations§

Source§

impl<I: 'static, A: 'static> Matcher<I, A>

Source

pub fn new() -> Self

Empty matcher with no arms and no default.

Source

pub fn when( self, pred: Predicate<I>, handler: impl Fn(I) -> A + Send + Sync + 'static, ) -> Self

Add a predicate arm evaluated before later arms.

Source

pub fn tag( self, expected: impl Into<String>, handler: impl Fn(I) -> A + Send + Sync + 'static, ) -> Self
where I: HasTag,

Add a tag arm: matches when HasTag::tag equals expected (string equality).

Source

pub fn or_else(self, handler: impl Fn(I) -> A + Send + Sync + 'static) -> Self

Fallback when no arm matches (used by Matcher::exhaustive only).

Source

pub fn run_exhaustive(self, input: I) -> A

Run this matcher once. Unlike Self::exhaustive, does not require I: Send (no shareable closure).

Source

pub fn exhaustive(self) -> impl Fn(I) -> A
where I: 'static, A: 'static,

Fn(I) -> A that panics when nothing matches and no Matcher::or_else was set.

The closure is only Send + Sync when I: Send (it captures Arc<Matcher<…>> and may be invoked with I across threads).

Source

pub fn option(self) -> impl Fn(I) -> Option<A> + Send + Sync
where I: 'static, A: 'static,

Fn(I) -> Option<A>None when unmatched; or_else is ignored.

Source

pub fn either(self) -> impl Fn(I) -> Result<A, I> + Send + Sync
where I: 'static, A: 'static,

Fn(I) -> Result<A, I>Err carries the original input when unmatched; or_else is ignored.

Trait Implementations§

Source§

impl<I: 'static, A: 'static> Default for Matcher<I, A>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<I, A> Freeze for Matcher<I, A>

§

impl<I, A> !RefUnwindSafe for Matcher<I, A>

§

impl<I, A> Send for Matcher<I, A>

§

impl<I, A> Sync for Matcher<I, A>

§

impl<I, A> Unpin for Matcher<I, A>

§

impl<I, A> UnsafeUnpin for Matcher<I, A>

§

impl<I, A> !UnwindSafe for Matcher<I, A>

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> Pipe for T

Source§

fn pipe<F, R>(self, f: F) -> R
where F: FnOnce(Self) -> R,

Applies f to self (F#-style forward pipe).
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.