rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: "Matt Gilbride" <mattgilbride@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Christian Brauner" <brauner@kernel.org>
Cc: Rob Landley <rob@landley.net>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Michel Lespinasse <michel@lespinasse.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v10 3/5] rust: rbtree: add mutable iterator
Date: Tue, 20 Aug 2024 07:35:37 +0000	[thread overview]
Message-ID: <dce117a2-3e07-4fd6-a2cf-e06db8306249@proton.me> (raw)
In-Reply-To: <20240819-b4-rbtree-v10-3-3b3b2c4d73af@google.com>

On 19.08.24 17:07, Matt Gilbride wrote:
> From: Wedson Almeida Filho <wedsonaf@gmail.com>
> 
> Add mutable Iterator implementation for `RBTree`,
> allowing iteration over (key, value) pairs in key order. Only values are
> mutable, as mutating keys implies modifying a node's position in the tree.
> 
> Mutable iteration is used by the binder driver during shutdown to
> clean up the tree maintained by the "range allocator" [1].
> 
> Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-6-08ba9197f637@google.com/ [1]
> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Tested-by: Alice Ryhl <aliceryhl@google.com>
> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
> Signed-off-by: Matt Gilbride <mattgilbride@google.com>
> ---

I got one nit below, but it already looks good:

Reviewed-by: Benno Lossin <benno.lossin@proton.me>

>  rust/kernel/rbtree.rs | 104 +++++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 90 insertions(+), 14 deletions(-)

[...]

> +impl<K, V> Iterator for IterRaw<K, V> {
> +    type Item = (*mut K, *mut V);
> +
>      fn next(&mut self) -> Option<Self::Item> {
>          if self.next.is_null() {
>              return None;
>          }
> 
> -        // SAFETY: By the type invariant of `Iter`, `self.next` is a valid node in an `RBTree`,
> +        // SAFETY: By the type invariant of `IterRaw`, `self.next` is a valid node in an `RBTree`,
>          // and by the type invariant of `RBTree`, all nodes point to the links field of `Node<K, V>` objects.
> -        let cur = unsafe { container_of!(self.next, Node<K, V>, links) };
> +        let cur: *mut Node<K, V> =

Do you need to specify this type? If not then the line would fit on one.

---
Cheers,
Benno

> +            unsafe { container_of!(self.next, Node<K, V>, links) }.cast_mut();
> 
>          // SAFETY: `self.next` is a valid tree node by the type invariants.
>          self.next = unsafe { bindings::rb_next(self.next) };
> 
> -        // SAFETY: By the same reasoning above, it is safe to dereference the node. Additionally,
> -        // it is ok to return a reference to members because the iterator must outlive it.
> -        Some(unsafe { (&(*cur).key, &(*cur).value) })
> +        // SAFETY: By the same reasoning above, it is safe to dereference the node.
> +        Some(unsafe { (addr_of_mut!((*cur).key), addr_of_mut!((*cur).value)) })
>      }
>  }
> 
> 
> --
> 2.46.0.184.g6999bdac58-goog
> 


  reply	other threads:[~2024-08-20  7:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-19 15:07 [PATCH v10 0/5] Red-black tree abstraction needed by Rust Binder Matt Gilbride
2024-08-19 15:07 ` [PATCH v10 1/5] rust: rbtree: add red-black tree implementation backed by the C version Matt Gilbride
2024-08-19 15:07 ` [PATCH v10 2/5] rust: rbtree: add iterator Matt Gilbride
2024-08-19 15:07 ` [PATCH v10 3/5] rust: rbtree: add mutable iterator Matt Gilbride
2024-08-20  7:35   ` Benno Lossin [this message]
2024-08-19 15:07 ` [PATCH v10 4/5] rust: rbtree: add cursor Matt Gilbride
2024-08-20  8:00   ` Benno Lossin
2024-08-19 15:07 ` [PATCH v10 5/5] rust: rbtree: add `RBTree::entry` Matt Gilbride
2024-08-20  8:08   ` Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dce117a2-3e07-4fd6-a2cf-e06db8306249@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=arve@android.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dave@stgolabs.net \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=mattgilbride@google.com \
    --cc=michel@lespinasse.org \
    --cc=ojeda@kernel.org \
    --cc=rob@landley.net \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tkjos@android.com \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).