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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use num::{traits::Pow, Integer, One, PrimInt, Unsigned};
use num::{BigUint, Zero};
use std::fmt::Debug;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

/// A wrapper around a primitive non-zero integer like `i32` or `u32`.
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug)]
pub struct NonZero<T: PrimUint> {
    value: T,
}

pub trait PrimUint: Sized + Debug + PrimInt + Unsigned + Integer {}
impl PrimUint for u8 {}
impl PrimUint for u16 {}
impl PrimUint for u32 {}
impl PrimUint for u64 {}
impl PrimUint for u128 {}

impl<T: PrimUint> NonZero<T> {
    pub fn is_even(&self) -> bool {
        self.get().is_even()
    }
    pub fn is_odd(&self) -> bool {
        self.get().is_odd()
    }
}

impl<T: PrimUint> 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: PrimUint> Add for NonZero<T> {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() + rhs.get(),
        }
    }
}

impl<T: PrimUint> Sub for NonZero<T> {
    type Output = Self;
    #[cfg(debug_assertions)]
    fn sub(self, rhs: Self) -> Self::Output {
        if self < rhs {
            panic!("{self:?} - {rhs:?} produces an underflow or value equal to zero");
        }

        Self {
            value: self.get() - rhs.get(),
        }
    }

    #[cfg(not(debug_assertions))]
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() - rhs.get(),
        }
    }
}

impl<T: PrimUint> Mul for NonZero<T> {
    type Output = Self;

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

impl<T: PrimUint> Div for NonZero<T> {
    type Output = Self;

    #[cfg(debug_assertions)]
    fn div(self, rhs: Self) -> Self::Output {
        if self < rhs {
            panic!("{self:?} / {rhs:?} produces an underflow or value equal to zero");
        }

        Self {
            value: self.get() / rhs.get(),
        }
    }

    #[cfg(not(debug_assertions))]
    fn div(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() / rhs.get(),
        }
    }
}

impl<T: PrimUint> Pow<u32> for NonZero<T> {
    type Output = Self;
    fn pow(self, rhs: u32) -> Self::Output {
        Self {
            value: self.get().pow(rhs),
        }
    }
}

impl<T: PrimUint> AddAssign for NonZero<T> {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs
    }
}

impl<T: PrimUint> SubAssign for NonZero<T> {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs
    }
}

impl<T: PrimUint> MulAssign for NonZero<T> {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs
    }
}

impl<T: PrimUint> DivAssign for NonZero<T> {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs
    }
}

impl<T: PrimUint> One for NonZero<T> {
    fn one() -> Self {
        Self { value: T::one() }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct RangeNonZeroUnsigned<T: PrimUint> {
    pub start: NonZero<T>,
    pub stop: NonZero<T>,

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

impl<T: PrimUint> 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: PrimUint> Iterator for RangeNonZeroUnsigned<T> {
    type Item = NonZero<T>;

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

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

impl<T: PrimUint> ToNonZero for T {}

#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Debug)]
pub struct NonZeroBigUint {
    value: BigUint,
}

impl NonZeroBigUint {
    pub fn is_even(&self) -> bool {
        self.get().is_even()
    }
    pub fn is_odd(&self) -> bool {
        self.get().is_odd()
    }
}

// NonZero<u*> -> NonZeroBigUint
impl From<NonZero<u8>> for NonZeroBigUint {
    fn from(value: NonZero<u8>) -> Self {
        let value: BigUint = BigUint::from(value.value);
        Self { value }
    }
}
impl From<NonZero<u16>> for NonZeroBigUint {
    fn from(value: NonZero<u16>) -> Self {
        let value: BigUint = BigUint::from(value.value);
        Self { value }
    }
}
impl From<NonZero<u32>> for NonZeroBigUint {
    fn from(value: NonZero<u32>) -> Self {
        let value: BigUint = BigUint::from(value.value);
        Self { value }
    }
}
impl From<NonZero<u64>> for NonZeroBigUint {
    fn from(value: NonZero<u64>) -> Self {
        let value: BigUint = BigUint::from(value.value);
        Self { value }
    }
}
impl From<NonZero<u128>> for NonZeroBigUint {
    fn from(value: NonZero<u128>) -> Self {
        let value: BigUint = BigUint::from(value.value);
        Self { value }
    }
}

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

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

impl Add for NonZeroBigUint {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() + rhs.get(),
        }
    }
}

impl Sub for NonZeroBigUint {
    type Output = Self;
    #[cfg(debug_assertions)]
    fn sub(self, rhs: Self) -> Self::Output {
        if self < rhs {
            panic!("{self:?} - {rhs:?} produces an underflow or value equal to zero");
        }

        Self {
            value: self.get() - rhs.get(),
        }
    }

    #[cfg(not(debug_assertions))]
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() - rhs.get(),
        }
    }
}

impl Mul for NonZeroBigUint {
    type Output = Self;

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

impl Div for NonZeroBigUint {
    type Output = Self;

    #[cfg(debug_assertions)]
    fn div(self, rhs: Self) -> Self::Output {
        if self < rhs {
            panic!("{self:?} / {rhs:?} produces an underflow or value equal to zero");
        }

        Self {
            value: self.get() / rhs.get(),
        }
    }

    #[cfg(not(debug_assertions))]
    fn div(self, rhs: Self) -> Self::Output {
        Self {
            value: self.get() / rhs.get(),
        }
    }
}

impl Pow<u32> for NonZeroBigUint {
    type Output = Self;
    fn pow(self, rhs: u32) -> Self::Output {
        Self {
            value: self.get().pow(rhs),
        }
    }
}

impl AddAssign for NonZeroBigUint {
    fn add_assign(&mut self, rhs: Self) {
        self.value.add_assign(rhs.get())
    }
}

impl SubAssign for NonZeroBigUint {
    fn sub_assign(&mut self, rhs: Self) {
        self.value.sub_assign(rhs.get())
    }
}

impl MulAssign for NonZeroBigUint {
    fn mul_assign(&mut self, rhs: Self) {
        self.value.mul_assign(rhs.get())
    }
}

impl DivAssign for NonZeroBigUint {
    fn div_assign(&mut self, rhs: Self) {
        self.value.div_assign(rhs.get())
    }
}

impl One for NonZeroBigUint {
    fn one() -> Self {
        Self {
            value: BigUint::one(),
        }
    }
}

#[allow(unused_imports)]
mod tests {
    use num::One;

    use crate::NonZeroBigUint;

    #[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();
    }

    #[test]
    #[allow(clippy::redundant_clone)]
    fn assignment_ops_for_nonzero_biguints_work() {
        let one = NonZeroBigUint::one();
        let mut big = one.clone();
        big += one.clone();
        assert_eq!(big, one.clone() + one.clone());
    }
}