Skip to content

Commit 0847afc

Browse files
committed
Make delete() not use unwrap, eliminate clones
1 parent fbf1859 commit 0847afc

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/util/counter.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
4343
.map(|f| f.to_owned())
4444
.collect();
4545
counts.sort_unstable();
46-
// panic!("{:?}", counts);
4746

4847
// Get the value under each key
4948
for count in counts.iter().rev() {
@@ -61,13 +60,13 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
6160
}
6261

6362
/// Remove an item from the internal order store
64-
fn purge_from_order(&mut self, item: &T, count: &u64) {
65-
if let Some(order) = self.order.get_mut(count) {
63+
fn purge_from_order(&mut self, item: &T, count: u64) {
64+
if let Some(order) = self.order.get_mut(&count) {
6665
// If there was data there, remove the existing item
6766
if !order.is_empty() {
6867
order.retain(|i| i != item);
6968
if order.is_empty() {
70-
self.order.remove(count);
69+
self.order.remove(&count);
7170
}
7271
};
7372
};
@@ -79,7 +78,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
7978

8079
/// Update the internal item order HashMap
8180
fn update_order(&mut self, item: T, old_count: &u64, new_count: &u64) {
82-
self.purge_from_order(&item, old_count);
81+
self.purge_from_order(&item, *old_count);
8382
match self.order.get_mut(new_count) {
8483
Some(v) => {
8584
v.push(item);
@@ -92,7 +91,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
9291

9392
/// Increment an item into the counter, creating if it does not exist
9493
fn increment(&mut self, item: T) {
95-
let old_count = self.state.get(&item).unwrap_or(&0).to_owned();
94+
let old_count = *self.state.get(&item).unwrap_or(&0);
9695
let new_count = old_count.checked_add(1);
9796
match new_count {
9897
Some(count) => self.state.insert(item.to_owned(), count),
@@ -103,7 +102,7 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
103102

104103
/// Reduce an item from the counter, removing if it becomes 0
105104
fn decrement(&mut self, item: T) {
106-
let old_count = self.state.get(&item).unwrap_or(&0).to_owned();
105+
let old_count = *self.state.get(&item).unwrap_or(&0);
107106
let new_count = old_count.checked_sub(1);
108107
match new_count {
109108
Some(count) => {
@@ -122,10 +121,10 @@ impl<T: Hash + Eq + PartialEq + Clone + Display> Counter<T> {
122121

123122
/// Remove an item from the counter completely
124123
fn delete(&mut self, item: &T) {
125-
// TODO: Make this safe
126-
let count = self.state.get(item).unwrap().to_owned();
127-
self.purge_from_order(item, &count);
128-
self.purge_from_state(item);
124+
if let Some(count) = self.state.get(item) {
125+
self.purge_from_order(item, *count);
126+
self.purge_from_state(item);
127+
}
129128
}
130129
}
131130

0 commit comments

Comments
 (0)