Skip to content

Commit 7a67744

Browse files
MajorBreakfastcramertj
authored andcommitted
Implement IterMut in terms of IterPinMut
1 parent f0604c2 commit 7a67744

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

futures-util/src/stream/futures_unordered/iter_mut.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
use std::marker::{PhantomData, Unpin};
2-
use super::FuturesUnordered;
3-
use super::node::Node;
1+
use std::marker::Unpin;
2+
use std::mem::PinMut;
3+
4+
use super::iter_pin_mut::IterPinMut;
45

56
#[derive(Debug)]
67
/// Mutable iterator over all futures in the unordered set.
7-
pub struct IterMut<'a, F: 'a + Unpin> {
8-
pub(super) node: *const Node<F>,
9-
pub(super) len: usize,
10-
pub(super) _marker: PhantomData<&'a mut FuturesUnordered<F>>
11-
}
8+
pub struct IterMut<'a, F: 'a + Unpin> (pub(super) IterPinMut<'a, F>);
129

1310
impl<'a, F: Unpin> Iterator for IterMut<'a, F> {
1411
type Item = &'a mut F;
1512

1613
fn next(&mut self) -> Option<&'a mut F> {
17-
if self.node.is_null() {
18-
return None;
19-
}
20-
unsafe {
21-
let future = (*(*self.node).future.get()).as_mut().unwrap();
22-
let next = *(*self.node).next_all.get();
23-
self.node = next;
24-
self.len -= 1;
25-
Some(future)
26-
}
14+
self.0.next().map(|f| unsafe { PinMut::get_mut(f) })
2715
}
2816

2917
fn size_hint(&self) -> (usize, Option<usize>) {
30-
(self.len, Some(self.len))
18+
self.0.size_hint()
3119
}
3220
}
3321

futures-util/src/stream/futures_unordered/iter_pin_mut.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::marker::PhantomData;
1+
use std::marker::{PhantomData, Unpin};
22
use std::mem::PinMut;
33

44
use super::FuturesUnordered;
@@ -12,6 +12,8 @@ pub struct IterPinMut<'a, F: 'a> {
1212
pub(super) _marker: PhantomData<&'a mut FuturesUnordered<F>>
1313
}
1414

15+
impl<'a, F: Unpin> Unpin for IterPinMut<'a, F> {}
16+
1517
impl<'a, F> Iterator for IterPinMut<'a, F> {
1618
type Item = PinMut<'a, F>;
1719

futures-util/src/stream/futures_unordered/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ impl<T> FuturesUnordered<T> {
150150

151151
/// Returns an iterator that allows modifying each future in the set.
152152
pub fn iter_mut(&mut self) -> IterMut<T> where T: Unpin {
153-
IterMut {
153+
IterMut(IterPinMut {
154154
node: self.head_all,
155155
len: self.len,
156156
_marker: PhantomData
157-
}
157+
})
158158
}
159159

160160
/// Returns an iterator that allows modifying each future in the set.

0 commit comments

Comments
 (0)