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 guard for [`U384`].
use super::U384;
impl U384 {
/// Returns `Some(self)` if the value is non-zero, or `None` if it is
/// zero.
///
/// Useful for validating that a value is non-zero before using it in
/// operations that require a non-zero operand (e.g., division).
///
/// # Examples
///
/// ```
/// use cnfy_uint::u384::U384;
///
/// assert!(U384::ONE.non_zero().is_some());
/// assert!(U384::ZERO.non_zero().is_none());
/// ```
#[inline]
pub const fn non_zero(self) -> Option<U384> {
if self.is_zero() {
None
} else {
Some(self)
}
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero returns None.
#[test]
fn zero_is_none() {
assert!(U384::ZERO.non_zero().is_none());
}
/// One returns Some(ONE).
#[test]
fn one_is_some() {
assert_eq!(U384::ONE.non_zero(), Some(U384::ONE));
}
/// MAX returns Some(MAX).
#[test]
fn max_is_some() {
assert_eq!(U384::MAX.non_zero(), Some(U384::MAX));
}
/// Arbitrary non-zero value returns Some.
#[test]
fn arbitrary() {
let v = U384::from_be_limbs([0, 0, 0, 0, 0, 42]);
assert_eq!(v.non_zero(), Some(v));
}
}