[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <57aa157d-ee94-419d-bad3-0bb93f5c2095@proton.me>
Date: Tue, 20 Aug 2024 08:08:22 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Matt Gilbride <mattgilbride@...gle.com>, Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>, Wedson Almeida Filho <wedsonaf@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Andreas Hindborg <a.hindborg@...sung.com>, Alice Ryhl <aliceryhl@...gle.com>, Greg Kroah-Hartman <gregkh@...uxfoundation.org>, Arve Hjønnevåg <arve@...roid.com>, Todd Kjos <tkjos@...roid.com>, Martijn Coenen <maco@...roid.com>, Joel Fernandes <joel@...lfernandes.org>, Carlos Llamas <cmllamas@...gle.com>, Suren Baghdasaryan <surenb@...gle.com>, Christian Brauner <brauner@...nel.org>
Cc: Rob Landley <rob@...dley.net>, Davidlohr Bueso <dave@...olabs.net>, Michel Lespinasse <michel@...pinasse.org>, rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v10 5/5] rust: rbtree: add `RBTree::entry`
On 19.08.24 17:07, Matt Gilbride wrote:
> From: Alice Ryhl <aliceryhl@...gle.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@...gle.com>
> Tested-by: Alice Ryhl <aliceryhl@...gle.com>
> Reviewed-by: Boqun Feng <boqun.feng@...il.com>
> Signed-off-by: Matt Gilbride <mattgilbride@...gle.com>
One nit below, but feel free to add if it doesn't work:
Reviewed-by: Benno Lossin <benno.lossin@...ton.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
> + }
> + }
Powered by blists - more mailing lists