rust: rbtree: simplify finding `current` in `remove_current`
Onur Özkan
reviewerPATCH v2 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 | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
rust/kernel/rbtree.rs +7 -16
@@ -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
Quoted 10 lines · Onur Özkan
The previous version used a verbose
matchto getcurrent, which may be slightly confusing at first glance.This change makes it shorter and more clearly expresses the intent: prefer
nextif available, otherwise fall back toprev.Signed-off-by Onur Özkan <[email protected]>
Quoted 9 lines · Onur Özkan
The previous version used a verbose
matchto getcurrent, which may be slightly confusing at first glance.This change makes it shorter and more clearly expresses the intent: prefer
nextif available, otherwise fall back toprev.Signed-off-by Onur Özkan <[email protected]>
Thanks, this is definitely easier to read.
Miguel Ojeda
AppliedQuoted 10 lines · Onur Özkan
The previous version used a verbose
matchto getcurrent, which may be slightly confusing at first glance.This change makes it shorter and more clearly expresses the intent: prefer
nextif available, otherwise fall back toprev.Signed-off-by Onur Özkan <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel