Inserts the association `key -> value` into the map, returning the previous value for `key`, if any.
(&mut self, key: K, value: V)
| 47 | /// Inserts the association `key -> value` into the map, |
| 48 | /// returning the previous value for `key`, if any. |
| 49 | pub fn insert(&mut self, key: K, value: V) -> Option<V> { |
| 50 | // Possibly convert to large map first. |
| 51 | self.maybe_convert_to_large(); |
| 52 | |
| 53 | match self { |
| 54 | Self::Small(list) => { |
| 55 | if let Some(idx) = Self::key_pos(list, &key) { |
| 56 | // SAFETY: `idx` was given by `key_pos`, so it must be in-bounds. |
| 57 | let (_, val) = unsafe { list.get_unchecked_mut(idx) }; |
| 58 | return Some(mem::replace(val, value)); |
| 59 | } |
| 60 | |
| 61 | list.push((key, value)); |
| 62 | None |
| 63 | } |
| 64 | Self::Large(map) => map.insert(key, value), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Returns either the existing value for `key` |
| 69 | /// or inserts into `key` using `or_insert`. |
nothing calls this directly
no test coverage detected