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
use crate::{
    iter::constructors::{
        con_iterable::ConcurrentIterable, into_exact_con_iter::IntoExactSizeConcurrentIter,
    },
    ConIterOfSlice, IntoConcurrentIter,
};

impl<'a, T: Send + Sync> ConcurrentIterable for &'a [T] {
    type Item<'i> = &'i T where
    Self: 'i;

    type ConIter<'i> = ConIterOfSlice<'i, T> where
        Self: 'i;

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

impl<'a, T: Send + Sync> IntoConcurrentIter for &'a [T] {
    type Item = &'a T;

    type ConIter = ConIterOfSlice<'a, T>;

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

impl<'a, T: Send + Sync> IntoExactSizeConcurrentIter for &'a [T] {
    type Item = &'a T;

    type ExactConIter = ConIterOfSlice<'a, T>;

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

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

    #[test]
    fn con_iter() {
        let values = ['a', 'b', 'c'];
        let slice = values.as_slice();

        let con_iter = slice.con_iter();
        assert_eq!(con_iter.next(), Some(&'a'));
        assert_eq!(con_iter.next(), Some(&'b'));
        assert_eq!(con_iter.next(), Some(&'c'));
        assert_eq!(con_iter.next(), None);

        let con_iter = slice.into_con_iter();
        assert_eq!(con_iter.next(), Some(&'a'));
        assert_eq!(con_iter.next(), Some(&'b'));
        assert_eq!(con_iter.next(), Some(&'c'));
        assert_eq!(con_iter.next(), None);

        let con_iter = slice.into_exact_con_iter();
        assert_eq!(con_iter.next(), Some(&'a'));
        assert_eq!(con_iter.next(), Some(&'b'));
        assert_eq!(con_iter.next(), Some(&'c'));
        assert_eq!(con_iter.next(), None);
    }
}