Skip to main content

Report

Struct Report 

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

What checking a match found: missing cases, unreachable arms, and redundant or-pattern alternatives.

§Examples

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

struct Flag;

impl Model for Flag {
    type Ty = ();
    fn signature(&self, _: &()) -> Signature<()> {
        Signature::Bool
    }
    fn fields(&self, _: &(), _: u32, _: &mut Vec<()>) {}
}

let arms = [
    Arm::new(Pat::bool(true)),
    Arm::new(Pat::or([Pat::bool(false), Pat::bool(true)])),
    Arm::new(Pat::wild()),
];
let report = check(&Flag, &(), &arms)?;
assert!(report.is_exhaustive());
assert_eq!(report.unreachable(), &[2]);
assert_eq!(report.redundant()[0].arm, 1);
assert_eq!(report.redundant()[0].node, 2); // the second alternative, `true`

Implementations§

Source§

impl<T> Report<T>

Source

pub fn is_exhaustive(&self) -> bool

Whether every value of the scrutinee’s type is matched by some unguarded arm.

§Examples
let guarded = [Arm::guarded(Pat::wild())];
assert!(!check(&Flag, &(), &guarded)?.is_exhaustive());
Source

pub fn missing(&self) -> &[Witness<T>]

Witnesses of values no unguarded arm matches; empty exactly when the match is exhaustive. There is at least one when it is not, and at most the witness limit. They are a sample chosen to be informative, not an enumeration of every missing value.

§Examples
let report = check(&Flag, &(), &[])?;
let missing: Vec<String> = report.missing().iter().map(|w| w.to_string()).collect();
assert_eq!(missing, ["false", "true"]);
Source

pub fn unreachable(&self) -> &[usize]

The indices of arms no value can reach, in ascending order. An arm is unreachable when every value its pattern matches is matched by an earlier unguarded arm, or when its pattern matches no value at all.

§Examples
let arms = [Arm::new(Pat::wild()), Arm::new(Pat::bool(true))];
assert_eq!(check(&Flag, &(), &arms)?.unreachable(), &[1]);
Source

pub fn redundant(&self) -> &[Redundant]

Or-pattern alternatives that no value reaches, inside arms that are themselves reachable, in arm order and then preorder. Only the outermost redundant alternative is reported: alternatives nested inside it are not listed again.

An alternative is redundant when every value it matches is matched by an earlier unguarded arm or, in an unguarded arm, by an earlier alternative of the same arm.

§Examples
// `true | false | true`: node 0 is the or-pattern, 1..=3 its alternatives.
let arms = [Arm::new(Pat::or([Pat::bool(true), Pat::bool(false), Pat::bool(true)]))];
let report = check(&Flag, &(), &arms)?;
assert_eq!(report.redundant(), &[Redundant { arm: 0, node: 3 }]);
Source

pub fn steps(&self) -> u64

The number of steps the check took, against the step limit. Useful to see how close real matches come to the limit.

§Examples
let report = check(&Flag, &(), &[Arm::new(Pat::wild())])?;
assert!(report.steps() > 0);

Trait Implementations§

Source§

impl<T: Clone> Clone for Report<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> Debug for Report<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<T> RefUnwindSafe for Report<T>

§

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

§

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

§

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

§

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

§

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