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
//! Range related types

use num::{BigUint, One};

use crate::{
    nonzero::NonZero, nonzero_biguint::NonZeroBigUint, traits::PrimUint, traits::ToNonZero,
};

#[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;
            self.value += One::one();
            Some(current_value)
        } else {
            None
        }
    }
}

#[derive(Debug, Clone)]
pub struct RangeNonZeroBigUint {
    pub start: NonZeroBigUint,
    pub stop: NonZeroBigUint,

    // Keeps track of the current value
    value: NonZeroBigUint,
}

impl RangeNonZeroBigUint {
    pub fn new(start: NonZeroBigUint, stop: NonZeroBigUint) -> Self {
        Self {
            start: start.clone(),
            stop,
            value: start,
        }
    }

    pub fn from_biguints(start: BigUint, stop: BigUint) -> Option<Self> {
        let start = NonZeroBigUint::new(start)?;
        let stop = NonZeroBigUint::new(stop)?;
        Some(Self::new(start, stop))
    }
}

impl Iterator for RangeNonZeroBigUint {
    type Item = NonZeroBigUint;

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