Skip to main content

assert_eq

Function assert_eq 

Source
pub fn assert_eq<T: Copy + Eq + Debug + Display>(
    actual: &T,
    expected: &T,
) -> Result<T, Mismatch<T>>
Expand description

Assert that the object is exactly equal to the provided test value.

§Motivation

Oftentimes one really only needs an assertion to be propagated upwards. Given that try blocks are not stable, this syntax has some merit. This assert can be used inside function arguments, at the tops of functions to get rid of an ugly if and makes it explicit that what you want is to do what the standard library’s assert_eq! does, but to create an error rather than panic.

§Examples

use goof::{Mismatch, assert_eq};

fn fallible_func(thing: &[u8]) -> Result<(), Mismatch<usize>> {
    assert_eq(&32, &thing.len())?;

    Ok(())
}

assert_eq!(fallible_func(&[]).unwrap_err(), assert_eq(&32, &0).unwrap_err())