rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: rbtree: simplify finding `current` in `remove_current`
@ 2025-07-04  5:45 Onur Özkan
  2025-07-04  7:36 ` Alice Ryhl
  0 siblings, 1 reply; 4+ messages in thread
From: Onur Özkan @ 2025-07-04  5:45 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 | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 8d978c896747..8f1052552132 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -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


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

* Re: [PATCH] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-04  5:45 [PATCH] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
@ 2025-07-04  7:36 ` Alice Ryhl
  2025-07-04 15:14   ` Onur
  0 siblings, 1 reply; 4+ messages in thread
From: Alice Ryhl @ 2025-07-04  7:36 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 Fri, Jul 04, 2025 at 08:45:39AM +0300, 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>
> ---
>  rust/kernel/rbtree.rs | 14 +++-----------
>  1 file changed, 3 insertions(+), 11 deletions(-)
> 
> diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
> index 8d978c896747..8f1052552132 100644
> --- a/rust/kernel/rbtree.rs
> +++ b/rust/kernel/rbtree.rs
> @@ -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,
>              }),

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

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

* Re: [PATCH] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-04  7:36 ` Alice Ryhl
@ 2025-07-04 15:14   ` Onur
  2025-07-04 22:15     ` Miguel Ojeda
  0 siblings, 1 reply; 4+ messages in thread
From: Onur @ 2025-07-04 15:14 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: rust-for-linux, linux-kernel, ojeda, alex.gaynor, boqun.feng,
	gary, bjorn3_gh, lossin, a.hindborg, tmgross, dakr, mattgilbride,
	wedsonaf, daniel, tamird

On Fri, 4 Jul 2025 07:36:16 +0000
Alice Ryhl <aliceryhl@google.com> wrote:

> On Fri, Jul 04, 2025 at 08:45:39AM +0300, 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>
> > ---
> >  rust/kernel/rbtree.rs | 14 +++-----------
> >  1 file changed, 3 insertions(+), 11 deletions(-)
> > 
> > diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
> > index 8d978c896747..8f1052552132 100644
> > --- a/rust/kernel/rbtree.rs
> > +++ b/rust/kernel/rbtree.rs
> > @@ -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,
> >              }),
> 
> 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)

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

Regards,
Onur

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

* Re: [PATCH] rust: rbtree: simplify finding `current` in `remove_current`
  2025-07-04 15:14   ` Onur
@ 2025-07-04 22:15     ` Miguel Ojeda
  0 siblings, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2025-07-04 22:15 UTC (permalink / raw)
  To: Onur
  Cc: Alice Ryhl, rust-for-linux, linux-kernel, ojeda, alex.gaynor,
	boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, tmgross, dakr,
	mattgilbride, wedsonaf, daniel, tamird

On Fri, Jul 4, 2025 at 5:14 PM Onur <work@onurozkan.dev> wrote:
>
> 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

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

end of thread, other threads:[~2025-07-04 22:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-04  5:45 [PATCH] rust: rbtree: simplify finding `current` in `remove_current` Onur Özkan
2025-07-04  7:36 ` Alice Ryhl
2025-07-04 15:14   ` Onur
2025-07-04 22:15     ` 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).