@@ -197,8 +197,26 @@ impl<K, V> RBTree<K, V> {
197
197
// INVARIANT: `bindings::rb_first` returns a valid pointer to a tree node given a valid pointer to a tree root.
198
198
Iter {
199
199
_tree : PhantomData ,
200
- // SAFETY: `self.root` is a valid pointer to the tree root.
201
- next : unsafe { bindings:: rb_first ( & self . root ) } ,
200
+ iter_raw : IterRaw {
201
+ // SAFETY: by the invariants, all pointers are valid.
202
+ next : unsafe { bindings:: rb_first ( & self . root ) } ,
203
+ _phantom : PhantomData ,
204
+ } ,
205
+ }
206
+ }
207
+
208
+ /// Returns a mutable iterator over the tree nodes, sorted by key.
209
+ pub fn iter_mut ( & mut self ) -> IterMut < ' _ , K , V > {
210
+ IterMut {
211
+ _tree : PhantomData ,
212
+ // INVARIANT:
213
+ // - `self.root` is a valid pointer to a tree root.
214
+ // - `bindings::rb_first` produces a valid pointer to a node given `root` is valid.
215
+ iter_raw : IterRaw {
216
+ // SAFETY: by the invariants, all pointers are valid.
217
+ next : unsafe { bindings:: rb_first ( & self . root ) } ,
218
+ _phantom : PhantomData ,
219
+ } ,
202
220
}
203
221
}
204
222
@@ -211,6 +229,11 @@ impl<K, V> RBTree<K, V> {
211
229
pub fn values ( & self ) -> impl Iterator < Item = & ' _ V > {
212
230
self . iter ( ) . map ( |( _, v) | v)
213
231
}
232
+
233
+ /// Returns a mutable iterator over the values of the nodes in the tree, sorted by key.
234
+ pub fn values_mut ( & mut self ) -> impl Iterator < Item = & ' _ mut V > {
235
+ self . iter_mut ( ) . map ( |( _, v) | v)
236
+ }
214
237
}
215
238
216
239
impl < K , V > RBTree < K , V >
@@ -414,13 +437,9 @@ impl<'a, K, V> IntoIterator for &'a RBTree<K, V> {
414
437
/// An iterator over the nodes of a [`RBTree`].
415
438
///
416
439
/// Instances are created by calling [`RBTree::iter`].
417
- ///
418
- /// # Invariants
419
- /// - `self.next` is a valid pointer.
420
- /// - `self.next` points to a node stored inside of a valid `RBTree`.
421
440
pub struct Iter < ' a , K , V > {
422
441
_tree : PhantomData < & ' a RBTree < K , V > > ,
423
- next : * mut bindings :: rb_node ,
442
+ iter_raw : IterRaw < K , V > ,
424
443
}
425
444
426
445
// SAFETY: The [`Iter`] gives out immutable references to K and V, so it has the same
@@ -434,21 +453,76 @@ unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {}
434
453
impl < ' a , K , V > Iterator for Iter < ' a , K , V > {
435
454
type Item = ( & ' a K , & ' a V ) ;
436
455
456
+ fn next ( & mut self ) -> Option < Self :: Item > {
457
+ self . iter_raw . next ( ) . map ( |( k, v) |
458
+ // SAFETY: Due to `self._tree`, `k` and `v` are valid for the lifetime of `'a`.
459
+ unsafe { ( & * k, & * v) } )
460
+ }
461
+ }
462
+
463
+ impl < ' a , K , V > IntoIterator for & ' a mut RBTree < K , V > {
464
+ type Item = ( & ' a K , & ' a mut V ) ;
465
+ type IntoIter = IterMut < ' a , K , V > ;
466
+
467
+ fn into_iter ( self ) -> Self :: IntoIter {
468
+ self . iter_mut ( )
469
+ }
470
+ }
471
+
472
+ /// A mutable iterator over the nodes of a [`RBTree`].
473
+ ///
474
+ /// Instances are created by calling [`RBTree::iter_mut`].
475
+ pub struct IterMut < ' a , K , V > {
476
+ _tree : PhantomData < & ' a mut RBTree < K , V > > ,
477
+ iter_raw : IterRaw < K , V > ,
478
+ }
479
+
480
+ // SAFETY: The [`RBTreeIterator`] gives out mutable references to K and V, so it has the same
481
+ // thread safety requirements as mutable references.
482
+ unsafe impl < ' a , K : Send , V : Send > Send for IterMut < ' a , K , V > { }
483
+
484
+ // SAFETY: The [`RBTreeIterator`] gives out mutable references to K and V, so it has the same
485
+ // thread safety requirements as mutable references.
486
+ unsafe impl < ' a , K : Sync , V : Sync > Sync for IterMut < ' a , K , V > { }
487
+
488
+ impl < ' a , K , V > Iterator for IterMut < ' a , K , V > {
489
+ type Item = ( & ' a K , & ' a mut V ) ;
490
+
491
+ fn next ( & mut self ) -> Option < Self :: Item > {
492
+ self . iter_raw . next ( ) . map ( |( k, v) |
493
+ // SAFETY: Due to `&mut self`, we have exclusive access to `k` and `v`, for the lifetime of `'a`.
494
+ unsafe { ( & * k, & mut * v) } )
495
+ }
496
+ }
497
+
498
+ /// A raw iterator over the nodes of a [`RBTree`].
499
+ ///
500
+ /// # Invariants
501
+ /// - `self.next` is a valid pointer.
502
+ /// - `self.next` points to a node stored inside of a valid `RBTree`.
503
+ struct IterRaw < K , V > {
504
+ next : * mut bindings:: rb_node ,
505
+ _phantom : PhantomData < fn ( ) -> ( K , V ) > ,
506
+ }
507
+
508
+ impl < K , V > Iterator for IterRaw < K , V > {
509
+ type Item = ( * mut K , * mut V ) ;
510
+
437
511
fn next ( & mut self ) -> Option < Self :: Item > {
438
512
if self . next . is_null ( ) {
439
513
return None ;
440
514
}
441
515
442
- // SAFETY: By the type invariant of `Iter `, `self.next` is a valid node in an `RBTree`,
516
+ // SAFETY: By the type invariant of `IterRaw `, `self.next` is a valid node in an `RBTree`,
443
517
// and by the type invariant of `RBTree`, all nodes point to the links field of `Node<K, V>` objects.
444
- let cur = unsafe { container_of ! ( self . next, Node <K , V >, links) } ;
518
+ let cur: * mut Node < K , V > =
519
+ unsafe { container_of ! ( self . next, Node <K , V >, links) } . cast_mut ( ) ;
445
520
446
521
// SAFETY: `self.next` is a valid tree node by the type invariants.
447
522
self . next = unsafe { bindings:: rb_next ( self . next ) } ;
448
523
449
- // SAFETY: By the same reasoning above, it is safe to dereference the node. Additionally,
450
- // it is ok to return a reference to members because the iterator must outlive it.
451
- Some ( unsafe { ( & ( * cur) . key , & ( * cur) . value ) } )
524
+ // SAFETY: By the same reasoning above, it is safe to dereference the node.
525
+ Some ( unsafe { ( addr_of_mut ! ( ( * cur) . key) , addr_of_mut ! ( ( * cur) . value) ) } )
452
526
}
453
527
}
454
528
0 commit comments