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
use super::mut_handle::MutHandle;
use core::{cell::UnsafeCell, marker::PhantomData};
pub struct IterCell<T, I>
where
I: Iterator<Item = T>,
{
iter: UnsafeCell<I>,
num_taken: UnsafeCell<usize>,
phantom: PhantomData<T>,
}
impl<T, I> From<I> for IterCell<T, I>
where
I: Iterator<Item = T>,
{
fn from(iter: I) -> Self {
Self {
iter: iter.into(),
num_taken: 0.into(),
phantom: PhantomData,
}
}
}
impl<T, I> IterCell<T, I>
where
I: Iterator<Item = T>,
{
pub fn into_inner(self) -> I {
self.iter.into_inner()
}
/// Pulls the next element from the iterator and returns its enumerated value.
///
/// Returns None if the iterator is completely consumed.
/// In this case, the `handle` will finalize its state as COMPLETED when dropped.
///
/// # SAFETY
///
/// Only one thread can call this method at a given instant.
/// This is satisfied by the mut handle.
#[inline(always)]
pub fn next(&self, mut handle: MutHandle) -> Option<T> {
match unsafe { &mut *self.iter.get() }.next() {
Some(item) => {
let num_taken = unsafe { &mut *self.num_taken.get() };
let idx = *num_taken;
*num_taken = idx + 1;
Some(item)
}
None => {
handle.set_target_to_completed();
None
}
}
}
/// Pulls the next element from the iterator and returns its enumerated value.
///
/// Returns None if the iterator is completely consumed.
/// In this case, the `handle` will finalize its state as COMPLETED when dropped.
///
/// # SAFETY
///
/// Only one thread can call this method at a given instant.
/// This is satisfied by the mut handle.
#[inline(always)]
pub fn next_with_idx(&self, mut handle: MutHandle) -> Option<(usize, T)> {
match unsafe { &mut *self.iter.get() }.next() {
Some(item) => {
let num_taken = unsafe { &mut *self.num_taken.get() };
let idx = *num_taken;
*num_taken = idx + 1;
Some((idx, item))
}
None => {
handle.set_target_to_completed();
None
}
}
}
/// Pulls and writes chunk-size (`buffer.len()`) elements from the iterator into the given `buffer` starting from position 0.
///
/// Returns the pair of (begin_idx, num_taken):
///
/// * begin_idx: index of the first taken item.
/// * num_taken: number of items pulled from the iterator; the method tries to pull `buffer.len()` items, however, might stop
/// early if the iterator is completely consumed.
///
/// If the method returns num_taken < buffer.len(); i.e., if the iterator is completely consumed,
/// the `handle` will finalize its state as COMPLETED when dropped.
///
/// # SAFETY
///
/// Only one thread can call this method at a given instant.
/// This is satisfied by the mut handle.
pub fn next_chunk_to_buffer(
&self,
mut handle: MutHandle,
buffer: &mut [Option<T>],
) -> (usize, usize) {
let num_taken = unsafe { &mut *self.num_taken.get() };
let begin_idx = *num_taken;
let iter = unsafe { &mut *self.iter.get() };
let mut num_taken_now = 0;
for x in buffer.iter_mut() {
match iter.next() {
Some(item) => {
*x = Some(item);
num_taken_now += 1;
}
None => handle.set_target_to_completed(),
}
}
*num_taken += num_taken_now;
(begin_idx, num_taken_now)
}
pub fn size_hint(&self, _handle: MutHandle) -> (usize, Option<usize>) {
let iter = unsafe { &mut *self.iter.get() };
iter.size_hint()
}
pub fn len(&self, _handle: MutHandle) -> usize
where
I: ExactSizeIterator,
{
let iter = unsafe { &mut *self.iter.get() };
iter.len()
}
}