cnfy-uint 0.2.3

Zero-dependency 256-bit unsigned integer arithmetic for cryptographic applications
Documentation
//! 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));
    }
}