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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use derive_more::{
    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Display,
    DivAssign, MulAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use num::{PrimInt, Unsigned};

/// A wrapper around a primitive non-zero integer like `i32` or `u32`.
#[derive(
    Copy,
    Clone,
    PartialOrd,
    Ord,
    PartialEq,
    Eq,
    Add,
    Sub,
    AddAssign,
    SubAssign,
    MulAssign,
    DivAssign,
    Shl,
    Shr,
    ShlAssign,
    ShrAssign,
    BitAnd,
    BitOr,
    BitXor,
    BitAndAssign,
    BitOrAssign,
    BitXorAssign,
    Display,
    Debug,
)]
pub struct NonZero<T: PrimInt> {
    value: T,
}

impl<T: PrimInt> NonZero<T> {
    pub fn new(value: T) -> Option<Self> {
        if value.is_zero() {
            None
        } else {
            Some(Self { value })
        }
    }

    /// Returns a destructured copy of the NonZero value.
    pub fn get(&self) -> T {
        self.value
    }
}

impl<T: PrimInt> std::ops::Mul for NonZero<T> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            value: (self.value * rhs.value),
        }
    }
}

impl<T: PrimInt> std::ops::Div for NonZero<T> {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        // This would always work, but returning an Option is annoying.
        // Self::new(self.value * rhs.value)

        // Instead, we'll simply use 1 as our minimum value
        let ans: T = (self.value / rhs.value).min(T::one());
        Self { value: ans }
    }
}

pub struct RangeNonZeroUnsigned<T: PrimInt + Unsigned> {
    pub start: NonZero<T>,
    pub stop: NonZero<T>,

    // Keeps track of the current value
    value: NonZero<T>,
}

impl<T: PrimInt + Unsigned> RangeNonZeroUnsigned<T> {
    pub fn new(start: NonZero<T>, stop: NonZero<T>) -> Self {
        Self {
            start,
            stop,
            value: start,
        }
    }

    pub fn from_primitives(start: T, stop: T) -> Option<Self> {
        let start = start.to_nonzero()?;
        let stop = stop.to_nonzero()?;
        Some(Self {
            start,
            stop,
            value: start,
        })
    }
}

impl<T: PrimInt + std::ops::AddAssign + Unsigned> Iterator for RangeNonZeroUnsigned<T> {
    type Item = NonZero<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.value < self.stop {
            let one: NonZero<T> = NonZero { value: T::one() };
            self.value += one;
            Some(self.value)
        } else {
            None
        }
    }
}

// /// Retursn a 'range' of `NonZero<u8>`s
// pub fn range_nonzero_u8(
//     start: NonZero<u8>,
//     stop: NonZero<u8>,
// ) -> impl Iterator<Item = NonZero<u8>> {
//     // Because NonZero<u8> doesn't implement std::iter::Step,
//     // we just yank the values out of start and stop.

//     // let start = start.value;
//     // let stop = stop.value;

//     // (start..stop).map(|value| NonZero { value })

//     (start..stop)
// }

pub trait ToNonZero
where
    Self: PrimInt,
{
    fn to_nonzero(self) -> Option<NonZero<Self>> {
        NonZero::new(self)
    }
}

impl<T: PrimInt> ToNonZero for T {}

mod tests {

    #[test]
    fn ops_work() {
        use crate::ToNonZero;
        let one: crate::NonZero<u8> = 1u8.to_nonzero().unwrap();
        let two: crate::NonZero<u8> = 2u8.to_nonzero().unwrap();
        let three: crate::NonZero<u8> = 3u8.to_nonzero().unwrap();

        // + - * /
        assert_eq!(one + two, three);
        assert_eq!(three - two, one);
        assert_eq!(two * one, two);
        assert_eq!(three / two, one);
    }

    #[test]
    fn ranges_work() {
        use crate::RangeNonZeroUnsigned;
        let _ = RangeNonZeroUnsigned::from_primitives(1u8, 10u8).unwrap();
        let _ = RangeNonZeroUnsigned::from_primitives(1u16, 10u16).unwrap();
        let _ = RangeNonZeroUnsigned::from_primitives(1u32, 10u32).unwrap();
        let _ = RangeNonZeroUnsigned::from_primitives(1u64, 10u64).unwrap();
        let _ = RangeNonZeroUnsigned::from_primitives(1u128, 10u128).unwrap();
    }
}