Skip to content

Commit d1a50a2

Browse files
committed
handle empty in retain
1 parent 463dbb7 commit d1a50a2

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ impl<T> PacketBuf<T> {
113113
}
114114

115115
pub fn retain(&mut self, f: impl FnMut(u64, &mut T) -> bool) {
116+
if self.is_empty() {
117+
return;
118+
}
116119
let mut f = f;
117120
let base = base(self.min, self.max);
118121
for i in self.min..self.max {
@@ -571,38 +574,38 @@ mod tests {
571574
}
572575

573576
#[derive(Debug, Clone)]
574-
enum Op {
577+
enum InsertRemoveGetOp {
575578
Insert(u64, u64),
576579
Remove(u64),
577580
Get(u64),
578581
}
579582

580-
fn op_strategy() -> impl Strategy<Value = Op> {
583+
fn op_strategy() -> impl Strategy<Value = InsertRemoveGetOp> {
581584
prop_oneof![
582-
(0..1000u64, any::<u64>()).prop_map(|(k, v)| Op::Insert(k, v)),
583-
(0..1000u64).prop_map(Op::Remove),
584-
(0..1000u64).prop_map(Op::Get),
585+
(0..1000u64, any::<u64>()).prop_map(|(k, v)| InsertRemoveGetOp::Insert(k, v)),
586+
(0..1000u64).prop_map(InsertRemoveGetOp::Remove),
587+
(0..1000u64).prop_map(InsertRemoveGetOp::Get),
585588
]
586589
}
587590

588591
proptest! {
589592
#[test]
590-
fn test_matches_btreemap(ops in prop::collection::vec(op_strategy(), 0..1000)) {
593+
fn test_insert_remove_get(ops in prop::collection::vec(op_strategy(), 0..1000)) {
591594
let mut pb = PacketBuf::default();
592595
let mut reference = BTreeMap::new();
593596

594597
for op in ops {
595598
match op {
596-
Op::Insert(k, v) => {
599+
InsertRemoveGetOp::Insert(k, v) => {
597600
pb.insert(k, v);
598601
reference.insert(k, v);
599602
}
600-
Op::Remove(k) => {
603+
InsertRemoveGetOp::Remove(k) => {
601604
let v1 = pb.remove(k);
602605
let v2 = reference.remove(&k);
603606
assert_eq!(v1, v2);
604607
}
605-
Op::Get(k) => {
608+
InsertRemoveGetOp::Get(k) => {
606609
let v1 = pb.get(k);
607610
let v2 = reference.get(&k);
608611
assert_eq!(v1, v2);

0 commit comments

Comments
 (0)