par_iter/iter/
cloned.rs

1use std::iter;
2
3use super::{plumbing::*, *};
4
5/// `Cloned` is an iterator that clones the elements of an underlying iterator.
6///
7/// This struct is created by the [`cloned()`] method on [`ParallelIterator`]
8///
9/// [`cloned()`]: trait.ParallelIterator.html#method.cloned
10/// [`ParallelIterator`]: trait.ParallelIterator.html
11#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
12#[derive(Debug, Clone)]
13pub struct Cloned<I: ParallelIterator> {
14    base: I,
15}
16
17impl<I> Cloned<I>
18where
19    I: ParallelIterator,
20{
21    /// Creates a new `Cloned` iterator.
22    pub(super) fn new(base: I) -> Self {
23        Cloned { base }
24    }
25}
26
27impl<'a, T, I> ParallelIterator for Cloned<I>
28where
29    I: ParallelIterator<Item = &'a T>,
30    T: 'a + Clone + Send + Sync,
31{
32    type Item = T;
33
34    fn drive_unindexed<C>(self, consumer: C) -> C::Result
35    where
36        C: UnindexedConsumer<Self::Item>,
37    {
38        let consumer1 = ClonedConsumer::new(consumer);
39        self.base.drive_unindexed(consumer1)
40    }
41
42    fn opt_len(&self) -> Option<usize> {
43        self.base.opt_len()
44    }
45}
46
47impl<'a, T, I> IndexedParallelIterator for Cloned<I>
48where
49    I: IndexedParallelIterator<Item = &'a T>,
50    T: 'a + Clone + Send + Sync,
51{
52    fn drive<C>(self, consumer: C) -> C::Result
53    where
54        C: Consumer<Self::Item>,
55    {
56        let consumer1 = ClonedConsumer::new(consumer);
57        self.base.drive(consumer1)
58    }
59
60    fn len(&self) -> usize {
61        self.base.len()
62    }
63
64    fn with_producer<CB>(self, callback: CB) -> CB::Output
65    where
66        CB: ProducerCallback<Self::Item>,
67    {
68        return self.base.with_producer(Callback { callback });
69
70        struct Callback<CB> {
71            callback: CB,
72        }
73
74        impl<'a, T, CB> ProducerCallback<&'a T> for Callback<CB>
75        where
76            CB: ProducerCallback<T>,
77            T: 'a + Clone + Send,
78        {
79            type Output = CB::Output;
80
81            fn callback<P>(self, base: P) -> CB::Output
82            where
83                P: Producer<Item = &'a T>,
84            {
85                let producer = ClonedProducer { base };
86                self.callback.callback(producer)
87            }
88        }
89    }
90}
91
92/// ////////////////////////////////////////////////////////////////////////
93
94struct ClonedProducer<P> {
95    base: P,
96}
97
98impl<'a, T, P> Producer for ClonedProducer<P>
99where
100    P: Producer<Item = &'a T>,
101    T: 'a + Clone,
102{
103    type IntoIter = iter::Cloned<P::IntoIter>;
104    type Item = T;
105
106    fn into_iter(self) -> Self::IntoIter {
107        self.base.into_iter().cloned()
108    }
109
110    fn min_len(&self) -> usize {
111        self.base.min_len()
112    }
113
114    fn max_len(&self) -> usize {
115        self.base.max_len()
116    }
117
118    fn split_at(self, index: usize) -> (Self, Self) {
119        let (left, right) = self.base.split_at(index);
120        (
121            ClonedProducer { base: left },
122            ClonedProducer { base: right },
123        )
124    }
125
126    fn fold_with<F>(self, folder: F) -> F
127    where
128        F: Folder<Self::Item>,
129    {
130        self.base.fold_with(ClonedFolder { base: folder }).base
131    }
132}
133
134/// ////////////////////////////////////////////////////////////////////////
135/// Consumer implementation
136
137struct ClonedConsumer<C> {
138    base: C,
139}
140
141impl<C> ClonedConsumer<C> {
142    fn new(base: C) -> Self {
143        ClonedConsumer { base }
144    }
145}
146
147impl<'a, T, C> Consumer<&'a T> for ClonedConsumer<C>
148where
149    C: Consumer<T>,
150    T: 'a + Clone,
151{
152    type Folder = ClonedFolder<C::Folder>;
153    type Reducer = C::Reducer;
154    type Result = C::Result;
155
156    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
157        let (left, right, reducer) = self.base.split_at(index);
158        (
159            ClonedConsumer::new(left),
160            ClonedConsumer::new(right),
161            reducer,
162        )
163    }
164
165    fn into_folder(self) -> Self::Folder {
166        ClonedFolder {
167            base: self.base.into_folder(),
168        }
169    }
170
171    fn full(&self) -> bool {
172        self.base.full()
173    }
174}
175
176impl<'a, T, C> UnindexedConsumer<&'a T> for ClonedConsumer<C>
177where
178    C: UnindexedConsumer<T>,
179    T: 'a + Clone,
180{
181    fn split_off_left(&self) -> Self {
182        ClonedConsumer::new(self.base.split_off_left())
183    }
184
185    fn to_reducer(&self) -> Self::Reducer {
186        self.base.to_reducer()
187    }
188}
189
190struct ClonedFolder<F> {
191    base: F,
192}
193
194impl<'a, T, F> Folder<&'a T> for ClonedFolder<F>
195where
196    F: Folder<T>,
197    T: 'a + Clone,
198{
199    type Result = F::Result;
200
201    fn consume(self, item: &'a T) -> Self {
202        ClonedFolder {
203            base: self.base.consume(item.clone()),
204        }
205    }
206
207    fn consume_iter<I>(mut self, iter: I) -> Self
208    where
209        I: IntoIterator<Item = &'a T>,
210    {
211        self.base = self.base.consume_iter(iter.into_iter().cloned());
212        self
213    }
214
215    fn complete(self) -> F::Result {
216        self.base.complete()
217    }
218
219    fn full(&self) -> bool {
220        self.base.full()
221    }
222}