//! 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));
    }
}
