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
use super::buffered::cloned_buffered_chunk::ClonedBufferedChunk;
use crate::{ConcurrentIter, ConcurrentIterX, NextChunk};
use std::marker::PhantomData;

/// An concurrent iterator, backed with an atomic iterator, that clones the elements of an underlying iterator.
///
/// This `struct` is created by the `cloned` method on the concurrent iterator.
pub struct Cloned<'a, T, C>
where
    T: Send + Sync + Clone,
{
    iter: C,
    phantom: PhantomData<&'a T>,
}

impl<'a, T, C> Cloned<'a, T, C>
where
    T: Send + Sync + Clone,
{
    pub(crate) fn new(iter: C) -> Self {
        Self {
            iter,
            phantom: PhantomData,
        }
    }

    pub(crate) fn underlying_iter(&self) -> &C {
        &self.iter
    }
}

unsafe impl<'a, T, C> Sync for Cloned<'a, T, C> where T: Send + Sync + Clone {}

unsafe impl<'a, T, C> Send for Cloned<'a, T, C> where T: Send + Sync + Clone {}

impl<'a, T, C> ConcurrentIterX for Cloned<'a, T, C>
where
    T: Send + Sync + Clone,
    C: ConcurrentIterX<Item = &'a T>,
{
    type Item = T;

    type SeqIter = std::iter::Cloned<C::SeqIter>;

    type BufferedIterX = ClonedBufferedChunk<'a, T, C::BufferedIterX>;

    fn into_seq_iter(self) -> Self::SeqIter {
        self.iter.into_seq_iter().cloned()
    }

    fn next_chunk_x(&self, chunk_size: usize) -> Option<impl ExactSizeIterator<Item = Self::Item>> {
        self.iter.next_chunk_x(chunk_size).map(|x| x.cloned())
    }

    fn next(&self) -> Option<Self::Item> {
        self.iter.next().cloned()
    }

    fn skip_to_end(&self) {
        self.iter.skip_to_end()
    }

    #[inline(always)]
    fn try_get_len(&self) -> Option<usize> {
        self.iter.try_get_len()
    }

    #[inline(always)]
    fn try_get_initial_len(&self) -> Option<usize> {
        self.iter.try_get_initial_len()
    }
}

impl<'a, T, C> ConcurrentIter for Cloned<'a, T, C>
where
    T: Send + Sync + Clone,
    C: ConcurrentIter<Item = &'a T>,
{
    type BufferedIter = ClonedBufferedChunk<'a, T, C::BufferedIter>;

    #[inline(always)]
    fn next_id_and_value(&self) -> Option<crate::Next<Self::Item>> {
        self.iter.next_id_and_value().map(|x| x.cloned())
    }

    #[inline(always)]
    fn next_chunk(
        &self,
        chunk_size: usize,
    ) -> Option<NextChunk<Self::Item, impl ExactSizeIterator<Item = Self::Item>>> {
        self.iter.next_chunk(chunk_size).map(|x| x.cloned())
    }
}

/// Trait converting a concurrent iterator yielding &T to one yielding T by cloning elements.
pub trait IntoCloned<'a, T, C>
where
    Self: Into<C>,
    T: Send + Sync + Clone + 'a,
    C: ConcurrentIter<Item = &'a T>,
{
    /// Converts this concurrent iterator over references into another concurrent iterator yielding clones of the elements.
    fn cloned(self) -> Cloned<'a, T, C> {
        Cloned::new(self.into())
    }
}

impl<'a, T, C> IntoCloned<'a, T, C> for C
where
    T: Send + Sync + Clone + 'a,
    C: ConcurrentIter<Item = &'a T>,
{
}