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
use super::*;
use super::internal::*;
use std;
pub struct VecIter<T: Send> {
vec: Vec<T>,
consumed: bool,
}
impl<T: Send> IntoParallelIterator for Vec<T> {
type Item = T;
type Iter = VecIter<T>;
fn into_par_iter(self) -> Self::Iter {
VecIter { vec: self, consumed: false }
}
}
impl<T: Send> ParallelIterator for VecIter<T> {
type Item = T;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where C: UnindexedConsumer<Self::Item>
{
bridge(self, consumer)
}
}
impl<T: Send> BoundedParallelIterator for VecIter<T> {
fn upper_bound(&mut self) -> usize {
ExactParallelIterator::len(self)
}
fn drive<C>(self, consumer: C) -> C::Result
where C: Consumer<Self::Item>
{
bridge(self, consumer)
}
}
impl<T: Send> ExactParallelIterator for VecIter<T> {
fn len(&mut self) -> usize {
self.vec.len()
}
}
impl<T: Send> IndexedParallelIterator for VecIter<T> {
fn with_producer<CB>(mut self, callback: CB) -> CB::Output
where CB: ProducerCallback<Self::Item>
{
let producer = VecProducer { slice: &mut self.vec };
self.consumed = true;
callback.callback(producer)
}
}
impl<T: Send> Drop for VecIter<T> {
fn drop(&mut self) {
if self.consumed {
unsafe { self.vec.set_len(0) };
}
}
}
pub struct VecProducer<'data, T: 'data + Send> {
slice: &'data mut [T]
}
impl<'data, T: 'data + Send> Producer for VecProducer<'data, T> {
fn cost(&mut self, len: usize) -> f64 {
len as f64
}
fn split_at(mut self, index: usize) -> (Self, Self) {
let slice = std::mem::replace(&mut self.slice, &mut []);
let (left, right) = slice.split_at_mut(index);
(VecProducer { slice: left }, VecProducer { slice: right })
}
}
impl<'data, T: 'data + Send> IntoIterator for VecProducer<'data, T> {
type Item = T;
type IntoIter = SliceDrain<'data, T>;
fn into_iter(mut self) -> Self::IntoIter {
let slice = std::mem::replace(&mut self.slice, &mut []);
SliceDrain { iter: slice.iter_mut() }
}
}
impl<'data, T: 'data + Send> Drop for VecProducer<'data, T> {
fn drop(&mut self) {
SliceDrain { iter: self.slice.iter_mut() };
}
}
pub struct SliceDrain<'data, T: 'data> {
iter: std::slice::IterMut<'data, T>
}
impl<'data, T: 'data> Iterator for SliceDrain<'data, T> {
type Item = T;
fn next(&mut self) -> Option<T> {
self.iter.next()
.map(|ptr| unsafe { std::ptr::read(ptr) })
}
}
impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
fn drop(&mut self) {
for ptr in &mut self.iter {
unsafe { std::ptr::read(ptr); }
}
}
}