Skip to contentSkip to conversation

rust: rbtree: simplify finding `current` in `remove_current`

PATCH Applied Onur Özkan 4 participants 4 messages Jul 8, 2025 – Jul 14, 2025 Rust

lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH v2 1/1 rust: rbtree: simplify finding `current` in `remove_current`

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.

---
 rust/kernel/rbtree.rs | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
rust/kernel/rbtree.rs +7 -16
index 8d978c896747..58ad52a07c92 100644
@@ -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

reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Onur Özkan

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.

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Onur Özkan
Quoted 9 lines · Onur Özkan

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.

Thanks, this is definitely easier to read.

Applied
#3 lore raw
In reply to Onur Özkan
Quoted 10 lines · Onur Özkan

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.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel