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
use crate::{
    iter::{
        constructors::{
            into_con_iter::IntoConcurrentIter, into_exact_con_iter::IntoExactSizeConcurrentIter,
        },
        implementors::range::ConIterOfRange,
    },
    ConcurrentIterable,
};
use std::ops::{Add, Range, Sub};

impl<Idx> ConcurrentIterable for Range<Idx>
where
    Idx: Send
        + Sync
        + Clone
        + Copy
        + From<usize>
        + Into<usize>
        + Add<Idx, Output = Idx>
        + Sub<Idx, Output = Idx>
        + Ord,
{
    type Item<'i> = Idx where Self: 'i;

    type ConIter<'i> = ConIterOfRange<Idx> where Self: 'i;

    fn con_iter(&self) -> Self::ConIter<'_> {
        Self::ConIter::new(self.clone())
    }
}

impl<Idx> IntoConcurrentIter for Range<Idx>
where
    Idx: Send
        + Sync
        + Clone
        + Copy
        + From<usize>
        + Into<usize>
        + Add<Idx, Output = Idx>
        + Sub<Idx, Output = Idx>
        + Ord,
{
    type Item = Idx;

    type ConIter = ConIterOfRange<Idx>;

    fn into_con_iter(self) -> Self::ConIter {
        Self::ConIter::new(self)
    }
}

impl<Idx> IntoExactSizeConcurrentIter for Range<Idx>
where
    Idx: Send
        + Sync
        + Clone
        + Copy
        + From<usize>
        + Into<usize>
        + Add<Idx, Output = Idx>
        + Sub<Idx, Output = Idx>
        + Ord,
{
    type Item = Idx;

    type ExactConIter = ConIterOfRange<Idx>;

    fn into_exact_con_iter(self) -> Self::ExactConIter {
        Self::ExactConIter::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ConIterOfIter, ConcurrentIter};

    #[test]
    fn con_iter() {
        let values = 42..45;

        let con_iter: ConIterOfRange<_> = values.con_iter();
        assert_eq!(con_iter.next(), Some(42));
        assert_eq!(con_iter.next(), Some(43));
        assert_eq!(con_iter.next(), Some(44));
        assert_eq!(con_iter.next(), None);

        let con_iter: ConIterOfRange<_> = values.clone().into_con_iter();
        assert_eq!(con_iter.next(), Some(42));
        assert_eq!(con_iter.next(), Some(43));
        assert_eq!(con_iter.next(), Some(44));
        assert_eq!(con_iter.next(), None);

        let con_iter: ConIterOfRange<_> = values.into_exact_con_iter();
        assert_eq!(con_iter.next(), Some(42));
        assert_eq!(con_iter.next(), Some(43));
        assert_eq!(con_iter.next(), Some(44));
        assert_eq!(con_iter.next(), None);
    }

    #[test]
    fn into_con_iter() {
        use crate::IterIntoConcurrentIter;

        let values = 42..45;

        let con_iter: ConIterOfIter<_, _> = values.into_iter().take(2).into_con_iter();
        assert_eq!(con_iter.next(), Some(42));
        assert_eq!(con_iter.next(), Some(43));
        assert_eq!(con_iter.next(), None);
    }
}