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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Non-zero check returning an [`Option`] for [`U320`].
use super::U320;
impl U320 {
/// Returns `Some(self)` if the value is non-zero, or `None` if it
/// is zero.
///
/// Useful for guarding against division by zero or ensuring a value
/// is meaningful before further computation.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u320::U320;
///
/// assert!(U320::ONE.non_zero().is_some());
/// assert!(U320::ZERO.non_zero().is_none());
/// ```
#[inline]
pub const fn non_zero(self) -> Option<U320> {
if self.is_zero() {
None
} else {
Some(self)
}
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero returns None.
#[test]
fn zero_is_none() {
assert!(U320::ZERO.non_zero().is_none());
}
/// One returns Some.
#[test]
fn one_is_some() {
assert_eq!(U320::ONE.non_zero(), Some(U320::ONE));
}
/// MAX returns Some.
#[test]
fn max_is_some() {
assert_eq!(U320::MAX.non_zero(), Some(U320::MAX));
}
/// Value with only MSB limb set returns Some.
#[test]
fn msb_only() {
let v = U320::from_be_limbs([1, 0, 0, 0, 0]);
assert_eq!(v.non_zero(), Some(v));
}
}