Skip to main content

Pat

Struct Pat 

Source
pub struct Pat { /* private fields */ }
Expand description

A pattern: the left-hand side of one match arm, or a part of one.

Patterns are built bottom-up with the constructors below and describe values of the type they are checked against, in terms of the Signature the Model gives that type. Which constructor fits which signature:

SignaturePatterns
Sumvariant
Productproduct
Boolbool
Int, BigIntint, range
Charchar, char_range
Opaquelit
Array, Sliceslice, slice_rest
anywild, or

A binding such as x or x @ p is, for matching purposes, _ or p.

§Node numbering

Diagnostics about part of a pattern (see Redundant) name the part by its preorder index in the arm’s pattern: the root is node 0, and each node is followed by its children (fields, alternatives, or slice elements, prefix before suffix) left to right, each child’s whole subtree before the next child.

§Examples

use match_lang::Pat;

// `Some(1 | 2..=5)`, with `Some` as variant 1 of its sum type.
let pat = Pat::variant(1, [Pat::or([Pat::int(1), Pat::range(2, 5)])]);
assert_eq!(pat.to_string(), "#1(1 | 2..=5)");

Implementations§

Source§

impl Pat

Source

pub fn wild() -> Pat

The wildcard _, which matches every value of any type. A binding pattern (x) is a wildcard to the checker.

§Examples
use match_lang::Pat;

assert_eq!(Pat::wild().to_string(), "_");
Source

pub fn variant(index: u32, fields: impl IntoIterator<Item = Pat>) -> Pat

Variant index of a Sum type, with one pattern per field of that variant, in field order.

The number of fields must match what the model’s fields reports for the variant; a mismatch is reported as Reason::Fields.

§Examples
use match_lang::Pat;

let none = Pat::variant(0, []);
let some = Pat::variant(1, [Pat::wild()]);
assert_eq!(none.to_string(), "#0");
assert_eq!(some.to_string(), "#1(_)");
Source

pub fn product(fields: impl IntoIterator<Item = Pat>) -> Pat

The single constructor of a Product type: a tuple, struct, record, box, or reference. One pattern per field, in the field order the model reports; a struct pattern with .. is written with _ for the omitted fields.

§Examples
use match_lang::Pat;

let pair = Pat::product([Pat::bool(true), Pat::wild()]);
assert_eq!(pair.to_string(), "(true, _)");
Source

pub fn bool(value: bool) -> Pat

A boolean literal, for a Bool type.

§Examples
use match_lang::Pat;

assert_eq!(Pat::bool(false).to_string(), "false");
Source

pub fn int(value: i128) -> Pat

An integer literal, for an Int or BigInt type. Equivalent to Pat::range(value, value).

§Examples
use match_lang::Pat;

assert_eq!(Pat::int(-7).to_string(), "-7");
Source

pub fn range(lo: i128, hi: i128) -> Pat

An inclusive integer range lo..=hi. An exclusive range lo..hi is range(lo, hi - 1); a half-open range takes the type’s bound for the open end. lo > hi is reported as Reason::EmptyRange, and a range that leaves an Int type’s bounds as Reason::OutOfDomain.

§Examples
use match_lang::Pat;

assert_eq!(Pat::range(0, 9).to_string(), "0..=9");
Source

pub fn char(value: char) -> Pat

A character literal, for a Char type.

§Examples
use match_lang::Pat;

assert_eq!(Pat::char('x').to_string(), "'x'");
Source

pub fn char_range(lo: char, hi: char) -> Pat

An inclusive character range lo..=hi. It covers every Unicode scalar value between the two, so a range across the surrogate block simply skips it.

§Examples
use match_lang::Pat;

assert_eq!(Pat::char_range('a', 'z').to_string(), "'a'..='z'");
Source

pub fn lit(id: u64) -> Pat

An opaque literal, for an Opaque type such as strings or floats compared by equality. Two literals are the same value exactly when their ids are equal, so the caller interns the literal (for example a string through intern-lang) and passes the id.

§Examples
use match_lang::Pat;

assert_eq!(Pat::lit(3).to_string(), "lit#3");
Source

pub fn or(alternatives: impl IntoIterator<Item = Pat>) -> Pat

An or-pattern: matches when any alternative matches, trying them left to right. Alternatives may themselves be or-patterns. An or-pattern with no alternatives matches nothing.

§Examples
use match_lang::Pat;

let p = Pat::or([Pat::int(1), Pat::int(2)]);
assert_eq!(p.to_string(), "1 | 2");
Source

pub fn slice(elements: impl IntoIterator<Item = Pat>) -> Pat

A fixed-length slice or array pattern [p1, ..., pn]: matches a sequence of exactly n elements. Against an Array type, n must equal the array’s length (Reason::Length otherwise).

§Examples
use match_lang::Pat;

assert_eq!(Pat::slice([Pat::int(1), Pat::wild()]).to_string(), "[1, _]");
Source

pub fn slice_rest( prefix: impl IntoIterator<Item = Pat>, suffix: impl IntoIterator<Item = Pat>, ) -> Pat

A slice pattern with a rest, [p1, ..., pk, .., q1, ..., qm]: matches any sequence of at least k + m elements whose first k match prefix and whose last m match suffix.

§Examples
use match_lang::Pat;

let first_and_last = Pat::slice_rest([Pat::int(0)], [Pat::int(9)]);
assert_eq!(first_and_last.to_string(), "[0, .., 9]");
assert_eq!(Pat::slice_rest([], []).to_string(), "[..]");

Trait Implementations§

Source§

impl Clone for Pat

Source§

fn clone(&self) -> Pat

Clones the tree with an explicit stack instead of recursion.

1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Pat

Source§

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

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

impl Display for Pat

Source§

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

Prints the pattern in a neutral syntax: _, #i(..) for variants, (..) for products, literals and lo..=hi ranges, lit#id for opaque literals, [..] for slices, and p | q for alternatives.

Source§

impl Drop for Pat

Source§

fn drop(&mut self)

Drops the tree with an explicit stack instead of recursion.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl Freeze for Pat

§

impl RefUnwindSafe for Pat

§

impl Send for Pat

§

impl Sync for Pat

§

impl Unpin for Pat

§

impl UnsafeUnpin for Pat

§

impl UnwindSafe for Pat

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.