Re: [PATCH] rust: rbtree: simplify finding `current` in `remove_current`
From: Alice Ryhl
Date: Fri Jul 04 2025 - 03:36:40 EST
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@xxxxxxxxxxxxx>
> ---
> 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