/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fallible_collections-0.5.1/src/hashmap.rs
Line | Count | Source |
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(feature = "std"))] |
10 | | type HashMap<K, V> = hashbrown::hash_map::HashMap<K, V>; |
11 | | |
12 | | #[cfg(feature = "std")] |
13 | | type HashMap<K, V> = std::collections::HashMap<K, V>; |
14 | | |
15 | | #[cfg(not(feature = "std"))] |
16 | | use hashbrown::hash_map::{IntoIter, Iter}; |
17 | | |
18 | | #[cfg(feature = "std")] |
19 | | use std::collections::hash_map::{IntoIter, Iter}; |
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 | 1.71k | fn default() -> Self { |
28 | 1.71k | Self { |
29 | 1.71k | inner: Default::default(), |
30 | 1.71k | } |
31 | 1.71k | } <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>> as core::default::Default>::default Line | Count | Source | 27 | 290 | fn default() -> Self { | 28 | 290 | Self { | 29 | 290 | inner: Default::default(), | 30 | 290 | } | 31 | 290 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>> as core::default::Default>::default Line | Count | Source | 27 | 290 | fn default() -> Self { | 28 | 290 | Self { | 29 | 290 | inner: Default::default(), | 30 | 290 | } | 31 | 290 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>> as core::default::Default>::default Line | Count | Source | 27 | 425 | fn default() -> Self { | 28 | 425 | Self { | 29 | 425 | inner: Default::default(), | 30 | 425 | } | 31 | 425 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>> as core::default::Default>::default Line | Count | Source | 27 | 290 | fn default() -> Self { | 28 | 290 | Self { | 29 | 290 | inner: Default::default(), | 30 | 290 | } | 31 | 290 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty> as core::default::Default>::default Line | Count | Source | 27 | 280 | fn default() -> Self { | 28 | 280 | Self { | 29 | 280 | inner: Default::default(), | 30 | 280 | } | 31 | 280 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::default::Default>::default Line | Count | Source | 27 | 137 | fn default() -> Self { | 28 | 137 | Self { | 29 | 137 | inner: Default::default(), | 30 | 137 | } | 31 | 137 | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _> as core::default::Default>::default |
32 | | } |
33 | | |
34 | | impl<K: Debug, V: Debug> Debug for TryHashMap<K, V> { |
35 | | #[inline] |
36 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
37 | 0 | self.inner.fmt(f) |
38 | 0 | } Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty> as core::fmt::Debug>::fmt Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _> as core::fmt::Debug>::fmt |
39 | | } |
40 | | |
41 | | impl<K, V> TryHashMap<K, V> |
42 | | where |
43 | | K: Eq + Hash, |
44 | | { |
45 | | #[inline] |
46 | 391 | pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> { |
47 | 391 | let mut map = Self { |
48 | 391 | inner: HashMap::new(), |
49 | 391 | }; |
50 | 391 | map.reserve(capacity)?; |
51 | 391 | Ok(map) |
52 | 391 | } <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::with_capacity Line | Count | Source | 46 | 180 | pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> { | 47 | 180 | let mut map = Self { | 48 | 180 | inner: HashMap::new(), | 49 | 180 | }; | 50 | 180 | map.reserve(capacity)?; | 51 | 180 | Ok(map) | 52 | 180 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::with_capacity Line | Count | Source | 46 | 120 | pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> { | 47 | 120 | let mut map = Self { | 48 | 120 | inner: HashMap::new(), | 49 | 120 | }; | 50 | 120 | map.reserve(capacity)?; | 51 | 120 | Ok(map) | 52 | 120 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::with_capacity Line | Count | Source | 46 | 91 | pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> { | 47 | 91 | let mut map = Self { | 48 | 91 | inner: HashMap::new(), | 49 | 91 | }; | 50 | 91 | map.reserve(capacity)?; | 51 | 91 | Ok(map) | 52 | 91 | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::with_capacity |
53 | | |
54 | | #[inline(always)] |
55 | 46.5k | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> |
56 | 46.5k | where |
57 | 46.5k | K: Borrow<Q>, |
58 | 46.5k | Q: Hash + Eq, |
59 | | { |
60 | 46.5k | self.inner.get(k) |
61 | 46.5k | } <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::get::<u32> Line | Count | Source | 55 | 19.0k | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 19.0k | where | 57 | 19.0k | K: Borrow<Q>, | 58 | 19.0k | Q: Hash + Eq, | 59 | | { | 60 | 19.0k | self.inner.get(k) | 61 | 19.0k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::get::<u32> Line | Count | Source | 55 | 6.43k | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 6.43k | where | 57 | 6.43k | K: Borrow<Q>, | 58 | 6.43k | Q: Hash + Eq, | 59 | | { | 60 | 6.43k | self.inner.get(k) | 61 | 6.43k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::get::<u32> Line | Count | Source | 55 | 996 | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 996 | where | 57 | 996 | K: Borrow<Q>, | 58 | 996 | Q: Hash + Eq, | 59 | | { | 60 | 996 | self.inner.get(k) | 61 | 996 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::get::<u32> Line | Count | Source | 55 | 18.3k | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 18.3k | where | 57 | 18.3k | K: Borrow<Q>, | 58 | 18.3k | Q: Hash + Eq, | 59 | | { | 60 | 18.3k | self.inner.get(k) | 61 | 18.3k | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex> Line | Count | Source | 55 | 363 | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 363 | where | 57 | 363 | K: Borrow<Q>, | 58 | 363 | Q: Hash + Eq, | 59 | | { | 60 | 363 | self.inner.get(k) | 61 | 363 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::get::<mp4parse::boxes::FourCC> Line | Count | Source | 55 | 22 | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 22 | where | 57 | 22 | K: Borrow<Q>, | 58 | 22 | Q: Hash + Eq, | 59 | | { | 60 | 22 | self.inner.get(k) | 61 | 22 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex> Line | Count | Source | 55 | 1.29k | pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | 56 | 1.29k | where | 57 | 1.29k | K: Borrow<Q>, | 58 | 1.29k | Q: Hash + Eq, | 59 | | { | 60 | 1.29k | self.inner.get(k) | 61 | 1.29k | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::get::<_> |
62 | | |
63 | | #[inline] |
64 | 343k | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { |
65 | 343k | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; |
66 | 343k | Ok(self.inner.insert(k, v)) |
67 | 343k | } <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::insert Line | Count | Source | 64 | 298k | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 298k | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 298k | Ok(self.inner.insert(k, v)) | 67 | 298k | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::insert Line | Count | Source | 64 | 272 | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 272 | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 272 | Ok(self.inner.insert(k, v)) | 67 | 272 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::insert Line | Count | Source | 64 | 22 | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 22 | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 22 | Ok(self.inner.insert(k, v)) | 67 | 22 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::insert Line | Count | Source | 64 | 19.0k | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 19.0k | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 19.0k | Ok(self.inner.insert(k, v)) | 67 | 19.0k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::insert Line | Count | Source | 64 | 6.43k | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 6.43k | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 6.43k | Ok(self.inner.insert(k, v)) | 67 | 6.43k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::insert Line | Count | Source | 64 | 421 | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 421 | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 421 | Ok(self.inner.insert(k, v)) | 67 | 421 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::insert Line | Count | Source | 64 | 18.3k | pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> { | 65 | 18.3k | self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?; | 66 | 18.3k | Ok(self.inner.insert(k, v)) | 67 | 18.3k | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::insert |
68 | | |
69 | | #[inline(always)] |
70 | 0 | pub fn iter(&self) -> Iter<'_, K, V> { |
71 | 0 | self.inner.iter() |
72 | 0 | } |
73 | | |
74 | | #[inline(always)] |
75 | 7 | pub fn len(&self) -> usize { |
76 | 7 | self.inner.len() |
77 | 7 | } <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::len Line | Count | Source | 75 | 7 | pub fn len(&self) -> usize { | 76 | 7 | self.inner.len() | 77 | 7 | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::len |
78 | | |
79 | | #[inline(always)] |
80 | 0 | pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> |
81 | 0 | where |
82 | 0 | K: Borrow<Q>, |
83 | 0 | Q: Hash + Eq, |
84 | | { |
85 | 0 | self.inner.remove(k) |
86 | 0 | } |
87 | | |
88 | | #[inline(always)] |
89 | 343k | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
90 | 343k | self.inner |
91 | 343k | .try_reserve(additional) |
92 | 343k | .map_err(|_| make_try_reserve_error()) Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::reserve::{closure#0} Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::reserve::{closure#0} |
93 | 343k | } <fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::reserve Line | Count | Source | 89 | 298k | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 298k | self.inner | 91 | 298k | .try_reserve(additional) | 92 | 298k | .map_err(|_| make_try_reserve_error()) | 93 | 298k | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::reserve Line | Count | Source | 89 | 392 | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 392 | self.inner | 91 | 392 | .try_reserve(additional) | 92 | 392 | .map_err(|_| make_try_reserve_error()) | 93 | 392 | } |
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::reserve Line | Count | Source | 89 | 113 | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 113 | self.inner | 91 | 113 | .try_reserve(additional) | 92 | 113 | .map_err(|_| make_try_reserve_error()) | 93 | 113 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::reserve Line | Count | Source | 89 | 19.0k | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 19.0k | self.inner | 91 | 19.0k | .try_reserve(additional) | 92 | 19.0k | .map_err(|_| make_try_reserve_error()) | 93 | 19.0k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::reserve Line | Count | Source | 89 | 6.43k | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 6.43k | self.inner | 91 | 6.43k | .try_reserve(additional) | 92 | 6.43k | .map_err(|_| make_try_reserve_error()) | 93 | 6.43k | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::reserve Line | Count | Source | 89 | 421 | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 421 | self.inner | 91 | 421 | .try_reserve(additional) | 92 | 421 | .map_err(|_| make_try_reserve_error()) | 93 | 421 | } |
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::reserve Line | Count | Source | 89 | 18.3k | fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | 90 | 18.3k | self.inner | 91 | 18.3k | .try_reserve(additional) | 92 | 18.3k | .map_err(|_| make_try_reserve_error()) | 93 | 18.3k | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::reserve |
94 | | } |
95 | | |
96 | | impl<K, V> IntoIterator for TryHashMap<K, V> { |
97 | | type Item = (K, V); |
98 | | type IntoIter = IntoIter<K, V>; |
99 | | |
100 | | #[inline(always)] |
101 | 168 | fn into_iter(self) -> Self::IntoIter { |
102 | 168 | self.inner.into_iter() |
103 | 168 | } <fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::iter::traits::collect::IntoIterator>::into_iter Line | Count | Source | 101 | 168 | fn into_iter(self) -> Self::IntoIter { | 102 | 168 | self.inner.into_iter() | 103 | 168 | } |
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _> as core::iter::traits::collect::IntoIterator>::into_iter |
104 | | } |
105 | | |
106 | | impl<K, V> TryClone for TryHashMap<K, V> |
107 | | where |
108 | | K: Eq + Hash + TryClone, |
109 | | V: TryClone, |
110 | | { |
111 | 0 | fn try_clone(&self) -> Result<Self, TryReserveError> { |
112 | 0 | let mut clone = Self::with_capacity(self.inner.len())?; |
113 | | |
114 | 0 | for (key, value) in self.inner.iter() { |
115 | 0 | clone.insert(key.try_clone()?, value.try_clone()?)?; |
116 | | } |
117 | | |
118 | 0 | Ok(clone) |
119 | 0 | } |
120 | | } |
121 | | |
122 | | #[test] |
123 | | fn tryhashmap_oom() { |
124 | | match TryHashMap::<char, char>::default().reserve(core::usize::MAX) { |
125 | | Ok(_) => panic!("it should be OOM"), |
126 | | _ => (), |
127 | | } |
128 | | } |
129 | | |
130 | | #[cold] |
131 | 0 | fn make_try_reserve_error() -> TryReserveError { |
132 | 0 | let mut v: alloc::vec::Vec<[u8; 1024]> = alloc::vec::Vec::new(); |
133 | | // this will always overflow capacity |
134 | 0 | v.try_reserve(!0).unwrap_err() |
135 | 0 | } |