1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#[macro_export]
macro_rules! NonZeroUsize {
($num:expr) => {
NonZeroUsize!($num, "Must be non-zero")
};
($num:expr, $message:literal) => {
std::num::NonZeroUsize::new($num).expect($message)
};
}
#[cfg(test)]
mod tests {
#[test]
fn test_nonzero() {
assert_eq!(1, NonZeroUsize!(1).get());
assert_eq!(std::usize::MAX, NonZeroUsize!(std::usize::MAX).get());
}
#[test]
#[should_panic(expected = "Must be non-zero")]
fn test_zero() {
NonZeroUsize!(0);
}
#[test]
#[should_panic(expected = "Custom message")]
fn test_zero_custom_message() {
NonZeroUsize!(0, "Custom message");
}
}