diff --git a/src/btree.rs b/src/btree.rs index 2267f2f..13a079c 100644 --- a/src/btree.rs +++ b/src/btree.rs @@ -56,9 +56,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// assert_eq!(bimap.len(), 3); /// ``` pub fn len(&self) -> usize { @@ -75,7 +75,7 @@ where /// /// let mut bimap = BiBTreeMap::new(); /// assert!(bimap.is_empty()); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(!bimap.is_empty()); /// bimap.remove_by_right(&1); /// assert!(bimap.is_empty()); @@ -92,9 +92,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// bimap.clear(); /// assert!(bimap.len() == 0); /// ``` @@ -114,9 +114,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for (left, right) in bimap.iter() { /// println!("({}, {})", left, right); @@ -139,9 +139,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for char_value in bimap.left_values() { /// println!("{}", char_value); @@ -164,9 +164,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for int_value in bimap.right_values() { /// println!("{}", int_value); @@ -191,7 +191,7 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert_eq!(bimap.get_by_left(&'a'), Some(&1)); /// assert_eq!(bimap.get_by_left(&'z'), None); /// ``` @@ -216,7 +216,7 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert_eq!(bimap.get_by_right(&1), Some(&'a')); /// assert_eq!(bimap.get_by_right(&2), None); /// ``` @@ -241,7 +241,7 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(bimap.contains_left(&'a')); /// assert!(!bimap.contains_left(&'b')); /// ``` @@ -266,7 +266,7 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(bimap.contains_right(&1)); /// assert!(!bimap.contains_right(&2)); /// ``` @@ -293,9 +293,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// assert_eq!(bimap.remove_by_left(&'b'), Some(('b', 2))); /// assert_eq!(bimap.remove_by_left(&'b'), None); @@ -331,9 +331,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// assert_eq!(bimap.remove_by_right(&2), Some(('b', 2))); /// assert_eq!(bimap.remove_by_right(&2), None); @@ -365,9 +365,9 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// bimap.retain(|&l, _| l != 'b'); /// assert_eq!(bimap.len(), 2); @@ -509,10 +509,10 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); - /// bimap.insert('d', 4); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); + /// bimap.insert('d', 4).expect_neither(); /// /// for (left, right) in bimap.left_range('b'..'d') { /// println!("({}, {})", left, right); @@ -546,7 +546,7 @@ where /// use bimap::BiBTreeMap; /// /// let mut bimap = BiBTreeMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// bimap.insert('b', 2); /// bimap.insert('c', 3); /// bimap.insert('d', 4); @@ -641,7 +641,7 @@ where { let mut bimap = BiBTreeMap::new(); for (left, right) in iter { - bimap.insert(left, right); + let _ = bimap.insert(left, right); } bimap } @@ -682,7 +682,7 @@ where { fn extend>(&mut self, iter: T) { iter.into_iter().for_each(move |(l, r)| { - self.insert(l, r); + let _ = self.insert(l, r); }); } } @@ -952,8 +952,8 @@ mod tests { #[test] fn clone() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); let bimap2 = bimap.clone(); assert_eq!(bimap, bimap2); } @@ -961,13 +961,13 @@ mod tests { #[test] fn deep_clone() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); let mut bimap2 = bimap.clone(); // would panic if clone() didn't deep clone - bimap.insert('b', 5); - bimap2.insert('a', 12); + assert!(bimap.insert('b', 5).did_overwrite()); + assert!(bimap2.insert('a', 12).did_overwrite()); bimap2.remove_by_left(&'a'); bimap.remove_by_right(&2); } @@ -977,10 +977,10 @@ mod tests { let mut bimap = BiBTreeMap::new(); assert_eq!("{}", format!("{:?}", bimap)); - bimap.insert('a', 1); + bimap.insert('a', 1).expect_neither(); assert_eq!("{'a' <> 1}", format!("{:?}", bimap)); - bimap.insert('b', 2); + bimap.insert('b', 2).expect_neither(); assert_eq!("{'a' <> 1, 'b' <> 2}", format!("{:?}", bimap)); } @@ -993,18 +993,18 @@ mod tests { fn eq() { let mut bimap = BiBTreeMap::new(); assert_eq!(bimap, bimap); - bimap.insert('a', 1); + bimap.insert('a', 1).expect_neither(); assert_eq!(bimap, bimap); - bimap.insert('b', 2); + bimap.insert('b', 2).expect_neither(); assert_eq!(bimap, bimap); let mut bimap2 = BiBTreeMap::new(); assert_ne!(bimap, bimap2); - bimap2.insert('a', 1); + bimap2.insert('a', 1).expect_neither(); assert_ne!(bimap, bimap2); - bimap2.insert('b', 2); + bimap2.insert('b', 2).expect_neither(); assert_eq!(bimap, bimap2); - bimap2.insert('c', 3); + bimap2.insert('c', 3).expect_neither(); assert_ne!(bimap, bimap2); } @@ -1019,17 +1019,17 @@ mod tests { ('b', 3), ]); let mut bimap2 = BiBTreeMap::new(); - bimap2.insert('a', 4); - bimap2.insert('b', 3); + bimap2.insert('a', 4).expect_neither(); + bimap2.insert('b', 3).expect_neither(); assert_eq!(bimap, bimap2); } #[test] fn into_iter() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let pairs = bimap.into_iter().collect::>(); assert_eq!(pairs, vec![('a', 3), ('b', 2), ('c', 1)]); } @@ -1037,9 +1037,9 @@ mod tests { #[test] fn into_iter_ref() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let pairs = (&bimap).into_iter().collect::>(); assert_eq!(pairs, vec![(&'a', &3), (&'b', &2), (&'c', &1)]); } @@ -1047,13 +1047,13 @@ mod tests { #[test] fn extend() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); bimap.extend(vec![('c', 3), ('b', 1), ('a', 4)]); let mut bimap2 = BiBTreeMap::new(); - bimap2.insert('a', 4); - bimap2.insert('b', 1); - bimap2.insert('c', 3); + bimap2.insert('a', 4).expect_neither(); + bimap2.insert('b', 1).expect_neither(); + bimap2.insert('c', 3).expect_neither(); assert_eq!(bimap, bimap2); } @@ -1075,9 +1075,9 @@ mod tests { #[test] fn iter() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let pairs = bimap.iter().map(|(c, i)| (*c, *i)).collect::>(); assert_eq!(pairs, vec![('a', 1), ('b', 2), ('c', 3)]); } @@ -1085,9 +1085,9 @@ mod tests { #[test] fn iter_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let mut iter = bimap.iter(); assert_eq!(iter.next_back(), Some((&'c', &3))); @@ -1098,9 +1098,9 @@ mod tests { #[test] fn into_iter_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let mut iter = bimap.into_iter(); assert_eq!(iter.next_back(), Some(('c', 3))); @@ -1111,9 +1111,9 @@ mod tests { #[test] fn left_values() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let left_values = bimap.left_values().cloned().collect::>(); assert_eq!(left_values, vec!['a', 'b', 'c']) } @@ -1121,9 +1121,9 @@ mod tests { #[test] fn left_values_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut iter = bimap.left_values(); assert_eq!(iter.next_back(), Some(&'c')); @@ -1134,9 +1134,9 @@ mod tests { #[test] fn right_values() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let right_values = bimap.right_values().cloned().collect::>(); assert_eq!(right_values, vec![1, 2, 3]) } @@ -1144,9 +1144,9 @@ mod tests { #[test] fn right_values_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut iter = bimap.right_values(); assert_eq!(iter.next_back(), Some(&3)); @@ -1157,10 +1157,10 @@ mod tests { #[test] fn left_range() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 4); - bimap.insert('b', 3); - bimap.insert('c', 2); - bimap.insert('d', 1); + bimap.insert('a', 4).expect_neither(); + bimap.insert('b', 3).expect_neither(); + bimap.insert('c', 2).expect_neither(); + bimap.insert('d', 1).expect_neither(); let left_range = bimap .left_range('b'..'d') .map(|(l, r)| (*l, *r)) @@ -1171,10 +1171,10 @@ mod tests { #[test] fn left_range_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 4); - bimap.insert('b', 3); - bimap.insert('c', 2); - bimap.insert('d', 1); + bimap.insert('a', 4).expect_neither(); + bimap.insert('b', 3).expect_neither(); + bimap.insert('c', 2).expect_neither(); + bimap.insert('d', 1).expect_neither(); let mut left_range = bimap.left_range('b'..'d'); assert_eq!(left_range.next_back(), Some((&'c', &2))); @@ -1185,10 +1185,10 @@ mod tests { #[test] fn right_range() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 4); - bimap.insert('b', 3); - bimap.insert('c', 2); - bimap.insert('d', 1); + bimap.insert('a', 4).expect_neither(); + bimap.insert('b', 3).expect_neither(); + bimap.insert('c', 2).expect_neither(); + bimap.insert('d', 1).expect_neither(); let right_range = bimap .right_range(2..4) .map(|(l, r)| (*l, *r)) @@ -1199,10 +1199,10 @@ mod tests { #[test] fn right_range_rev() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 4); - bimap.insert('b', 3); - bimap.insert('c', 2); - bimap.insert('d', 1); + bimap.insert('a', 4).expect_neither(); + bimap.insert('b', 3).expect_neither(); + bimap.insert('c', 2).expect_neither(); + bimap.insert('d', 1).expect_neither(); let mut right_range = bimap.right_range(2..4); assert_eq!(right_range.next_back(), Some((&'b', &3))); diff --git a/src/hash.rs b/src/hash.rs index c56f84c..23ca7f7 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -75,9 +75,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// assert_eq!(bimap.len(), 3); /// ``` pub fn len(&self) -> usize { @@ -94,7 +94,7 @@ where /// /// let mut bimap = BiHashMap::new(); /// assert!(bimap.is_empty()); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(!bimap.is_empty()); /// bimap.remove_by_right(&1); /// assert!(bimap.is_empty()); @@ -126,9 +126,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// bimap.clear(); /// assert!(bimap.len() == 0); /// ``` @@ -148,9 +148,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for (left, right) in bimap.iter() { /// println!("({}, {})", left, right); @@ -173,9 +173,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for char_value in bimap.left_values() { /// println!("{}", char_value); @@ -198,9 +198,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// for int_value in bimap.right_values() { /// println!("{}", int_value); @@ -232,7 +232,7 @@ where /// let s_left = RandomState::new(); /// let s_right = RandomState::new(); /// let mut bimap = BiHashMap::::with_hashers(s_left, s_right); - /// bimap.insert('a', 42); + /// bimap.insert('a', 42).expect_neither(); /// ``` pub fn with_hashers(hash_builder_left: LS, hash_builder_right: RS) -> Self { Self { @@ -299,8 +299,8 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::::with_capacity(100); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); /// assert!(bimap.capacity() >= 100); /// bimap.shrink_to_fit(); /// assert!(bimap.capacity() >= 2); @@ -323,8 +323,8 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::::with_capacity(100); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); /// assert!(bimap.capacity() >= 100); /// bimap.shrink_to(10); /// assert!(bimap.capacity() >= 10); @@ -348,7 +348,7 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert_eq!(bimap.get_by_left(&'a'), Some(&1)); /// assert_eq!(bimap.get_by_left(&'z'), None); /// ``` @@ -372,7 +372,7 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert_eq!(bimap.get_by_right(&1), Some(&'a')); /// assert_eq!(bimap.get_by_right(&2), None); /// ``` @@ -396,7 +396,7 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(bimap.contains_left(&'a')); /// assert!(!bimap.contains_left(&'b')); /// ``` @@ -420,7 +420,7 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); + /// bimap.insert('a', 1).expect_neither(); /// assert!(bimap.contains_right(&1)); /// assert!(!bimap.contains_right(&2)); /// ``` @@ -446,9 +446,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// assert_eq!(bimap.remove_by_left(&'b'), Some(('b', 2))); /// assert_eq!(bimap.remove_by_left(&'b'), None); @@ -483,9 +483,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// /// assert_eq!(bimap.remove_by_right(&2), Some(('b', 2))); /// assert_eq!(bimap.remove_by_right(&2), None); @@ -612,9 +612,9 @@ where /// use bimap::BiHashMap; /// /// let mut bimap = BiHashMap::new(); - /// bimap.insert('a', 1); - /// bimap.insert('b', 2); - /// bimap.insert('c', 3); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('b', 2).expect_neither(); + /// bimap.insert('c', 3).expect_neither(); /// bimap.retain(|&l, &r| r >= 2); /// assert_eq!(bimap.len(), 2); /// assert_eq!(bimap.get_by_left(&'b'), Some(&2)); @@ -660,7 +660,7 @@ where self.right2left.hasher().clone(), ); for (l, r) in self.iter() { - new_bimap.insert(l.clone(), r.clone()); + new_bimap.insert(l.clone(), r.clone()).expect_neither(); } new_bimap } @@ -742,7 +742,7 @@ where } }; for (left, right) in iter { - bimap.insert(left, right); + let _ = bimap.insert(left, right); } bimap } @@ -785,7 +785,7 @@ where { fn extend>(&mut self, iter: T) { iter.into_iter().for_each(move |(l, r)| { - self.insert(l, r); + let _ = self.insert(l, r); }); } } @@ -932,8 +932,8 @@ mod tests { #[test] fn clone() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); let bimap2 = bimap.clone(); assert_eq!(bimap, bimap2); } @@ -941,13 +941,13 @@ mod tests { #[test] fn deep_clone() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); let mut bimap2 = bimap.clone(); // would panic if clone() didn't deep clone - bimap.insert('b', 5); - bimap2.insert('a', 12); + assert!(bimap.insert('b', 5).did_overwrite()); + assert!(bimap2.insert('a', 12).did_overwrite()); bimap2.remove_by_left(&'a'); bimap.remove_by_right(&2); } @@ -957,10 +957,10 @@ mod tests { let mut bimap = BiHashMap::new(); assert_eq!("{}", format!("{:?}", bimap)); - bimap.insert('a', 1); + bimap.insert('a', 1).expect_neither(); assert_eq!("{'a' <> 1}", format!("{:?}", bimap)); - bimap.insert('b', 2); + bimap.insert('b', 2).expect_neither(); let expected1 = "{'a' <> 1, 'b' <> 2}"; let expected2 = "{'b' <> 2, 'a' <> 1}"; let formatted = format!("{:?}", bimap); @@ -976,18 +976,18 @@ mod tests { fn eq() { let mut bimap = BiHashMap::new(); assert_eq!(bimap, bimap); - bimap.insert('a', 1); + bimap.insert('a', 1).expect_neither(); assert_eq!(bimap, bimap); - bimap.insert('b', 2); + bimap.insert('b', 2).expect_neither(); assert_eq!(bimap, bimap); let mut bimap2 = BiHashMap::new(); assert_ne!(bimap, bimap2); - bimap2.insert('a', 1); + bimap2.insert('a', 1).expect_neither(); assert_ne!(bimap, bimap2); - bimap2.insert('b', 2); + bimap2.insert('b', 2).expect_neither(); assert_eq!(bimap, bimap2); - bimap2.insert('c', 3); + bimap2.insert('c', 3).expect_neither(); assert_ne!(bimap, bimap2); } @@ -1002,17 +1002,17 @@ mod tests { ('b', 3), ]); let mut bimap2 = BiHashMap::new(); - bimap2.insert('a', 4); - bimap2.insert('b', 3); + bimap2.insert('a', 4).expect_neither(); + bimap2.insert('b', 3).expect_neither(); assert_eq!(bimap, bimap2); } #[test] fn into_iter() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut pairs = bimap.into_iter().collect::>(); pairs.sort(); assert_eq!(pairs, vec![('a', 3), ('b', 2), ('c', 1)]); @@ -1021,9 +1021,9 @@ mod tests { #[test] fn into_iter_ref() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut pairs = (&bimap).into_iter().collect::>(); pairs.sort(); assert_eq!(pairs, vec![(&'a', &3), (&'b', &2), (&'c', &1)]); @@ -1032,22 +1032,22 @@ mod tests { #[test] fn extend() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); bimap.extend(vec![('c', 3), ('b', 1), ('a', 4)]); let mut bimap2 = BiHashMap::new(); - bimap2.insert('a', 4); - bimap2.insert('b', 1); - bimap2.insert('c', 3); + bimap2.insert('a', 4).expect_neither(); + bimap2.insert('b', 1).expect_neither(); + bimap2.insert('c', 3).expect_neither(); assert_eq!(bimap, bimap2); } #[test] fn iter() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let mut pairs = bimap.iter().map(|(c, i)| (*c, *i)).collect::>(); pairs.sort(); assert_eq!(pairs, vec![('a', 1), ('b', 2), ('c', 3)]); @@ -1056,9 +1056,9 @@ mod tests { #[test] fn left_values() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut left_values = bimap.left_values().cloned().collect::>(); left_values.sort(); assert_eq!(left_values, vec!['a', 'b', 'c']) @@ -1067,9 +1067,9 @@ mod tests { #[test] fn right_values() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 3); - bimap.insert('b', 2); - bimap.insert('c', 1); + bimap.insert('a', 3).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 1).expect_neither(); let mut right_values = bimap.right_values().cloned().collect::>(); right_values.sort(); assert_eq!(right_values, vec![1, 2, 3]) @@ -1086,7 +1086,7 @@ mod tests { let s_left = hash_map::RandomState::new(); let s_right = hash_map::RandomState::new(); let mut bimap = BiHashMap::::with_hashers(s_left, s_right); - bimap.insert('a', 42); + bimap.insert('a', 42).expect_neither(); assert_eq!(Some(&'a'), bimap.get_by_right(&42)); assert_eq!(Some(&42), bimap.get_by_left(&'a')); } @@ -1111,8 +1111,8 @@ mod tests { assert_eq!(bimap.len(), 0); assert!(bimap.capacity() >= 100); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); assert!(!bimap.is_empty()); assert_eq!(bimap.len(), 2); assert!(bimap.capacity() >= 100); @@ -1130,8 +1130,8 @@ mod tests { assert_eq!(bimap.len(), 0); assert!(bimap.capacity() >= 100); - bimap.insert('a', 1); - bimap.insert('b', 2); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); assert!(!bimap.is_empty()); assert_eq!(bimap.len(), 2); assert!(bimap.capacity() >= 100); @@ -1201,9 +1201,9 @@ mod tests { #[test] fn retain_calls_f_once() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); // retain one element let mut i = 0; diff --git a/src/lib.rs b/src/lib.rs index f814032..209c221 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,8 +62,8 @@ //! use bimap::BiMap; //! //! let mut bimap = BiMap::new(); -//! bimap.insert('a', 1); -//! bimap.insert('b', 1); // what to do here? +//! bimap.insert('a', 1).expect_neither(); +//! let _ = bimap.insert('b', 1); // what to do here? //! ``` //! //! In order to maintain the bijection, the bimap cannot have both left-right @@ -135,7 +135,7 @@ //! //! // insert both Foos into a bimap //! let mut bimap = BiMap::new(); -//! bimap.insert(foo1, 99); +//! bimap.insert(foo1, 99).expect_neither(); //! let overwritten = bimap.insert(foo2, 100); //! //! // foo1 is overwritten and returned @@ -198,11 +198,14 @@ #[allow(unused_imports)] #[macro_use] extern crate alloc; +extern crate core; mod mem; pub mod btree; + pub use btree::BiBTreeMap; +use core::fmt::Debug; #[cfg(feature = "std")] pub mod hash; @@ -225,6 +228,7 @@ pub mod serde; /// The previous left-right pairs, if any, that were overwritten by a call to /// the [`insert`](BiHashMap::insert) method of a bimap. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[must_use = "This `Overwritten` may not be a `Neither` variant, which should be handled"] pub enum Overwritten { /// Neither the left nor the right value previously existed in the bimap. Neither, @@ -266,6 +270,34 @@ impl Overwritten { pub fn did_overwrite(&self) -> bool { !matches!(self, Overwritten::Neither) } + + /// Panics if the `Overwritten` variant is not `Neither`. + /// This method is useful for consume Overwritten like `unwrap`. + /// + /// # Examples + /// + /// ```rust,should_panic + /// use bimap::{BiMap, Overwritten}; + /// + /// let mut bimap = BiMap::new(); + /// bimap.insert('a', 1).expect_neither(); + /// bimap.insert('a', 2).expect_neither(); + /// ``` + pub fn expect_neither(&self) { + match self { + Overwritten::Neither => (), + _ => panic!( + "Expected Overwritten::Neither, got {}", + match self { + Overwritten::Left(_, _) => "Overwritten::Left", + Overwritten::Right(_, _) => "Overwritten::Right", + Overwritten::Pair(_, _) => "Overwritten::Pair", + Overwritten::Both(_, _) => "Overwritten::Both", + _ => unreachable!(), + } + ), + } + } } #[cfg(test)] diff --git a/src/serde.rs b/src/serde.rs index 83fd9cc..be470b7 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -123,9 +123,9 @@ //! let mut bimap = BiHashMap::new(); //! //! // insert some pairs -//! bimap.insert('A', 1); -//! bimap.insert('B', 2); -//! bimap.insert('C', 3); +//! bimap.insert('A', 1).expect_neither(); +//! bimap.insert('B', 2).expect_neither(); +//! bimap.insert('C', 3).expect_neither(); //! //! // serialize the bimap //! let json = serde_json::to_string(&bimap).unwrap(); @@ -196,7 +196,7 @@ where None => BiHashMap::::with_hashers(LS::default(), RS::default()), }; while let Some((l, r)) = entries.next_entry()? { - map.insert(l, r); + let _ = map.insert(l, r); } Ok(map) } @@ -246,7 +246,7 @@ where fn visit_map>(self, mut entries: A) -> Result { let mut map = BiBTreeMap::new(); while let Some((l, r)) = entries.next_entry()? { - map.insert(l, r); + map.insert(l, r).expect_neither(); } Ok(map) } @@ -274,9 +274,9 @@ mod tests { #[test] fn serde_hash() { let mut bimap = BiHashMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let json = serde_json::to_string(&bimap).unwrap(); let bimap2 = serde_json::from_str(&json).unwrap(); @@ -295,9 +295,9 @@ mod tests { >::with_capacity_and_hashers( 4, hasher_builder.clone(), hasher_builder.clone() ); - bimap.insert('f', 1); - bimap.insert('g', 2); - bimap.insert('h', 3); + bimap.insert('f', 1).expect_neither(); + bimap.insert('g', 2).expect_neither(); + bimap.insert('h', 3).expect_neither(); let json = serde_json::to_string(&bimap).unwrap(); let bimap2 = serde_json::from_str(&json).unwrap(); @@ -316,9 +316,9 @@ mod tests { >::with_capacity_and_hashers( 4, hasher_builder.clone(), hasher_builder.clone() ); - bimap.insert('x', 1); - bimap.insert('y', 2); - bimap.insert('z', 3); + bimap.insert('x', 1).expect_neither(); + bimap.insert('y', 2).expect_neither(); + bimap.insert('z', 3).expect_neither(); let json = serde_json::to_string(&bimap).unwrap(); let bimap2 = serde_json::from_str(&json).unwrap(); @@ -329,9 +329,9 @@ mod tests { #[test] fn serde_btree() { let mut bimap = BiBTreeMap::new(); - bimap.insert('a', 1); - bimap.insert('b', 2); - bimap.insert('c', 3); + bimap.insert('a', 1).expect_neither(); + bimap.insert('b', 2).expect_neither(); + bimap.insert('c', 3).expect_neither(); let json = serde_json::to_string(&bimap).unwrap(); let bimap2 = serde_json::from_str(&json).unwrap(); diff --git a/tests/hello_world.rs b/tests/hello_world.rs index 658268a..e0842ba 100644 --- a/tests/hello_world.rs +++ b/tests/hello_world.rs @@ -5,9 +5,9 @@ fn main() { // A bijective map between letters of the English alphabet and their positions. let mut alphabet = bimap::BiMap::::new(); - alphabet.insert('A', 1); + alphabet.insert('A', 1).expect_neither(); // some letters omitted for brevity - alphabet.insert('Z', 26); + alphabet.insert('Z', 26).expect_neither(); println!("A is at position {}", alphabet.get_by_left(&'A').unwrap()); println!("{} is at position 26", alphabet.get_by_right(&26).unwrap());