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
/// Result of a `next_id_and_value` call on a concurrent iterator which contains two bits of information:
/// * `idx`: index of the element in the iterator.
/// * `value`: of the element.
#[derive(Debug)]
pub struct Next<T> {
    /// Index of the element in the iterator.
    pub idx: usize,
    /// Value of the element.
    pub value: T,
}

impl<'a, T: Clone> Next<&'a T> {
    /// Converts the next into one where the `value` is cloned.
    pub fn cloned(self) -> Next<T> {
        Next {
            idx: self.idx,
            value: self.value.clone(),
        }
    }
}

impl<'a, T: Copy> Next<&'a T> {
    /// Converts the next into one where the `value` is copied.
    pub fn copied(self) -> Next<T> {
        Next {
            idx: self.idx,
            value: *self.value,
        }
    }
}

/// A trait representing return types of a `next_chunk` call on a concurrent iterator.
pub struct NextChunk<T, Iter>
where
    Iter: ExactSizeIterator<Item = T>,
{
    /// The index of the first element to be yielded by the `values` iterator.
    pub begin_idx: usize,

    /// Elements in the obtained chunk.
    pub values: Iter,
}

impl<'a, T: Clone, Iter> NextChunk<&'a T, Iter>
where
    Iter: ExactSizeIterator<Item = &'a T>,
{
    /// Converts the next into one where the `values` are cloned.
    pub fn cloned(self) -> NextChunk<T, std::iter::Cloned<Iter>> {
        NextChunk {
            begin_idx: self.begin_idx,
            values: self.values.cloned(),
        }
    }
}

impl<'a, T: Copy, Iter> NextChunk<&'a T, Iter>
where
    Iter: ExactSizeIterator<Item = &'a T>,
{
    /// Converts the next into one where the `values` are copied.
    pub fn copied(self) -> NextChunk<T, std::iter::Copied<Iter>> {
        NextChunk {
            begin_idx: self.begin_idx,
            values: self.values.copied(),
        }
    }
}