| 1 |
//! Implement Fallible HashMap |
| 2 |
use super::TryClone; |
| 3 |
use crate::TryReserveError; |
| 4 |
use core::borrow::Borrow; |
| 5 |
use core::default::Default; |
| 6 |
use core::fmt::Debug; |
| 7 |
use core::hash::Hash; |
| 8 |
|
| 9 |
#[cfg(not(all(feature = "std", feature = "rust_1_57")))] |
| 10 |
type HashMap<K, V> = hashbrown::hash_map::HashMap<K, V>; |
| 11 |
|
| 12 |
#[cfg(all(feature = "std", feature = "rust_1_57"))] |
| 13 |
type HashMap<K, V> = std::collections::HashMap<K, V>; |
| 14 |
|
| 15 |
#[cfg(not(all(feature = "std", feature = "rust_1_57")))] |
| 16 |
use hashbrown::hash_map::{Iter, IntoIter}; |
| 17 |
|
| 18 |
#[cfg(all(feature = "std", feature = "rust_1_57"))] |
| 19 |
use std::collections::hash_map::{Iter, IntoIter}; |
| 20 |
|
| 21 |
pub struct TryHashMap<K, V> { |
| 22 |
inner: HashMap<K, V>, |
| 23 |
} |
| 24 |
|
| 25 |
impl<K, V> Default for TryHashMap<K, V> { |
| 26 |
#[inline(always)] |
| 27 |
fn default() -> Self { |
| 28 |
Self { |
| 29 |
inner: Default::default(), |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl<K: Debug, V: Debug> Debug for TryHashMap<K, V> { |
| 35 |
#[inline] |
| 36 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 37 |
self.inner.fmt(f) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl<K, V> TryHashMap<K, V> |
| 42 |
where |
| 43 |
K: Eq + Hash, |
| 44 |
{ |
| 45 |
#[inline] |
| 46 |
pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> { |
| 47 |
let mut map = Self { |
| 48 |
inner: HashMap::new(), |
| 49 |
}; |
| 50 |
map.reserve(capacity)?; |
| 51 |
Ok(map) |
| 52 |
} |
| 53 |
|
| 54 |
#[inline(always)] |
| 55 |
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> |
| 56 |
where |
| 57 |
K: Borrow<Q>, |
| 58 |
Q: Hash + Eq, |
| 59 |
{ |
| 60 |
self.inner.get(k) |
| 61 |
} |
| 62 |
|
| 63 |
#[inline] |
| 64 |
pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { |
| 65 |
self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; |
| 66 |
Ok(self.inner.insert(k, v)) |
| 67 |
} |
| 68 |
|
| 69 |
#[inline(always)] |
| 70 |
pub fn iter(&self) -> Iter<'_, K, V> { |
| 71 |
self.inner.iter() |
| 72 |
} |
| 73 |
|
| 74 |
#[inline(always)] |
| 75 |
pub fn len(&self) -> usize { |
| 76 |
self.inner.len() |
| 77 |
} |
| 78 |
|
| 79 |
#[inline(always)] |
| 80 |
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> |
| 81 |
where |
| 82 |
K: Borrow<Q>, |
| 83 |
Q: Hash + Eq, |
| 84 |
{ |
| 85 |
self.inner.remove(k) |
| 86 |
} |
| 87 |
|
| 88 |
#[inline(always)] |
| 89 |
fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
| 90 |
#[cfg(not(all(feature = "std", feature = "rust_1_57")))] |
| 91 |
{ |
| 92 |
self.inner.try_reserve(additional) |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(all(feature = "std", feature = "rust_1_57"))] |
| 96 |
{ |
| 97 |
self.inner.try_reserve(additional).map_err(|_| { |
| 98 |
crate::make_try_reserve_error(self.len(), additional, |
| 99 |
core::mem::size_of::<K>() + core::mem::size_of::<V>(), |
| 100 |
core::mem::align_of::<K>().max(core::mem::align_of::<V>()), |
| 101 |
) |
| 102 |
}) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl<K, V> IntoIterator for TryHashMap<K, V> { |
| 108 |
type Item = (K, V); |
| 109 |
type IntoIter = IntoIter<K, V>; |
| 110 |
|
| 111 |
#[inline(always)] |
| 112 |
fn into_iter(self) -> Self::IntoIter { |
| 113 |
self.inner.into_iter() |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl<K, V> TryClone for TryHashMap<K, V> |
| 118 |
where |
| 119 |
K: Eq + Hash + TryClone, |
| 120 |
V: TryClone, |
| 121 |
{ |
| 122 |
fn try_clone(&self) -> Result<Self, TryReserveError> { |
| 123 |
let mut clone = Self::with_capacity(self.inner.len())?; |
| 124 |
|
| 125 |
for (key, value) in self.inner.iter() { |
| 126 |
clone.insert(key.try_clone()?, value.try_clone()?)?; |
| 127 |
} |
| 128 |
|
| 129 |
Ok(clone) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn tryhashmap_oom() { |
| 135 |
match TryHashMap::<char, char>::default().reserve(core::usize::MAX) { |
| 136 |
Ok(_) => panic!("it should be OOM"), |
| 137 |
_ => (), |
| 138 |
} |
| 139 |
} |