rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
@ 2025-07-08  7:58 Onur Özkan
  2025-07-08  9:05 ` Alice Ryhl
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Onur Özkan @ 2025-07-08  7:58 UTC (permalink / raw)
  To: rust-for-linux
  Cc: linux-kernel, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, mattgilbride,
	wedsonaf, daniel, tamird, 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`.

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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-08  7:58 [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
@ 2025-07-08  9:05 ` Alice Ryhl
  2025-07-08 12:51 ` Alexandre Courbot
  2025-07-14 23:31 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Alice Ryhl @ 2025-07-08  9:05 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, linux-kernel, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, tmgross, dakr, mattgilbride,
	wedsonaf, daniel, tamird

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>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-08  7:58 [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
  2025-07-08  9:05 ` Alice Ryhl
@ 2025-07-08 12:51 ` Alexandre Courbot
  2025-07-14 23:31 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Alexandre Courbot @ 2025-07-08 12:51 UTC (permalink / raw)
  To: Onur Özkan, rust-for-linux
  Cc: linux-kernel, ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh,
	lossin, a.hindborg, aliceryhl, tmgross, dakr, mattgilbride,
	wedsonaf, daniel, tamird

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>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-08  7:58 [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
  2025-07-08  9:05 ` Alice Ryhl
  2025-07-08 12:51 ` Alexandre Courbot
@ 2025-07-14 23:31 ` Miguel Ojeda
  2 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2025-07-14 23:31 UTC (permalink / raw)
  To: Onur Özkan
  Cc: rust-for-linux, linux-kernel, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, aliceryhl, tmgross, dakr,
	mattgilbride, wedsonaf, daniel, tamird

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-14 23:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08  7:58 [PATCH v2] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
2025-07-08  9:05 ` Alice Ryhl
2025-07-08 12:51 ` Alexandre Courbot
2025-07-14 23:31 ` Miguel Ojeda

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).