Skip to content

Commit

Permalink
some stylistic improvements (#130)
Browse files Browse the repository at this point in the history
replace if-let-else-unreachable with let-else-unreachable to reduce nesting

merge match arms with identical contents
  • Loading branch information
ugur-a authored Nov 19, 2024
1 parent 98435a0 commit b6f21d8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/iter/traverser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ impl<'g, K, V> Iterator for NodeIter<'g, K, V> {
BinEntry::TreeNode(ref tree_node) => {
e = Some(&tree_node.node);
}
BinEntry::Moved => unreachable!("Nodes can only point to Nodes or TreeNodes"),
BinEntry::Tree(_) => unreachable!("Nodes can only point to Nodes or TreeNodes"),
BinEntry::Moved | BinEntry::Tree(_) => {
unreachable!("Nodes can only point to Nodes or TreeNodes")
}
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1720,11 +1720,10 @@ where
Err(changed) => {
assert!(!changed.current.is_null());
bin = changed.current;
if let BinEntry::Node(node) = unsafe { changed.new.into_box() }.value {
key = node.key;
} else {
let BinEntry::Node(node) = unsafe { changed.new.into_box() }.value else {
unreachable!("we declared node and it is a BinEntry::Node");
}
};
key = node.key;
}
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,11 +949,10 @@ impl<K, V> TreeBin<K, V> {
Box::from_raw(ptr).value
};

if let BinEntry::Tree(mut tree_bin) = bin {
tree_bin.drop_fields(false);
} else {
let BinEntry::Tree(mut tree_bin) = bin else {
unreachable!("bin is a tree bin");
}
};
tree_bin.drop_fields(false);
});
}

Expand Down Expand Up @@ -988,16 +987,15 @@ impl<K, V> TreeBin<K, V> {
) {
let mut p = from;
while !p.is_null() {
if let BinEntry::TreeNode(tree_node) = p.into_box().value {
// if specified, drop the value in this node
if drop_values {
let _ = tree_node.node.value.into_box();
}
// then we move to the next node
p = tree_node.node.next.load(Ordering::SeqCst, guard);
} else {
let BinEntry::TreeNode(tree_node) = p.into_box().value else {
unreachable!("Trees can only ever contain TreeNodes");
};
// if specified, drop the value in this node
if drop_values {
let _ = tree_node.node.value.into_box();
}
// then we move to the next node
p = tree_node.node.next.load(Ordering::SeqCst, guard);
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ impl<K, V> Table<K, V> {
BinEntry::Node(_) => {
let mut node = bin;
loop {
let n = if let BinEntry::Node(ref n) = **node {
n
} else {
let BinEntry::Node(ref n) = **node else {
unreachable!("BinEntry::Node only points to BinEntry::Node");
};

Expand Down Expand Up @@ -209,9 +207,7 @@ impl<K, V> Table<K, V> {
// we replaced the bin with a NULL, so there's no future way to access it
// either; we own all the nodes in the list.

let node = if let BinEntry::Node(node) = p.value {
node
} else {
let BinEntry::Node(node) = p.value else {
unreachable!();
};

Expand All @@ -228,10 +224,8 @@ impl<K, V> Table<K, V> {
BinEntry::Tree(_) => {
// safety: same as for BinEntry::Node
let p = unsafe { bin.into_box() };
let bin = if let BinEntry::Tree(bin) = p.value {
bin
} else {
unreachable!();
let BinEntry::Tree(bin) = p.value else {
unreachable!()
};
// TreeBin::drop will take care of freeing the contained TreeNodes and their values
drop(bin);
Expand Down

0 comments on commit b6f21d8

Please sign in to comment.