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>
impl<T> Witness<T>
Sourcepub fn root(&self) -> WitnessRef<'_, T>
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));Sourcepub fn ctor(&self) -> Ctor
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));Sourcepub fn ty(&self) -> &T
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");Sourcepub fn fields(&self) -> WitnessFields<'_, T> ⓘ
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]);Sourcepub fn display<'a, M: Model<Ty = T>>(
&'a self,
model: &'a M,
) -> WitnessDisplay<'a, M>
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)");