Re: [PATCH v4 4/6] rust: rbtree: add mutable iterator

From: Boqun Feng
Date: Mon Jun 03 2024 - 14:26:16 EST


On Mon, Jun 03, 2024 at 07:52:36PM +0200, Alice Ryhl wrote:
> On Mon, Jun 3, 2024 at 7:42 PM Boqun Feng <boqun.feng@xxxxxxxxx> wrote:
> >
> > On Mon, Jun 03, 2024 at 04:05:19PM +0000, Matt Gilbride wrote:
> > [...]
> > > +/// A mutable iterator over the nodes of a [`RBTree`].
> > > +///
> > > +/// Instances are created by calling [`RBTree::iter_mut`].
> > > +pub struct IterMut<'a, K, V> {
> > > + _tree: PhantomData<&'a mut RBTree<K, V>>,
> > > + iter_raw: IterRaw<K, V>,
> > > +}
> > > +
> > > +// SAFETY: The [`RBTreeIterator`] gives out mutable references to K and V, so it has the same
> >
> > s/RBTreeIterator/IterMut ?
> >
> > Also `IterMut` doesn't give out mutable references to K, which makes
> > me think...
> >
> > > +// thread safety requirements as mutable references.
> > > +unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
> > > +
> >
> > we can lose the constrains to `K: Sync`, right?
>
> Either Send or Sync would be valid here, but almost all types that are
> Sync are also Send, but the opposite is not the case. So I wouldn't
> consider that to be loosening the constrain.
>

You are probably right. But it's a bit pity that we miss the `Sync` +
`!Send` case :-/

Regards,
Boqun

> Alice