Skip to main content

Witness

Struct Witness 

Source
pub struct Witness<T> { /* private fields */ }
Expand description

A pattern describing values that no arm matches: proof that a match is not exhaustive.

Every value matching the witness is matched by no unguarded arm (a Wild node stands for any value of its type, an Other node for any value no pattern names). Every witness matches at least one value.

A witness is a tree of WitnessRef nodes, each with a Ctor, the type of the value at that position, and the fields beneath it. It prints in a neutral syntax with Display, or with the language’s variant names through display.

§Examples

use match_lang::{check, Arm, Ctor, Model, Pat, Signature};

struct Bytes;

impl Model for Bytes {
    type Ty = ();
    fn signature(&self, _: &()) -> Signature<()> {
        Signature::Int { min: 0, max: 255 }
    }
    fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
}

let arms = [Arm::new(Pat::range(0, 9)), Arm::new(Pat::range(20, 255))];
let report = check(&Bytes, &(), &arms)?;
let witness = &report.missing()[0];
assert_eq!(witness.ctor(), Ctor::Int { lo: 10, hi: 19 });
assert_eq!(witness.to_string(), "10..=19");

Implementations§

Source§

impl<T> Witness<T>

Source

pub fn root(&self) -> WitnessRef<'_, T>

The root node.

§Examples
let report = check(&Flag, &(), &[Arm::new(Pat::bool(true))])?;
assert_eq!(report.missing()[0].root().ctor(), Ctor::Bool(false));
Source

pub fn ctor(&self) -> Ctor

The root’s constructor; shorthand for root().ctor().

§Examples
let report = check(&Flag, &(), &[Arm::new(Pat::bool(false))])?;
assert_eq!(report.missing()[0].ctor(), Ctor::Bool(true));
Source

pub fn ty(&self) -> &T

The type of the whole witness: the scrutinee’s type.

§Examples
let report = check(&Flag, &"bool", &[])?;
assert_eq!(*report.missing()[0].ty(), "bool");
Source

pub fn fields(&self) -> WitnessFields<'_, T> ⓘ

The root’s fields; shorthand for root().fields().

§Examples
let arms = [Arm::new(Pat::product([Pat::bool(true), Pat::wild()]))];
let report = check(&Pair, &true, &arms)?;
let fields: Vec<Ctor> = report.missing()[0].fields().map(|f| f.ctor()).collect();
assert_eq!(fields, [Ctor::Bool(false), Ctor::Wild]);
Source

pub fn display<'a, M: Model<Ty = T>>( &'a self, model: &'a M, ) -> WitnessDisplay<'a, M>

Prints the witness with the model’s variant names (see Model::variant_name).

§Examples
use match_lang::{check, Arm, Model, Pat, Signature};

struct Opt;

impl Model for Opt {
    type Ty = u8; // 0: Option<bool>, 1: bool
    fn signature(&self, ty: &u8) -> Signature<u8> {
        if *ty == 0 { Signature::Sum { count: 2 } } else { Signature::Bool }
    }
    fn fields(&self, _: &u8, variant: u32, out: &mut Vec<u8>) {
        if variant == 1 {
            out.push(1);
        }
    }
    fn variant_name(&self, _: &u8, variant: u32) -> Option<&str> {
        Some(if variant == 0 { "None" } else { "Some" })
    }
}

let arms = [Arm::new(Pat::variant(0, [])), Arm::new(Pat::variant(1, [Pat::bool(true)]))];
let report = check(&Opt, &0, &arms)?;
assert_eq!(report.missing()[0].to_string(), "#1(false)");
assert_eq!(report.missing()[0].display(&Opt).to_string(), "Some(false)");

Trait Implementations§

Source§

impl<T: Clone> Clone for Witness<T>

Source§

fn clone(&self) -> Self

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> Debug for Witness<T>

Source§

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

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

impl<T> Display for Witness<T>

Source§

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

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

impl<T: Eq> Eq for Witness<T>

Source§

impl<T: PartialEq> PartialEq for Witness<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq> StructuralPartialEq for Witness<T>

Auto Trait Implementations§

§

impl<T> Freeze for Witness<T>
where Vec<WNode<T>>: Freeze,

§

impl<T> RefUnwindSafe for Witness<T>
where Vec<WNode<T>>: RefUnwindSafe,

§

impl<T> Send for Witness<T>
where Vec<WNode<T>>: Send,

§

impl<T> Sync for Witness<T>
where Vec<WNode<T>>: Sync,

§

impl<T> Unpin for Witness<T>
where Vec<WNode<T>>: Unpin,

§

impl<T> UnsafeUnpin for Witness<T>
where Vec<WNode<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Witness<T>
where Vec<WNode<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> 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.