Inserts the `key`/`value` pair into the hash map, assigning a new identifier to the `key` if it does not yet have one. If the `key` is already present, returns a pair with the previous value and the already assigned identifier. If the `keys` is not yet present, returns a pair with None and the newly-assigned identifier. Returns `None` when the IDs run out.
(&mut self, key: K, value: V)
| 64 | /// |
| 65 | /// Returns `None` when the IDs run out. |
| 66 | pub(super) fn insert(&mut self, key: K, value: V) -> Option<(Option<V>, I)> { |
| 67 | let id = match self.map.get(&key) { |
| 68 | Some((_value, id)) => *id, |
| 69 | None => match I::try_from(self.map.len()) { |
| 70 | Ok(id) => id, |
| 71 | Err(_) => return None, |
| 72 | }, |
| 73 | }; |
| 74 | self.map |
| 75 | .insert(key, (value, id)) |
| 76 | .map(|(prev_value, prev_id)| { |
| 77 | debug_assert_eq!(prev_id, id); |
| 78 | (Some(prev_value), id) |
| 79 | }) |
| 80 | .or(Some((None, id))) |
| 81 | } |
| 82 | |
| 83 | /// Returns the number of assigned identifiers. |
| 84 | pub(super) fn len(&self) -> usize { |