Catch insert() on ManagedMap with zero-sized backing store

This commit is contained in:
Astro 2018-03-06 17:20:07 +01:00 committed by whitequark
parent 660378c0bc
commit ecbc2ee8e5

View File

@ -157,6 +157,8 @@ impl<'a, K: Ord + 'a, V: 'a> ManagedMap<'a, K, V> {
pub fn insert(&mut self, key: K, new_value: V) -> Result<Option<V>, (K, V)> {
match self {
&mut ManagedMap::Borrowed(ref mut pairs) if pairs.len() < 1 =>
Err((key, new_value)), // no space at all
&mut ManagedMap::Borrowed(ref mut pairs) => {
match binary_search_by_key(pairs, &key) {
Err(_) if pairs[pairs.len() - 1].is_some() =>
@ -398,6 +400,13 @@ mod test {
assert_eq!(unwrap(&map), [Some(("a", 1)), Some(("b", 2)), Some(("c", 3)), None]);
}
#[test]
fn test_insert_no_space() {
// Zero-sized backing store
let mut map = ManagedMap::Borrowed(&mut []);
assert_eq!(map.insert("a", 1), Err(("a", 1)));
}
#[test]
fn test_remove_nonexistent() {
let mut pairs = one_pair_full();