1 use crate::{collections::TryClone, error::OutOfMemory}; 2 use core::{ 3 borrow::Borrow, 4 cmp::Ordering, 5 fmt, 6 hash::{BuildHasher, Hash}, 7 marker::PhantomData, 8 mem, 9 ops::{Index, IndexMut, RangeBounds}, 10 }; 11 use indexmap::map as inner; 12 13 #[cfg(feature = "std")] 14 use std::hash::RandomState as DefaultHashBuilder; 15 16 #[cfg(not(feature = "std"))] 17 use hashbrown::DefaultHashBuilder; 18 19 /// A wrapper around [`indexmap::IndexMap`] that only provides fallible 20 /// allocation. 21 pub struct TryIndexMap<K, V, S = DefaultHashBuilder> { 22 inner: indexmap::IndexMap<K, V, S>, 23 } 24 25 impl<K, V, S> TryClone for TryIndexMap<K, V, S> 26 where 27 K: Eq + Hash + TryClone, 28 V: TryClone, 29 S: BuildHasher + TryClone, 30 { try_clone(&self) -> Result<Self, OutOfMemory>31 fn try_clone(&self) -> Result<Self, OutOfMemory> { 32 let mut map = Self::with_capacity_and_hasher(self.capacity(), self.hasher().try_clone()?)?; 33 for (k, v) in self.iter() { 34 map.insert(k.try_clone()?, v.try_clone()?)?; 35 } 36 Ok(map) 37 } 38 } 39 40 impl<K, V, S> fmt::Debug for TryIndexMap<K, V, S> 41 where 42 K: fmt::Debug, 43 V: fmt::Debug, 44 S: fmt::Debug, 45 { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 47 fmt::Debug::fmt(&self.inner, f) 48 } 49 } 50 51 impl<K, V, S> From<TryIndexMap<K, V, S>> for indexmap::IndexMap<K, V, S> { from(map: TryIndexMap<K, V, S>) -> Self52 fn from(map: TryIndexMap<K, V, S>) -> Self { 53 map.inner 54 } 55 } 56 57 impl<K, V, S> From<indexmap::IndexMap<K, V, S>> for TryIndexMap<K, V, S> { from(inner: indexmap::IndexMap<K, V, S>) -> Self58 fn from(inner: indexmap::IndexMap<K, V, S>) -> Self { 59 Self { inner } 60 } 61 } 62 63 impl<K, V, H> serde::ser::Serialize for TryIndexMap<K, V, H> 64 where 65 K: serde::ser::Serialize, 66 V: serde::ser::Serialize, 67 { serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer,68 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 69 where 70 S: serde::Serializer, 71 { 72 use serde::ser::SerializeMap as _; 73 let mut map = serializer.serialize_map(Some(self.len()))?; 74 for (k, v) in self { 75 map.serialize_entry(k, v)?; 76 } 77 map.end() 78 } 79 } 80 81 impl<'de, K, V> serde::de::Deserialize<'de> for TryIndexMap<K, V> 82 where 83 K: serde::de::Deserialize<'de> + Hash + Eq, 84 V: serde::de::Deserialize<'de>, 85 { deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de>,86 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> 87 where 88 D: serde::Deserializer<'de>, 89 { 90 struct Visitor<K, V>(PhantomData<fn() -> TryIndexMap<K, V>>); 91 92 impl<'de, K, V> serde::de::Visitor<'de> for Visitor<K, V> 93 where 94 K: serde::de::Deserialize<'de> + Hash + Eq, 95 V: serde::de::Deserialize<'de>, 96 { 97 type Value = TryIndexMap<K, V>; 98 99 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { 100 f.write_str("an `IndexMap`") 101 } 102 103 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> 104 where 105 A: serde::de::MapAccess<'de>, 106 { 107 use serde::de::Error as _; 108 109 let mut result = TryIndexMap::<K, V>::new(); 110 111 if let Some(len) = map.size_hint() { 112 result.reserve(len).map_err(|oom| A::Error::custom(oom))?; 113 } 114 115 while let Some((k, v)) = map.next_entry::<K, V>()? { 116 result.insert(k, v).map_err(|oom| A::Error::custom(oom))?; 117 } 118 119 Ok(result) 120 } 121 } 122 123 deserializer.deserialize_map(Visitor(PhantomData)) 124 } 125 } 126 127 impl<K, V> TryIndexMap<K, V> { 128 /// Same as [`indexmap::IndexMap::new`]. new() -> Self129 pub fn new() -> Self { 130 Self { 131 inner: indexmap::IndexMap::with_hasher(<_>::default()), 132 } 133 } 134 135 /// Same as [`indexmap::IndexMap::with_capacity`] but returns an error on 136 /// allocation failure. with_capacity(n: usize) -> Result<Self, OutOfMemory>137 pub fn with_capacity(n: usize) -> Result<Self, OutOfMemory> { 138 Self::with_capacity_and_hasher(n, <_>::default()) 139 } 140 } 141 142 impl<K, V, S> TryIndexMap<K, V, S> { 143 /// Same as [`indexmap::IndexMap::with_capacity_and_hasher`] but returns an 144 /// error on allocation failure. with_capacity_and_hasher(n: usize, hash_builder: S) -> Result<Self, OutOfMemory>145 pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Result<Self, OutOfMemory> { 146 let mut map = Self::with_hasher(hash_builder); 147 map.reserve(n)?; 148 Ok(map) 149 } 150 151 /// Same as [`indexmap::IndexMap::with_hasher`]. with_hasher(hash_builder: S) -> Self152 pub const fn with_hasher(hash_builder: S) -> Self { 153 TryIndexMap { 154 inner: indexmap::IndexMap::with_hasher(hash_builder), 155 } 156 } 157 158 /// Same as [`indexmap::IndexMap::capacity`]. capacity(&self) -> usize159 pub fn capacity(&self) -> usize { 160 self.inner.capacity() 161 } 162 163 /// Same as [`indexmap::IndexMap::hasher`]. hasher(&self) -> &S164 pub fn hasher(&self) -> &S { 165 self.inner.hasher() 166 } 167 168 /// Same as [`indexmap::IndexMap::len`]. len(&self) -> usize169 pub fn len(&self) -> usize { 170 self.inner.len() 171 } 172 173 /// Same as [`indexmap::IndexMap::is_empty`]. is_empty(&self) -> bool174 pub fn is_empty(&self) -> bool { 175 self.inner.is_empty() 176 } 177 178 /// Same as [`indexmap::IndexMap::iter`]. iter(&self) -> inner::Iter<'_, K, V>179 pub fn iter(&self) -> inner::Iter<'_, K, V> { 180 self.inner.iter() 181 } 182 183 /// Same as [`indexmap::IndexMap::iter_mut`]. iter_mut(&mut self) -> inner::IterMut<'_, K, V>184 pub fn iter_mut(&mut self) -> inner::IterMut<'_, K, V> { 185 self.inner.iter_mut() 186 } 187 188 /// Same as [`indexmap::IndexMap::keys`]. keys(&self) -> inner::Keys<'_, K, V>189 pub fn keys(&self) -> inner::Keys<'_, K, V> { 190 self.inner.keys() 191 } 192 193 /// Same as [`indexmap::IndexMap::into_keys`]. into_keys(self) -> inner::IntoKeys<K, V>194 pub fn into_keys(self) -> inner::IntoKeys<K, V> { 195 self.inner.into_keys() 196 } 197 198 /// Same as [`indexmap::IndexMap::values`]. values(&self) -> inner::Values<'_, K, V>199 pub fn values(&self) -> inner::Values<'_, K, V> { 200 self.inner.values() 201 } 202 203 /// Same as [`indexmap::IndexMap::values_mut`]. values_mut(&mut self) -> inner::ValuesMut<'_, K, V>204 pub fn values_mut(&mut self) -> inner::ValuesMut<'_, K, V> { 205 self.inner.values_mut() 206 } 207 208 /// Same as [`indexmap::IndexMap::into_values`]. into_values(self) -> inner::IntoValues<K, V>209 pub fn into_values(self) -> inner::IntoValues<K, V> { 210 self.inner.into_values() 211 } 212 213 /// Same as [`indexmap::IndexMap::clear`]. clear(&mut self)214 pub fn clear(&mut self) { 215 self.inner.clear() 216 } 217 218 /// Same as [`indexmap::IndexMap::truncate`]. truncate(&mut self, len: usize)219 pub fn truncate(&mut self, len: usize) { 220 self.inner.truncate(len); 221 } 222 223 /// Same as [`indexmap::IndexMap::drain`]. drain<R>(&mut self, range: R) -> inner::Drain<'_, K, V> where R: RangeBounds<usize>,224 pub fn drain<R>(&mut self, range: R) -> inner::Drain<'_, K, V> 225 where 226 R: RangeBounds<usize>, 227 { 228 self.inner.drain(range) 229 } 230 231 /// Same as [`indexmap::IndexMap::extract_if`]. extract_if<F, R>(&mut self, range: R, pred: F) -> inner::ExtractIf<'_, K, V, F> where F: FnMut(&K, &mut V) -> bool, R: RangeBounds<usize>,232 pub fn extract_if<F, R>(&mut self, range: R, pred: F) -> inner::ExtractIf<'_, K, V, F> 233 where 234 F: FnMut(&K, &mut V) -> bool, 235 R: RangeBounds<usize>, 236 { 237 self.inner.extract_if(range, pred) 238 } 239 240 /// Same as [`indexmap::IndexMap::split_off`] but returns an error on 241 /// allocation failure. split_off(&mut self, at: usize) -> Result<Self, OutOfMemory> where K: Eq + Hash, S: BuildHasher + TryClone,242 pub fn split_off(&mut self, at: usize) -> Result<Self, OutOfMemory> 243 where 244 K: Eq + Hash, 245 S: BuildHasher + TryClone, 246 { 247 assert!(at <= self.len()); 248 let mut map = Self::with_capacity_and_hasher(self.len() - at, self.hasher().try_clone()?)?; 249 for (k, v) in self.drain(at..) { 250 map.insert(k, v)?; 251 } 252 Ok(map) 253 } 254 255 /// Same as [`indexmap::IndexMap::reserve`] but returns an error on 256 /// allocation failure. reserve(&mut self, additional: usize) -> Result<(), OutOfMemory>257 pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> { 258 self.inner.try_reserve(additional).map_err(|_| { 259 let new_len = self.len().saturating_add(additional); 260 OutOfMemory::new( 261 new_len 262 .saturating_mul(mem::size_of::<K>()) 263 .saturating_add(new_len.saturating_mul(mem::size_of::<V>())), 264 ) 265 }) 266 } 267 268 /// Same as [`indexmap::IndexMap::reserve_exact`] but returns an error on 269 /// allocation failure. reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory>270 pub fn reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> { 271 self.inner.try_reserve_exact(additional).map_err(|_| { 272 let new_len = self.len().saturating_add(additional); 273 OutOfMemory::new( 274 new_len 275 .saturating_mul(mem::size_of::<K>()) 276 .saturating_add(new_len.saturating_mul(mem::size_of::<V>())), 277 ) 278 }) 279 } 280 } 281 282 impl<K, V, S> TryIndexMap<K, V, S> 283 where 284 K: Hash + Eq, 285 S: BuildHasher, 286 { 287 /// Same as [`indexmap::IndexMap::insert`] but returns an error on 288 /// allocation failure. insert(&mut self, key: K, value: V) -> Result<Option<V>, OutOfMemory>289 pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>, OutOfMemory> { 290 self.reserve(1)?; 291 Ok(self.inner.insert(key, value)) 292 } 293 294 /// Same as [`indexmap::IndexMap::insert_full`] but returns an error on 295 /// allocation failure. insert_full(&mut self, key: K, value: V) -> Result<(usize, Option<V>), OutOfMemory>296 pub fn insert_full(&mut self, key: K, value: V) -> Result<(usize, Option<V>), OutOfMemory> { 297 self.reserve(1)?; 298 Ok(self.inner.insert_full(key, value)) 299 } 300 301 /// Same as [`indexmap::IndexMap::insert_sorted`] but returns an error on 302 /// allocation failure. insert_sorted(&mut self, key: K, value: V) -> Result<(usize, Option<V>), OutOfMemory> where K: Ord,303 pub fn insert_sorted(&mut self, key: K, value: V) -> Result<(usize, Option<V>), OutOfMemory> 304 where 305 K: Ord, 306 { 307 self.reserve(1)?; 308 Ok(self.inner.insert_sorted(key, value)) 309 } 310 311 /// Same as [`indexmap::IndexMap::insert_sorted_by`] but returns an error on 312 /// allocation failure. insert_sorted_by<F>( &mut self, key: K, value: V, cmp: F, ) -> Result<(usize, Option<V>), OutOfMemory> where F: FnMut(&K, &V, &K, &V) -> Ordering,313 pub fn insert_sorted_by<F>( 314 &mut self, 315 key: K, 316 value: V, 317 cmp: F, 318 ) -> Result<(usize, Option<V>), OutOfMemory> 319 where 320 F: FnMut(&K, &V, &K, &V) -> Ordering, 321 { 322 self.reserve(1)?; 323 Ok(self.inner.insert_sorted_by(key, value, cmp)) 324 } 325 326 /// Same as [`indexmap::IndexMap::insert_sorted_by_key`] but returns an 327 /// error on allocation failure. insert_sorted_by_key<B, F>( &mut self, key: K, value: V, sort_key: F, ) -> Result<(usize, Option<V>), OutOfMemory> where B: Ord, F: FnMut(&K, &V) -> B,328 pub fn insert_sorted_by_key<B, F>( 329 &mut self, 330 key: K, 331 value: V, 332 sort_key: F, 333 ) -> Result<(usize, Option<V>), OutOfMemory> 334 where 335 B: Ord, 336 F: FnMut(&K, &V) -> B, 337 { 338 self.reserve(1)?; 339 Ok(self.inner.insert_sorted_by_key(key, value, sort_key)) 340 } 341 342 /// Same as [`indexmap::IndexMap::insert_before`] but returns an error on 343 /// allocation failure. insert_before( &mut self, index: usize, key: K, value: V, ) -> Result<(usize, Option<V>), OutOfMemory>344 pub fn insert_before( 345 &mut self, 346 index: usize, 347 key: K, 348 value: V, 349 ) -> Result<(usize, Option<V>), OutOfMemory> { 350 self.reserve(1)?; 351 Ok(self.inner.insert_before(index, key, value)) 352 } 353 354 /// Same as [`indexmap::IndexMap::shift_insert`] but returns an error on 355 /// allocation failure. shift_insert( &mut self, index: usize, key: K, value: V, ) -> Result<Option<V>, OutOfMemory>356 pub fn shift_insert( 357 &mut self, 358 index: usize, 359 key: K, 360 value: V, 361 ) -> Result<Option<V>, OutOfMemory> { 362 self.reserve(1)?; 363 Ok(self.inner.shift_insert(index, key, value)) 364 } 365 366 /// Same as [`indexmap::IndexMap::shift_insert`]. replace_index(&mut self, index: usize, key: K) -> Result<K, (usize, K)>367 pub fn replace_index(&mut self, index: usize, key: K) -> Result<K, (usize, K)> { 368 self.inner.replace_index(index, key) 369 } 370 } 371 372 impl<K, V, S> TryIndexMap<K, V, S> 373 where 374 S: BuildHasher, 375 { 376 /// Same as [`indexmap::IndexMap::contains_key`]. contains_key<Q>(&self, key: &Q) -> bool where Q: ?Sized + Hash + Eq, K: Borrow<Q>,377 pub fn contains_key<Q>(&self, key: &Q) -> bool 378 where 379 Q: ?Sized + Hash + Eq, 380 K: Borrow<Q>, 381 { 382 self.inner.contains_key(key) 383 } 384 385 /// Same as [`indexmap::IndexMap::get`]. get<Q>(&self, key: &Q) -> Option<&V> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,386 pub fn get<Q>(&self, key: &Q) -> Option<&V> 387 where 388 Q: ?Sized + Hash + Eq, 389 K: Borrow<Q>, 390 { 391 self.inner.get(key) 392 } 393 394 /// Same as [`indexmap::IndexMap::get_key_value`]. get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,395 pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> 396 where 397 Q: ?Sized + Hash + Eq, 398 K: Borrow<Q>, 399 { 400 self.inner.get_key_value(key) 401 } 402 403 /// Same as [`indexmap::IndexMap::get_full`]. get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,404 pub fn get_full<Q>(&self, key: &Q) -> Option<(usize, &K, &V)> 405 where 406 Q: ?Sized + Hash + Eq, 407 K: Borrow<Q>, 408 { 409 self.inner.get_full(key) 410 } 411 412 /// Same as [`indexmap::IndexMap::get_index_of`]. get_index_of<Q>(&self, key: &Q) -> Option<usize> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,413 pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize> 414 where 415 Q: ?Sized + Hash + Eq, 416 K: Borrow<Q>, 417 { 418 self.inner.get_index_of(key) 419 } 420 421 /// Same as [`indexmap::IndexMap::get_mut`]. get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,422 pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V> 423 where 424 Q: ?Sized + Hash + Eq, 425 K: Borrow<Q>, 426 { 427 self.inner.get_mut(key) 428 } 429 430 /// Same as [`indexmap::IndexMap::get_key_value_mut`]. get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,431 pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)> 432 where 433 Q: ?Sized + Hash + Eq, 434 K: Borrow<Q>, 435 { 436 self.inner.get_key_value_mut(key) 437 } 438 439 /// Same as [`indexmap::IndexMap::get_full_mut`]. get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,440 pub fn get_full_mut<Q>(&mut self, key: &Q) -> Option<(usize, &K, &mut V)> 441 where 442 Q: ?Sized + Hash + Eq, 443 K: Borrow<Q>, 444 { 445 self.inner.get_full_mut(key) 446 } 447 448 /// Same as [`indexmap::IndexMap::get_disjoint_mut`]. get_disjoint_mut<Q, const N: usize>(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N] where Q: ?Sized + Hash + Eq, K: Borrow<Q>,449 pub fn get_disjoint_mut<Q, const N: usize>(&mut self, keys: [&Q; N]) -> [Option<&mut V>; N] 450 where 451 Q: ?Sized + Hash + Eq, 452 K: Borrow<Q>, 453 { 454 self.inner.get_disjoint_mut(keys) 455 } 456 457 /// Same as [`indexmap::IndexMap::swap_remove`]. swap_remove<Q>(&mut self, key: &Q) -> Option<V> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,458 pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V> 459 where 460 Q: ?Sized + Hash + Eq, 461 K: Borrow<Q>, 462 { 463 self.inner.swap_remove(key) 464 } 465 466 /// Same as [`indexmap::IndexMap::swap_remove_entry`]. swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,467 pub fn swap_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> 468 where 469 Q: ?Sized + Hash + Eq, 470 K: Borrow<Q>, 471 { 472 self.inner.swap_remove_entry(key) 473 } 474 475 /// Same as [`indexmap::IndexMap::swap_remove_full`]. swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,476 pub fn swap_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> 477 where 478 Q: ?Sized + Hash + Eq, 479 K: Borrow<Q>, 480 { 481 self.inner.swap_remove_full(key) 482 } 483 484 /// Same as [`indexmap::IndexMap::shift_remove`]. shift_remove<Q>(&mut self, key: &Q) -> Option<V> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,485 pub fn shift_remove<Q>(&mut self, key: &Q) -> Option<V> 486 where 487 Q: ?Sized + Hash + Eq, 488 K: Borrow<Q>, 489 { 490 self.inner.shift_remove(key) 491 } 492 493 /// Same as [`indexmap::IndexMap::shift_remove_entry`]. shift_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,494 pub fn shift_remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> 495 where 496 Q: ?Sized + Hash + Eq, 497 K: Borrow<Q>, 498 { 499 self.inner.shift_remove_entry(key) 500 } 501 502 /// Same as [`indexmap::IndexMap::shift_remove_full`]. shift_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> where Q: ?Sized + Hash + Eq, K: Borrow<Q>,503 pub fn shift_remove_full<Q>(&mut self, key: &Q) -> Option<(usize, K, V)> 504 where 505 Q: ?Sized + Hash + Eq, 506 K: Borrow<Q>, 507 { 508 self.inner.shift_remove_full(key) 509 } 510 } 511 512 impl<K, V, S> TryIndexMap<K, V, S> { 513 /// Same as [`indexmap::IndexMap::pop`]. pop(&mut self) -> Option<(K, V)>514 pub fn pop(&mut self) -> Option<(K, V)> { 515 self.inner.pop() 516 } 517 518 /// Same as [`indexmap::IndexMap::retain`]. retain<F>(&mut self, keep: F) where F: FnMut(&K, &mut V) -> bool,519 pub fn retain<F>(&mut self, keep: F) 520 where 521 F: FnMut(&K, &mut V) -> bool, 522 { 523 self.inner.retain(keep); 524 } 525 526 /// Same as [`indexmap::IndexMap::sort_keys`]. sort_keys(&mut self) where K: Ord,527 pub fn sort_keys(&mut self) 528 where 529 K: Ord, 530 { 531 self.inner.sort_keys(); 532 } 533 534 /// Same as [`indexmap::IndexMap::sort_by`]. sort_by<F>(&mut self, cmp: F) where F: FnMut(&K, &V, &K, &V) -> Ordering,535 pub fn sort_by<F>(&mut self, cmp: F) 536 where 537 F: FnMut(&K, &V, &K, &V) -> Ordering, 538 { 539 self.inner.sort_by(cmp); 540 } 541 542 /// Same as [`indexmap::IndexMap::sorted_by`]. sorted_by<F>(self, cmp: F) -> inner::IntoIter<K, V> where F: FnMut(&K, &V, &K, &V) -> Ordering,543 pub fn sorted_by<F>(self, cmp: F) -> inner::IntoIter<K, V> 544 where 545 F: FnMut(&K, &V, &K, &V) -> Ordering, 546 { 547 self.inner.sorted_by(cmp) 548 } 549 550 /// Same as [`indexmap::IndexMap::sort_by_key`]. sort_by_key<T, F>(&mut self, sort_key: F) where T: Ord, F: FnMut(&K, &V) -> T,551 pub fn sort_by_key<T, F>(&mut self, sort_key: F) 552 where 553 T: Ord, 554 F: FnMut(&K, &V) -> T, 555 { 556 self.inner.sort_by_key(sort_key); 557 } 558 559 /// Same as [`indexmap::IndexMap::sort_unstable_keys`]. sort_unstable_keys(&mut self) where K: Ord,560 pub fn sort_unstable_keys(&mut self) 561 where 562 K: Ord, 563 { 564 self.inner.sort_unstable_keys(); 565 } 566 567 /// Same as [`indexmap::IndexMap::sort_unstable_by`]. sort_unstable_by<F>(&mut self, cmp: F) where F: FnMut(&K, &V, &K, &V) -> Ordering,568 pub fn sort_unstable_by<F>(&mut self, cmp: F) 569 where 570 F: FnMut(&K, &V, &K, &V) -> Ordering, 571 { 572 self.inner.sort_unstable_by(cmp); 573 } 574 575 /// Same as [`indexmap::IndexMap::sorted_unstable_by`]. sorted_unstable_by<F>(self, cmp: F) -> inner::IntoIter<K, V> where F: FnMut(&K, &V, &K, &V) -> Ordering,576 pub fn sorted_unstable_by<F>(self, cmp: F) -> inner::IntoIter<K, V> 577 where 578 F: FnMut(&K, &V, &K, &V) -> Ordering, 579 { 580 self.inner.sorted_unstable_by(cmp) 581 } 582 583 /// Same as [`indexmap::IndexMap::sort_unstable_by_key`]. sort_unstable_by_key<T, F>(&mut self, sort_key: F) where T: Ord, F: FnMut(&K, &V) -> T,584 pub fn sort_unstable_by_key<T, F>(&mut self, sort_key: F) 585 where 586 T: Ord, 587 F: FnMut(&K, &V) -> T, 588 { 589 self.inner.sort_unstable_by_key(sort_key); 590 } 591 592 /// Same as [`indexmap::IndexMap::binary_search_keys`]. binary_search_keys(&self, x: &K) -> Result<usize, usize> where K: Ord,593 pub fn binary_search_keys(&self, x: &K) -> Result<usize, usize> 594 where 595 K: Ord, 596 { 597 self.inner.binary_search_keys(x) 598 } 599 600 /// Same as [`indexmap::IndexMap::binary_search_by`]. binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> where F: FnMut(&'a K, &'a V) -> Ordering,601 pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize> 602 where 603 F: FnMut(&'a K, &'a V) -> Ordering, 604 { 605 self.inner.binary_search_by(f) 606 } 607 608 /// Same as [`indexmap::IndexMap::binary_search_by_key`]. binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> where F: FnMut(&'a K, &'a V) -> B, B: Ord,609 pub fn binary_search_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<usize, usize> 610 where 611 F: FnMut(&'a K, &'a V) -> B, 612 B: Ord, 613 { 614 self.inner.binary_search_by_key(b, f) 615 } 616 617 /// Same as [`indexmap::IndexMap::is_sorted`]. is_sorted(&self) -> bool where K: PartialOrd,618 pub fn is_sorted(&self) -> bool 619 where 620 K: PartialOrd, 621 { 622 self.inner.is_sorted() 623 } 624 625 /// Same as [`indexmap::IndexMap::is_sorted_by`]. is_sorted_by<'a, F>(&'a self, cmp: F) -> bool where F: FnMut(&'a K, &'a V, &'a K, &'a V) -> bool,626 pub fn is_sorted_by<'a, F>(&'a self, cmp: F) -> bool 627 where 628 F: FnMut(&'a K, &'a V, &'a K, &'a V) -> bool, 629 { 630 self.inner.is_sorted_by(cmp) 631 } 632 633 /// Same as [`indexmap::IndexMap::is_sorted_by_key`]. is_sorted_by_key<'a, F, T>(&'a self, sort_key: F) -> bool where F: FnMut(&'a K, &'a V) -> T, T: PartialOrd,634 pub fn is_sorted_by_key<'a, F, T>(&'a self, sort_key: F) -> bool 635 where 636 F: FnMut(&'a K, &'a V) -> T, 637 T: PartialOrd, 638 { 639 self.inner.is_sorted_by_key(sort_key) 640 } 641 642 /// Same as [`indexmap::IndexMap::partition_point`]. 643 #[must_use] partition_point<P>(&self, pred: P) -> usize where P: FnMut(&K, &V) -> bool,644 pub fn partition_point<P>(&self, pred: P) -> usize 645 where 646 P: FnMut(&K, &V) -> bool, 647 { 648 self.inner.partition_point(pred) 649 } 650 651 /// Same as [`indexmap::IndexMap::reverse`]. reverse(&mut self)652 pub fn reverse(&mut self) { 653 self.inner.reverse() 654 } 655 656 /// Same as [`indexmap::IndexMap::get_index`]. get_index(&self, index: usize) -> Option<(&K, &V)>657 pub fn get_index(&self, index: usize) -> Option<(&K, &V)> { 658 self.inner.get_index(index) 659 } 660 661 /// Same as [`indexmap::IndexMap::get_index_mut`]. get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)>662 pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { 663 self.inner.get_index_mut(index) 664 } 665 666 /// Same as [`indexmap::IndexMap::get_disjoint_indices_mut`]. get_disjoint_indices_mut<const N: usize>( &mut self, indices: [usize; N], ) -> Result<[(&K, &mut V); N], indexmap::GetDisjointMutError>667 pub fn get_disjoint_indices_mut<const N: usize>( 668 &mut self, 669 indices: [usize; N], 670 ) -> Result<[(&K, &mut V); N], indexmap::GetDisjointMutError> { 671 self.inner.get_disjoint_indices_mut(indices) 672 } 673 674 /// Same as [`indexmap::IndexMap::first`]. first(&self) -> Option<(&K, &V)>675 pub fn first(&self) -> Option<(&K, &V)> { 676 self.inner.first() 677 } 678 679 /// Same as [`indexmap::IndexMap::first_mut`]. first_mut(&mut self) -> Option<(&K, &mut V)>680 pub fn first_mut(&mut self) -> Option<(&K, &mut V)> { 681 self.inner.first_mut() 682 } 683 684 /// Same as [`indexmap::IndexMap::last`]. last(&self) -> Option<(&K, &V)>685 pub fn last(&self) -> Option<(&K, &V)> { 686 self.inner.last() 687 } 688 689 /// Same as [`indexmap::IndexMap::last_mut`]. last_mut(&mut self) -> Option<(&K, &mut V)>690 pub fn last_mut(&mut self) -> Option<(&K, &mut V)> { 691 self.inner.last_mut() 692 } 693 694 /// Same as [`indexmap::IndexMap::swap_remove_index`]. swap_remove_index(&mut self, index: usize) -> Option<(K, V)>695 pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> { 696 self.inner.swap_remove_index(index) 697 } 698 699 /// Same as [`indexmap::IndexMap::shift_remove_index`]. shift_remove_index(&mut self, index: usize) -> Option<(K, V)>700 pub fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> { 701 self.inner.shift_remove_index(index) 702 } 703 704 /// Same as [`indexmap::IndexMap::move_index`]. move_index(&mut self, from: usize, to: usize)705 pub fn move_index(&mut self, from: usize, to: usize) { 706 self.inner.move_index(from, to) 707 } 708 709 /// Same as [`indexmap::IndexMap::swap_indices`]. swap_indices(&mut self, a: usize, b: usize)710 pub fn swap_indices(&mut self, a: usize, b: usize) { 711 self.inner.swap_indices(a, b) 712 } 713 } 714 715 impl<K, V, Q, S> Index<&Q> for TryIndexMap<K, V, S> 716 where 717 Q: ?Sized + Hash + Eq, 718 K: Borrow<Q>, 719 S: BuildHasher, 720 { 721 type Output = V; 722 index(&self, key: &Q) -> &V723 fn index(&self, key: &Q) -> &V { 724 &self.inner[key] 725 } 726 } 727 728 impl<K, V, Q, S> IndexMut<&Q> for TryIndexMap<K, V, S> 729 where 730 Q: ?Sized + Hash + Eq, 731 K: Borrow<Q>, 732 S: BuildHasher, 733 { index_mut(&mut self, key: &Q) -> &mut V734 fn index_mut(&mut self, key: &Q) -> &mut V { 735 &mut self.inner[key] 736 } 737 } 738 739 impl<K, V, S> Index<usize> for TryIndexMap<K, V, S> { 740 type Output = V; 741 index(&self, index: usize) -> &V742 fn index(&self, index: usize) -> &V { 743 &self.inner[index] 744 } 745 } 746 747 impl<K, V, S> IndexMut<usize> for TryIndexMap<K, V, S> { index_mut(&mut self, index: usize) -> &mut V748 fn index_mut(&mut self, index: usize) -> &mut V { 749 &mut self.inner[index] 750 } 751 } 752 753 impl<K, V, S> Default for TryIndexMap<K, V, S> 754 where 755 S: Default, 756 { default() -> Self757 fn default() -> Self { 758 Self::with_hasher(S::default()) 759 } 760 } 761 762 impl<K, V1, S1, V2, S2> PartialEq<TryIndexMap<K, V2, S2>> for TryIndexMap<K, V1, S1> 763 where 764 K: Hash + Eq, 765 V1: PartialEq<V2>, 766 S1: BuildHasher, 767 S2: BuildHasher, 768 { eq(&self, other: &TryIndexMap<K, V2, S2>) -> bool769 fn eq(&self, other: &TryIndexMap<K, V2, S2>) -> bool { 770 &self.inner == &other.inner 771 } 772 } 773 774 impl<K, V, S> Eq for TryIndexMap<K, V, S> 775 where 776 K: Eq + Hash, 777 V: Eq, 778 S: BuildHasher, 779 { 780 } 781 782 impl<'a, K, V, S> IntoIterator for &'a TryIndexMap<K, V, S> { 783 type Item = (&'a K, &'a V); 784 type IntoIter = inner::Iter<'a, K, V>; 785 into_iter(self) -> Self::IntoIter786 fn into_iter(self) -> Self::IntoIter { 787 self.iter() 788 } 789 } 790 791 impl<'a, K, V, S> IntoIterator for &'a mut TryIndexMap<K, V, S> { 792 type Item = (&'a K, &'a mut V); 793 type IntoIter = inner::IterMut<'a, K, V>; 794 into_iter(self) -> Self::IntoIter795 fn into_iter(self) -> Self::IntoIter { 796 self.iter_mut() 797 } 798 } 799 800 impl<K, V, S> IntoIterator for TryIndexMap<K, V, S> { 801 type Item = (K, V); 802 type IntoIter = inner::IntoIter<K, V>; 803 into_iter(self) -> Self::IntoIter804 fn into_iter(self) -> Self::IntoIter { 805 self.inner.into_iter() 806 } 807 } 808 809 #[cfg(test)] 810 mod tests { 811 use super::*; 812 use crate::error::Result; 813 814 #[test] smoke() -> Result<()>815 fn smoke() -> Result<()> { 816 let mut map = TryIndexMap::new(); 817 818 map.insert("a", 10)?; 819 map.insert("b", 20)?; 820 map.insert("c", 30)?; 821 822 assert_eq!(map[&"a"], 10); 823 assert_eq!(map[&"b"], 20); 824 assert_eq!(map[&"c"], 30); 825 826 assert_eq!(map[0], 10); 827 assert_eq!(map[1], 20); 828 assert_eq!(map[2], 30); 829 830 Ok(()) 831 } 832 } 833