Commit b6f88506 authored by Onur Özkan's avatar Onur Özkan Committed by Miguel Ojeda
Browse files

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

Signed-off-by: default avatarOnur Özkan <work@onurozkan.dev>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Reviewed-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
Link: https://lore.kernel.org/r/20250708075850.25789-1-work@onurozkan.dev


Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
parent 8ffb9456
Loading
Loading
Loading
Loading
+7 −16
Original line number Diff line number Diff line
@@ -775,23 +775,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`.
            Some(Self {
        let cursor = next.or(prev).map(|current| Self {
            current,
            tree: self.tree,
            }),
            node,
        )
        });

        (cursor, node)
    }

    /// Remove the previous node, returning it if it exists.