Skip to contentSkip to conversation

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

PATCH Superseded Onur Özkan 3 participants 4 messages Jul 4, 2025 Rust

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

PATCH 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 | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
rust/kernel/rbtree.rs +3 -11
index 8d978c896747..8f1052552132 100644
@@ -769,18 +769,10 @@ 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`.
-            Some(Self {
+            next.or(prev).map(|current| Self {
+                // INVARIANT:
+                // - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
                 current,
                 tree: self.tree,
             }),

--
2.50.0

reviewer
#1 lore raw
Quoted 29 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.

---
 rust/kernel/rbtree.rs | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
rust/kernel/rbtree.rs +0 -8
index 8d978c896747..8f1052552132 100644
@@ -769,18 +769,10 @@ 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);
-            }
-        };
-

rust/kernel/rbtree.rs pub fn remove_current(self) -> (Option<Self>, RBTreeNode<K, V>) { · line 778

Quoted 10 lines
rust/kernel/rbtree.rs +3 -3
         (
-            // INVARIANT:
-            // - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
-            Some(Self {
+            next.or(prev).map(|current| Self {
+                // INVARIANT:
+                // - `current` is a valid node in the [`RBTree`] pointed to by `self.tree`.
                 current,
                 tree: self.tree,
             }),

I'm okay with this change, but the INVARIANT: comment usually goes before the StructName { declaration rather than on the field. For example, what about this?

	// 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,
	});
	
	(cursor, node)

Alice

author reviewer
#2 lore raw
Quoted 47 lines · On Fri, 4 Jul 2025 07:36:16 +0000 Alice Ryhl <[email protected]> wrote:
Quoted 39 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.

---
 rust/kernel/rbtree.rs | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
rust/kernel/rbtree.rs +0 -0
index 8d978c896747..8f1052552132 100644
@@ -769,18 +769,10 @@ 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)) };

rust/kernel/rbtree.rs +0 -10
-        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.

rust/kernel/rbtree.rs +3 -1
-            Some(Self {
+            next.or(prev).map(|current| Self {
+                // INVARIANT:
+                // - `current` is a valid node in the [`RBTree`]

pointed to by self.tree. current,

Quoted 2 lines
                 tree: self.tree,
             }),

I'm okay with this change, but the INVARIANT: comment usually goes before the StructName { declaration rather than on the field. For example, what about this?

Quoted 8 lines
	// 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,
	});
	
	(cursor, node)

Looks nice. Do you want me to send v2 right away, or wait couple of days to give sometime to other reviewers?

Regards,
Onur

#3 lore raw
Quoted 3 lines · Onur

Looks nice. Do you want me to send v2 right away, or wait couple of days to give sometime to other reviewers?

Unless there is a particular reason (e.g. it needs to land quickly), in general it is best to wait at least a few days.

Cheers,
Miguel