scuffle_bytes_util/
range_check.rs

1//! A helper macro to ensure that a number is within the specified [$lower, $upper] bounds.
2
3/// Enforces that a number is within the specified \[LOWER, UPPER\] bounds.
4///
5/// The brackets indicate that this range is inclusive on both sides.
6#[macro_export]
7macro_rules! range_check {
8    ($n:expr, $lower:expr, $upper:expr) => {{
9        let n = $n;
10
11        #[allow(unused_comparisons, clippy::manual_range_contains)]
12        if n < $lower || n > $upper {
13            ::std::result::Result::Err(::std::io::Error::new(
14                ::std::io::ErrorKind::InvalidData,
15                format!("{} is out of range [{}, {}]: {}", stringify!($n), $lower, $upper, n),
16            ))
17        } else {
18            ::std::result::Result::Ok(())
19        }
20    }};
21}
22
23#[cfg(test)]
24#[cfg_attr(all(test, coverage_nightly), coverage(off))]
25mod tests {
26    #[test]
27    fn u64() {
28        let i = 2u64;
29        range_check!(i, 0, 63).unwrap();
30    }
31}