@@ -18,13 +18,17 @@ pub struct IterMut<'a, Fut: Unpin>(pub(super) IterPinMut<'a, Fut>);
1818
1919#[ derive( Debug ) ]
2020/// Immutable iterator over all futures in the unordered set.
21- pub struct Iter < ' a , Fut : Unpin > {
21+ pub struct IterPinRef < ' a , Fut > {
2222 pub ( super ) task : * const Task < Fut > ,
2323 pub ( super ) len : usize ,
2424 pub ( super ) pending_next_all : * mut Task < Fut > ,
2525 pub ( super ) _marker : PhantomData < & ' a FuturesUnordered < Fut > > ,
2626}
2727
28+ #[ derive( Debug ) ]
29+ /// Immutable iterator over all the futures in the unordered set.
30+ pub struct Iter < ' a , Fut : Unpin > ( pub ( super ) IterPinRef < ' a , Fut > ) ;
31+
2832#[ derive( Debug ) ]
2933/// Owned iterator over all futures in the unordered set.
3034pub struct IntoIter < Fut : Unpin > {
@@ -109,10 +113,10 @@ impl<'a, Fut: Unpin> Iterator for IterMut<'a, Fut> {
109113
110114impl < Fut : Unpin > ExactSizeIterator for IterMut < ' _ , Fut > { }
111115
112- impl < ' a , Fut : Unpin > Iterator for Iter < ' a , Fut > {
113- type Item = & ' a Fut ;
116+ impl < ' a , Fut : Unpin > Iterator for IterPinRef < ' a , Fut > {
117+ type Item = Pin < & ' a Fut > ;
114118
115- fn next ( & mut self ) -> Option < & ' a Fut > {
119+ fn next ( & mut self ) -> Option < Pin < & ' a Fut > > {
116120 if self . task . is_null ( ) {
117121 return None ;
118122 }
@@ -127,7 +131,7 @@ impl<'a, Fut: Unpin> Iterator for Iter<'a, Fut> {
127131 let next = ( * self . task ) . spin_next_all ( self . pending_next_all , Relaxed ) ;
128132 self . task = next;
129133 self . len -= 1 ;
130- Some ( future)
134+ Some ( Pin :: new_unchecked ( future) )
131135 }
132136 }
133137
@@ -136,6 +140,20 @@ impl<'a, Fut: Unpin> Iterator for Iter<'a, Fut> {
136140 }
137141}
138142
143+ impl < Fut : Unpin > ExactSizeIterator for IterPinRef < ' _ , Fut > { }
144+
145+ impl < ' a , Fut : Unpin > Iterator for Iter < ' a , Fut > {
146+ type Item = & ' a Fut ;
147+
148+ fn next ( & mut self ) -> Option < & ' a Fut > {
149+ self . 0 . next ( ) . map ( Pin :: get_ref)
150+ }
151+
152+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
153+ self . 0 . size_hint ( )
154+ }
155+ }
156+
139157impl < Fut : Unpin > ExactSizeIterator for Iter < ' _ , Fut > { }
140158
141159// SAFETY: we do nothing thread-local and there is no interior mutability,
@@ -146,5 +164,5 @@ unsafe impl<Fut: Sync> Sync for IterPinMut<'_, Fut> {}
146164unsafe impl < Fut : Send + Unpin > Send for IntoIter < Fut > { }
147165unsafe impl < Fut : Sync + Unpin > Sync for IntoIter < Fut > { }
148166
149- unsafe impl < Fut : Send + Unpin > Send for Iter < ' _ , Fut > { }
150- unsafe impl < Fut : Sync + Unpin > Sync for Iter < ' _ , Fut > { }
167+ unsafe impl < Fut : Send + Unpin > Send for IterPinRef < ' _ , Fut > { }
168+ unsafe impl < Fut : Sync + Unpin > Sync for IterPinRef < ' _ , Fut > { }
0 commit comments