@@ -127,15 +127,22 @@ where
127127 /// This iterator returns futures in an arbitrary order, which may change.
128128 ///
129129 /// If downcasting a future to `T` fails it will be skipped in the iterator.
130- pub fn iter_of_type < T > ( & self ) -> impl Iterator < Item = ( & ID , & T ) >
130+ pub fn iter_of_type < T > ( & self ) -> impl Iterator < Item = ( & ID , Pin < & T > ) >
131131 where
132132 T : ' static ,
133133 {
134134 self . inner . iter ( ) . filter_map ( |a| {
135- let pin = a. inner . inner . as_ref ( ) ;
136- let any = Pin :: into_inner ( pin) as & ( dyn Any + Send ) ;
135+ let pointer = a. inner . inner . as_ref ( ) . get_ref ( ) ;
136+
137+ let any = pointer as & ( dyn Any + Send ) ;
138+ // SAFETY: this returns `None` and drops a `&T`, which is safe because dropping a reference is trivial.
137139 let inner = any. downcast_ref :: < T > ( ) ?;
138- Some ( ( & a. tag , inner) )
140+
141+ // Safety: The pointer is already pinned, and will remain pinned for its entire lifetime,
142+ // because we return a `Pin<&T>`.
143+ let pinned = unsafe { Pin :: new_unchecked ( inner) } ;
144+
145+ Some ( ( & a. tag , pinned) )
139146 } )
140147 }
141148
@@ -144,15 +151,22 @@ where
144151 /// This iterator returns futures in an arbitrary order, which may change.
145152 ///
146153 /// If downcasting a future to `T` fails it will be skipped in the iterator.
147- pub fn iter_mut_of_type < T > ( & mut self ) -> impl Iterator < Item = ( & ID , & mut T ) >
154+ pub fn iter_mut_of_type < T > ( & mut self ) -> impl Iterator < Item = ( & ID , Pin < & mut T > ) >
148155 where
149156 T : ' static ,
150157 {
151158 self . inner . iter_mut ( ) . filter_map ( |a| {
152159 let pin = a. inner . inner . as_mut ( ) ;
153- let any = Pin :: into_inner ( pin) as & mut ( dyn Any + Send ) ;
160+
161+ // Safety: We are only temporarily manipulating the pointer and pinning it again further down.
162+ let pointer = unsafe { Pin :: into_inner_unchecked ( pin) } ;
163+ let any = pointer as & mut ( dyn Any + Send ) ;
154164 let inner = any. downcast_mut :: < T > ( ) ?;
155- Some ( ( & a. tag , inner) )
165+
166+ // Safety: The pointer is already pinned.
167+ let pinned = unsafe { Pin :: new_unchecked ( inner) } ;
168+
169+ Some ( ( & a. tag , pinned) )
156170 } )
157171 }
158172}
@@ -329,7 +343,7 @@ mod tests {
329343 }
330344 assert ! ( !sender. iter( ) . any( |tx| tx. is_canceled( ) ) ) ;
331345
332- for ( _, rx) in futures. iter_mut_of_type :: < oneshot:: Receiver < ( ) > > ( ) {
346+ for ( _, mut rx) in futures. iter_mut_of_type :: < oneshot:: Receiver < ( ) > > ( ) {
333347 rx. close ( ) ;
334348 }
335349 assert ! ( sender. iter( ) . all( |tx| tx. is_canceled( ) ) ) ;
0 commit comments