Skip to main content

Maybe

Struct Maybe 

Source
pub struct Maybe<T: Pattern>(pub T);
Expand description

A pattern that matches 0 or 1 occurrences of the inner pattern T, making it optional.

Because both 0 and 1 occurrences are valid matches, Maybe never fails: Pattern::immediate_match and Pattern::trailing_match always return Ok, falling back to an empty match at the relevant end of the input if T doesn’t match there. Likewise, Pattern::first_match / Pattern::first_match_ex always match at the very start of the input.

More conveniently created via Pattern::maybe

§Example

use {
    shrimple_parser::{
        Pattern,
        parser::one,
        pattern::Maybe,
    },
    core::convert::Infallible,
};

// Matches an optional '-' sign at the start of the input.
assert_eq!(
    one::<_, Infallible>('-'.maybe())("-123"),
    Ok(("123", "-")),
);

// No '-' present - still matches, with an empty matched fragment.
assert_eq!(
    one::<_, Infallible>('-'.maybe())("123"),
    Ok(("123", "")),
);

Tuple Fields§

§0: T

Trait Implementations§

Source§

impl<T: Clone + Pattern> Clone for Maybe<T>

Source§

fn clone(&self) -> Maybe<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy + Pattern> Copy for Maybe<T>

Source§

impl<T: Debug + Pattern> Debug for Maybe<T>

Source§

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

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

impl<T: Pattern> Pattern for Maybe<T>

Source§

fn immediate_matches<I: Input>(&self, input: I) -> (I, I)

Repeating “0 or 1 T” is the same as “0 or more T”, so this delegates directly to T’s implementation rather than looping on Pattern::immediate_match, which would never terminate since Maybe::immediate_match never fails.

Source§

fn trailing_matches_counted<I: Input>(&self, input: I) -> (I, usize)

See Maybe::immediate_matches for why this delegates straight to T instead of looping on Pattern::trailing_match.

Source§

fn immediate_match<I: Input>(&self, input: I) -> Result<(I, I), I>

The return values are (rest of the input, matched fragment at the beginning). Read more
Source§

fn immediate_matches_counted<I: Input>(&self, input: I) -> (I, (I, usize))

Like Pattern::immediate_matches, but also counts the number of matches. Read more
Source§

fn trailing_match<I: Input>(&self, input: I) -> Result<(I, I), I>

Like Pattern::immediate_match, but matches at the end of input. The return values are (the input before the match, the match) Read more
Source§

fn first_match<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

The return values are (the match + rest of the input, (string before the match, the match)). Read more
Source§

fn first_match_ex<I: Input>(&self, input: I) -> Result<(I, (I, I)), I>

Like Pattern::first_match, but the match is excluded from the rest of the input. Read more
Source§

fn by_ref(&self) -> Ref<'_, Self>

Get the pattern by reference to avoid moving it, which will happen in generic code Read more
Source§

fn and<Other: Pattern>(self, other: Other) -> Chain<Self, Other>
where Self: Sized,

Combine self and another pattern into a pattern that matches both of them in a sequence, with self before other Read more
Source§

fn not_escaped_by<Prefix: Pattern>( self, prefix: Prefix, ) -> NotEscaped<Prefix, Self>
where Self: Sized,

Create a pattern that’ll match self only if it’s not escaped (immediately preceded) by the provided pattern. Read more
Source§

fn not_enclosed_by<Enclosure: Pattern>( self, enc: Enclosure, ) -> NotEnclosed<Enclosure, Self>
where Self: Sized,

Create a pattern that’ll match self only if it’s not enclosed (preceded & superceded) by the provided pattern. Read more
Source§

fn many(self) -> Many<Self>
where Self: Sized,

Creates a greedy pattern that matches self as many times as possible. See Many
Source§

fn maybe(self) -> Maybe<Self>
where Self: Sized,

Creates a pattern that matches self 0 or 1 times. See Maybe

Auto Trait Implementations§

§

impl<T> Freeze for Maybe<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Maybe<T>
where T: RefUnwindSafe,

§

impl<T> Send for Maybe<T>
where T: Send,

§

impl<T> Sync for Maybe<T>
where T: Sync,

§

impl<T> Unpin for Maybe<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Maybe<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Maybe<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoPattern for T
where T: Pattern,

Source§

type Pattern = T

The pattern that this value converts to.
Source§

fn into_pattern(self) -> <T as IntoPattern>::Pattern

Converts the value into a pattern.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.