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 5/5] rust: rbtree: add `RBTree::entry`
Date: Tue, 20 Aug 2024 08:08:22 +0000	[thread overview]
Message-ID: <57aa157d-ee94-419d-bad3-0bb93f5c2095@proton.me> (raw)
In-Reply-To: <20240819-b4-rbtree-v10-5-3b3b2c4d73af@google.com>

On 19.08.24 17:07, Matt Gilbride wrote:
> From: Alice Ryhl <aliceryhl@google.com>
> 
> This mirrors the entry API [1] from the Rust standard library on
> `RBTree`. This API can be used to access the entry at a specific key and
> make modifications depending on whether the key is vacant or occupied.
> This API is useful because it can often be used to avoid traversing the
> tree multiple times.
> 
> This is used by binder to look up and conditionally access or insert a
> value, depending on whether it is there or not [2].
> 
> Link: https://doc.rust-lang.org/stable/std/collections/btree_map/enum.Entry.html [1]
> Link: https://android-review.googlesource.com/c/kernel/common/+/2849906 [2]
> Signed-off-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>

One nit below, but feel free to add if it doesn't work:

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

> ---
>  rust/kernel/rbtree.rs | 310 ++++++++++++++++++++++++++++++++++++++------------
>  1 file changed, 235 insertions(+), 75 deletions(-)

[...]

> +    /// Gets a mutable reference to the value in the entry.
> +    pub fn get_mut(&mut self) -> &mut V {
> +        // SAFETY:
> +        // - `self.node_links` is a valid pointer to a node in the tree.
> +        // - We have exclusive access to the underlying tree, and can thus give out a mutable reference.
> +        unsafe {
> +            &mut (*(container_of!(self.node_links, Node<K, V>, links) as *mut Node<K, V>)).value

Does `.cast_mut()` work here instead of `as *mut Node<K, V>`? (also
below)

---
Cheers,
Benno

> +        }
> +    }
> +
> +    /// Converts the entry into a mutable reference to its value.
> +    ///
> +    /// If you need multiple references to the `OccupiedEntry`, see [`self#get_mut`].
> +    pub fn into_mut(self) -> &'a mut V {
> +        // SAFETY:
> +        // - `self.node_links` is a valid pointer to a node in the tree.
> +        // - This consumes the `&'a mut RBTree<K, V>`, therefore it can give out a mutable reference that lives for `'a`.
> +        unsafe {
> +            &mut (*(container_of!(self.node_links, Node<K, V>, links) as *mut Node<K, V>)).value
> +        }
> +    }


      reply	other threads:[~2024-08-20  8:08 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
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 [this message]

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=57aa157d-ee94-419d-bad3-0bb93f5c2095@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).