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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use super::plumbing::*;
use super::*;

use std::iter;

/// `Copied` is an iterator that copies the elements of an underlying iterator.
///
/// This struct is created by the [`copied()`] method on [`ParallelIterator`]
///
/// [`copied()`]: trait.ParallelIterator.html#method.copied
/// [`ParallelIterator`]: trait.ParallelIterator.html
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[derive(Debug, Clone)]
pub struct Copied<I: ParallelIterator> {
    base: I,
}

impl<I> Copied<I>
where
    I: ParallelIterator,
{
    /// Create a new `Copied` iterator.
    pub(super) fn new(base: I) -> Self {
        Copied { base }
    }
}

impl<'a, T, I> ParallelIterator for Copied<I>
where
    I: ParallelIterator<Item = &'a T>,
    T: 'a + Copy + Send + Sync,
{
    type Item = T;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        let consumer1 = CopiedConsumer::new(consumer);
        self.base.drive_unindexed(consumer1)
    }

    fn opt_len(&self) -> Option<usize> {
        self.base.opt_len()
    }
}

impl<'a, T, I> IndexedParallelIterator for Copied<I>
where
    I: IndexedParallelIterator<Item = &'a T>,
    T: 'a + Copy + Send + Sync,
{
    fn drive<C>(self, consumer: C) -> C::Result
    where
        C: Consumer<Self::Item>,
    {
        let consumer1 = CopiedConsumer::new(consumer);
        self.base.drive(consumer1)
    }

    fn len(&self) -> usize {
        self.base.len()
    }

    fn with_producer<CB>(self, callback: CB) -> CB::Output
    where
        CB: ProducerCallback<Self::Item>,
    {
        return self.base.with_producer(Callback { callback });

        struct Callback<CB> {
            callback: CB,
        }

        impl<'a, T, CB> ProducerCallback<&'a T> for Callback<CB>
        where
            CB: ProducerCallback<T>,
            T: 'a + Copy + Send,
        {
            type Output = CB::Output;

            fn callback<P>(self, base: P) -> CB::Output
            where
                P: Producer<Item = &'a T>,
            {
                let producer = CopiedProducer { base };
                self.callback.callback(producer)
            }
        }
    }
}

/// ////////////////////////////////////////////////////////////////////////

struct CopiedProducer<P> {
    base: P,
}

impl<'a, T, P> Producer for CopiedProducer<P>
where
    P: Producer<Item = &'a T>,
    T: 'a + Copy,
{
    type Item = T;
    type IntoIter = iter::Cloned<P::IntoIter>;

    fn into_iter(self) -> Self::IntoIter {
        // FIXME: use `Iterator::copied()` when Rust 1.36 is our minimum.
        self.base.into_iter().cloned()
    }

    fn min_len(&self) -> usize {
        self.base.min_len()
    }

    fn max_len(&self) -> usize {
        self.base.max_len()
    }

    fn split_at(self, index: usize) -> (Self, Self) {
        let (left, right) = self.base.split_at(index);
        (
            CopiedProducer { base: left },
            CopiedProducer { base: right },
        )
    }

    fn fold_with<F>(self, folder: F) -> F
    where
        F: Folder<Self::Item>,
    {
        self.base.fold_with(CopiedFolder { base: folder }).base
    }
}

/// ////////////////////////////////////////////////////////////////////////
/// Consumer implementation

struct CopiedConsumer<C> {
    base: C,
}

impl<C> CopiedConsumer<C> {
    fn new(base: C) -> Self {
        CopiedConsumer { base }
    }
}

impl<'a, T, C> Consumer<&'a T> for CopiedConsumer<C>
where
    C: Consumer<T>,
    T: 'a + Copy,
{
    type Folder = CopiedFolder<C::Folder>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            CopiedConsumer::new(left),
            CopiedConsumer::new(right),
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        CopiedFolder {
            base: self.base.into_folder(),
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

impl<'a, T, C> UnindexedConsumer<&'a T> for CopiedConsumer<C>
where
    C: UnindexedConsumer<T>,
    T: 'a + Copy,
{
    fn split_off_left(&self) -> Self {
        CopiedConsumer::new(self.base.split_off_left())
    }

    fn to_reducer(&self) -> Self::Reducer {
        self.base.to_reducer()
    }
}

struct CopiedFolder<F> {
    base: F,
}

impl<'a, T, F> Folder<&'a T> for CopiedFolder<F>
where
    F: Folder<T>,
    T: 'a + Copy,
{
    type Result = F::Result;

    fn consume(self, &item: &'a T) -> Self {
        CopiedFolder {
            base: self.base.consume(item),
        }
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = &'a T>,
    {
        // FIXME: use `Iterator::copied()` when Rust 1.36 is our minimum.
        self.base = self.base.consume_iter(iter.into_iter().cloned());
        self
    }

    fn complete(self) -> F::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}