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
use num::{BigUint, CheckedSub, One, PrimInt};

#[deny(missing_docs)]

/// A primitive unsigned integer like u8 or u32 which is known to be non-zero
pub struct NonZero<T: PrimInt + Copy> {
    value: T,
}

impl<T: PrimInt + Copy> NonZero<T> {
    // Tries to create a new instance of NonZero.
    // If `value` equals zero, this returns None,
    // otherwise this returns a new NonZero containing `value`.
    pub fn new(value: T) -> Option<Self> {
        let value: T = value.checked_sub(&T::one())?;
        Some(Self { value })
    }

    /// Unsafe version of new that doesn't ensure the value is nonzero
    pub fn unchecked_new(value: T) -> Self {
        let value = value - T::one();
        Self { value }
    }

    /// Returns a reference to the non-zero value stored in `self`
    pub fn get(&self) -> T {
        self.value + T::one()
    }
}

/// A `BigUint` that is known to be non-zero.
pub struct NonZeroBigUint {
    value: BigUint,
}

impl NonZeroBigUint {
    // Tries to create a new instance of NonZero.
    // If `value` equals zero, this returns None,
    // otherwise this returns a new NonZero containing `value`.
    pub fn new(value: BigUint) -> Option<Self> {
        let value: BigUint = value.checked_sub(&BigUint::one())?;
        Some(Self { value })
    }

    /// Unsafe version of new that doesn't ensure the value is nonzero
    pub fn unchecked_new(value: BigUint) -> Self {
        let value: BigUint = value - BigUint::one();
        Self { value }
    }

    /// Returns a reference to the non-zero value stored in `self`
    pub fn get(&self) -> BigUint {
        self.value.clone() + BigUint::one()
    }
}