Skip to content

btree: use pattern matching instead of unwrap #15727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/libcollections/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ impl<K: Clone + Ord, V: Clone> Leaf<K, V> {
return (Node::new_leaf(self.clone().elts), false);
}
//If there is an index, insert at that index.
_ => {
if index.unwrap() >= self.elts.len() {
Some(i) => {
if i >= self.elts.len() {
self.elts.push(to_insert.clone());
}
else {
self.elts.insert(index.unwrap(), to_insert.clone());
self.elts.insert(i, to_insert.clone());
}
}
}
Expand Down Expand Up @@ -526,16 +526,16 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
self.clone().rightmost_child),
outcome);
}
_ => {
if index.unwrap() == self.elts.len() {
Some(i) => {
if i == self.elts.len() {
let new_outcome = self.clone().rightmost_child.insert(k.clone(),
v.clone(),
ub.clone());
new_branch = new_outcome.clone().val0();
outcome = new_outcome.val1();
}
else {
let new_outcome = self.elts.get(index.unwrap()).left.clone().insert(k.clone(),
let new_outcome = self.elts.get(i).left.clone().insert(k.clone(),
v.clone(),
ub.clone());
new_branch = new_outcome.clone().val0();
Expand All @@ -547,11 +547,11 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
//If we have a leaf, we do not need to resize the tree,
//so we can return false.
LeafNode(..) => {
if index.unwrap() == self.elts.len() {
if i == self.elts.len() {
self.rightmost_child = box new_branch.clone();
}
else {
self.elts.get_mut(index.unwrap()).left = box new_branch.clone();
self.elts.get_mut(i).left = box new_branch.clone();
}
return (Node::new_branch(self.clone().elts,
self.clone().rightmost_child),
Expand Down Expand Up @@ -589,13 +589,13 @@ impl<K: Clone + Ord, V: Clone> Branch<K, V> {
self.clone().rightmost_child),
false);
}
_ => {
self.elts.insert(new_elt_index.unwrap(), new_elt);
if new_elt_index.unwrap() + 1 >= self.elts.len() {
Some(i) => {
self.elts.insert(i, new_elt);
if i + 1 >= self.elts.len() {
self.rightmost_child = branch.clone().rightmost_child;
}
else {
self.elts.get_mut(new_elt_index.unwrap() + 1).left =
self.elts.get_mut(i + 1).left =
branch.clone().rightmost_child;
}
}
Expand Down