[PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`

Onur Özkan posted 1 patch 3 months ago
rust/kernel/rbtree.rs | 23 +++++++----------------
1 file changed, 7 insertions(+), 16 deletions(-)
[PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
Posted by Onur Özkan 3 months ago
The previous version used a verbose `match` to get
`current`, which may be slightly confusing at first
glance.

This change makes it shorter and more clearly expresses
the intent: prefer `next` if available, otherwise fall
back to `prev`.

Signed-off-by: Onur Özkan <work@onurozkan.dev>
---
 rust/kernel/rbtree.rs | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 8d978c896747..58ad52a07c92 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -769,23 +769,14 @@ pub fn remove_current(self) -> (Option<Self>, RBTreeNode<K, V>) {
         // the tree cannot change. By the tree invariant, all nodes are valid.
         unsafe { bindings::rb_erase(&mut (*this).links, addr_of_mut!(self.tree.root)) };

-        let current = match (prev, next) {
-            (_, Some(next)) => next,
-            (Some(prev), None) => prev,
-            (None, None) => {
-                return (None, node);
-            }
-        };
+        // INVARIANT:
+        // - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
+        let cursor = next.or(prev).map(|current| Self {
+            current,
+            tree: self.tree,
+        });

-        (
-            // INVARIANT:
-            // - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
-            Some(Self {
-                current,
-                tree: self.tree,
-            }),
-            node,
-        )
+        (cursor, node)
     }

     /// Remove the previous node, returning it if it exists.
--
2.50.0

Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
Posted by Miguel Ojeda 2 months, 3 weeks ago
On Tue, Jul 8, 2025 at 9:59 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> The previous version used a verbose `match` to get
> `current`, which may be slightly confusing at first
> glance.
>
> This change makes it shorter and more clearly expresses
> the intent: prefer `next` if available, otherwise fall
> back to `prev`.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel
Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
Posted by Alexandre Courbot 3 months ago
On Tue Jul 8, 2025 at 4:58 PM JST, Onur Özkan wrote:
> The previous version used a verbose `match` to get
> `current`, which may be slightly confusing at first
> glance.
>
> This change makes it shorter and more clearly expresses
> the intent: prefer `next` if available, otherwise fall
> back to `prev`.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

Thanks, this is definitely easier to read.

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
Posted by Alice Ryhl 3 months ago
On Tue, Jul 8, 2025 at 9:59 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> The previous version used a verbose `match` to get
> `current`, which may be slightly confusing at first
> glance.
>
> This change makes it shorter and more clearly expresses
> the intent: prefer `next` if available, otherwise fall
> back to `prev`.
>
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>