Insert a new string into this pool.
(&mut self, s: &str)
| 178 | |
| 179 | /// Insert a new string into this pool. |
| 180 | pub fn insert(&mut self, s: &str) -> Result<Atom, OutOfMemory> { |
| 181 | if let Some(atom) = self.map.get(s) { |
| 182 | return Ok(*atom); |
| 183 | } |
| 184 | |
| 185 | self.map.reserve(1)?; |
| 186 | self.strings.reserve(1)?; |
| 187 | |
| 188 | let mut owned = TryString::new(); |
| 189 | owned.reserve_exact(s.len())?; |
| 190 | owned.push_str(s).expect("reserved capacity"); |
| 191 | let owned = owned |
| 192 | .into_boxed_str() |
| 193 | .expect("reserved exact capacity, so shouldn't need to realloc"); |
| 194 | |
| 195 | self.insert_new_boxed_str(owned) |
| 196 | } |
| 197 | |
| 198 | fn insert_new_boxed_str(&mut self, owned: Box<str>) -> Result<Atom, OutOfMemory> { |
| 199 | debug_assert!(!self.map.contains_key(&*owned)); |