futures_util/stream/futures_unordered/
iter.rs1use super::task::Task;
2use super::Arc;
3use super::FuturesUnordered;
4use core::marker::PhantomData;
5use core::pin::Pin;
6use core::ptr;
7use core::sync::atomic::Ordering::Relaxed;
8
9#[derive(Debug)]
11pub struct IterPinMut<'a, Fut> {
12 pub(super) task: *const Task<Fut>,
13 pub(super) len: usize,
14 pub(super) _marker: PhantomData<&'a mut FuturesUnordered<Fut>>,
15}
16
17#[derive(Debug)]
19pub struct IterMut<'a, Fut: Unpin>(pub(super) IterPinMut<'a, Fut>);
20
21#[derive(Debug)]
23pub struct IterPinRef<'a, Fut> {
24 pub(super) task: *const Task<Fut>,
25 pub(super) len: usize,
26 pub(super) pending_next_all: *mut Task<Fut>,
27 pub(super) _marker: PhantomData<&'a FuturesUnordered<Fut>>,
28}
29
30#[derive(Debug)]
32pub struct Iter<'a, Fut: Unpin>(pub(super) IterPinRef<'a, Fut>);
33
34#[derive(Debug)]
36pub struct IntoIter<Fut: Unpin> {
37 pub(super) len: usize,
38 pub(super) inner: FuturesUnordered<Fut>,
39}
40
41impl<Fut: Unpin> Iterator for IntoIter<Fut> {
42 type Item = Fut;
43
44 fn next(&mut self) -> Option<Self::Item> {
45 let task = self.inner.head_all.get_mut();
48
49 if (*task).is_null() {
50 return None;
51 }
52
53 unsafe {
54 let head = *task;
55
56 let future = (*(*head).future.get()).take().unwrap();
58
59 let next = (*head).next_all.load(Relaxed);
64 *task = next;
65 if !next.is_null() {
66 *(*next).prev_all.get() = ptr::null_mut();
67 }
68 self.len -= 1;
69
70 let arc = Arc::from_raw(head);
71 arc.next_all.store(self.inner.pending_next_all(), Relaxed);
72 *arc.prev_all.get() = ptr::null_mut();
73 self.inner.release_task(arc);
74
75 Some(future)
76 }
77 }
78
79 fn size_hint(&self) -> (usize, Option<usize>) {
80 (self.len, Some(self.len))
81 }
82}
83
84impl<Fut: Unpin> ExactSizeIterator for IntoIter<Fut> {}
85
86impl<'a, Fut> Iterator for IterPinMut<'a, Fut> {
87 type Item = Pin<&'a mut Fut>;
88
89 fn next(&mut self) -> Option<Self::Item> {
90 if self.task.is_null() {
91 return None;
92 }
93
94 unsafe {
95 let future = (*(*self.task).future.get()).as_mut().unwrap();
96
97 let next = (*self.task).next_all.load(Relaxed);
102 self.task = next;
103 self.len -= 1;
104 Some(Pin::new_unchecked(future))
105 }
106 }
107
108 fn size_hint(&self) -> (usize, Option<usize>) {
109 (self.len, Some(self.len))
110 }
111}
112
113impl<Fut> ExactSizeIterator for IterPinMut<'_, Fut> {}
114
115impl<'a, Fut: Unpin> Iterator for IterMut<'a, Fut> {
116 type Item = &'a mut Fut;
117
118 fn next(&mut self) -> Option<Self::Item> {
119 self.0.next().map(Pin::get_mut)
120 }
121
122 fn size_hint(&self) -> (usize, Option<usize>) {
123 self.0.size_hint()
124 }
125}
126
127impl<Fut: Unpin> ExactSizeIterator for IterMut<'_, Fut> {}
128
129impl<'a, Fut> Iterator for IterPinRef<'a, Fut> {
130 type Item = Pin<&'a Fut>;
131
132 fn next(&mut self) -> Option<Self::Item> {
133 if self.task.is_null() {
134 return None;
135 }
136
137 unsafe {
138 let future = (*(*self.task).future.get()).as_ref().unwrap();
139
140 let next = (*self.task).spin_next_all(self.pending_next_all, Relaxed);
145 self.task = next;
146 self.len -= 1;
147 Some(Pin::new_unchecked(future))
148 }
149 }
150
151 fn size_hint(&self) -> (usize, Option<usize>) {
152 (self.len, Some(self.len))
153 }
154}
155
156impl<Fut> ExactSizeIterator for IterPinRef<'_, Fut> {}
157
158impl<'a, Fut: Unpin> Iterator for Iter<'a, Fut> {
159 type Item = &'a Fut;
160
161 fn next(&mut self) -> Option<Self::Item> {
162 self.0.next().map(Pin::get_ref)
163 }
164
165 fn size_hint(&self) -> (usize, Option<usize>) {
166 self.0.size_hint()
167 }
168}
169
170impl<Fut: Unpin> ExactSizeIterator for Iter<'_, Fut> {}
171
172unsafe impl<Fut: Sync> Send for IterPinRef<'_, Fut> {}
176unsafe impl<Fut: Sync> Sync for IterPinRef<'_, Fut> {}
177
178unsafe impl<Fut: Send> Send for IterPinMut<'_, Fut> {}
181unsafe impl<Fut: Sync> Sync for IterPinMut<'_, Fut> {}
182
183unsafe impl<Fut: Send + Unpin> Send for IntoIter<Fut> {}
184unsafe impl<Fut: Sync + Unpin> Sync for IntoIter<Fut> {}