Function assert_in

Source
pub fn assert_in<T: Ord + Copy>(
    value: &T,
    range: &Range<T>,
) -> Result<T, Outside<T>>
Expand description

Assert that the object is exactly within the boundaries given by the range operand.

§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::{Outside, assert_in};

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

    Ok(())
}

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