Coverage Report

Created: 2026-08-14 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/fallible_collections-0.5.2/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
3.32k
    fn default() -> Self {
28
3.32k
        Self {
29
3.32k
            inner: Default::default(),
30
3.32k
        }
31
3.32k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty> as core::default::Default>::default
Line
Count
Source
27
1.03k
    fn default() -> Self {
28
1.03k
        Self {
29
1.03k
            inner: Default::default(),
30
1.03k
        }
31
1.03k
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>> as core::default::Default>::default
Line
Count
Source
27
383
    fn default() -> Self {
28
383
        Self {
29
383
            inner: Default::default(),
30
383
        }
31
383
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
383
    fn default() -> Self {
28
383
        Self {
29
383
            inner: Default::default(),
30
383
        }
31
383
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
383
    fn default() -> Self {
28
383
        Self {
29
383
            inner: Default::default(),
30
383
        }
31
383
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>> as core::default::Default>::default
Line
Count
Source
27
789
    fn default() -> Self {
28
789
        Self {
29
789
            inner: Default::default(),
30
789
        }
31
789
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::default::Default>::default
Line
Count
Source
27
353
    fn default() -> Self {
28
353
        Self {
29
353
            inner: Default::default(),
30
353
        }
31
353
    }
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
3.97k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
3.97k
        let mut map = Self {
48
3.97k
            inner: HashMap::new(),
49
3.97k
        };
50
3.97k
        map.reserve(capacity)?;
51
3.97k
        Ok(map)
52
3.97k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::with_capacity
Line
Count
Source
46
1.46k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
1.46k
        let mut map = Self {
48
1.46k
            inner: HashMap::new(),
49
1.46k
        };
50
1.46k
        map.reserve(capacity)?;
51
1.46k
        Ok(map)
52
1.46k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::with_capacity
Line
Count
Source
46
981
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
981
        let mut map = Self {
48
981
            inner: HashMap::new(),
49
981
        };
50
981
        map.reserve(capacity)?;
51
981
        Ok(map)
52
981
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::with_capacity
Line
Count
Source
46
1.53k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
1.53k
        let mut map = Self {
48
1.53k
            inner: HashMap::new(),
49
1.53k
        };
50
1.53k
        map.reserve(capacity)?;
51
1.53k
        Ok(map)
52
1.53k
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::with_capacity
53
54
    #[inline(always)]
55
108k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
108k
    where
57
108k
        K: Borrow<Q>,
58
108k
        Q: Hash + Eq,
59
    {
60
108k
        self.inner.get(k)
61
108k
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>>>::get::<(u32, usize)>
Line
Count
Source
55
25.4k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
25.4k
    where
57
25.4k
        K: Borrow<Q>,
58
25.4k
        Q: Hash + Eq,
59
    {
60
25.4k
        self.inner.get(k)
61
25.4k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::get::<u32>
Line
Count
Source
55
58.3k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
58.3k
    where
57
58.3k
        K: Borrow<Q>,
58
58.3k
        Q: Hash + Eq,
59
    {
60
58.3k
        self.inner.get(k)
61
58.3k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::get::<u32>
Line
Count
Source
55
13.9k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
13.9k
    where
57
13.9k
        K: Borrow<Q>,
58
13.9k
        Q: Hash + Eq,
59
    {
60
13.9k
        self.inner.get(k)
61
13.9k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::get::<u32>
Line
Count
Source
55
1.16k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
1.16k
    where
57
1.16k
        K: Borrow<Q>,
58
1.16k
        Q: Hash + Eq,
59
    {
60
1.16k
        self.inner.get(k)
61
1.16k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
5.61k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
5.61k
    where
57
5.61k
        K: Borrow<Q>,
58
5.61k
        Q: Hash + Eq,
59
    {
60
5.61k
        self.inner.get(k)
61
5.61k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::get::<mp4parse::boxes::FourCC>
Line
Count
Source
55
115
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
115
    where
57
115
        K: Borrow<Q>,
58
115
        Q: Hash + Eq,
59
    {
60
115
        self.inner.get(k)
61
115
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
4.38k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
4.38k
    where
57
4.38k
        K: Borrow<Q>,
58
4.38k
        Q: Hash + Eq,
59
    {
60
4.38k
        self.inner.get(k)
61
4.38k
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::get::<_>
62
63
    #[inline]
64
626k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
626k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
626k
        Ok(self.inner.insert(k, v))
67
626k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::insert
Line
Count
Source
64
566k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
566k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
566k
        Ok(self.inner.insert(k, v))
67
566k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::insert
Line
Count
Source
64
2.20k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
2.20k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
2.20k
        Ok(self.inner.insert(k, v))
67
2.20k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::insert
Line
Count
Source
64
111
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
111
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
111
        Ok(self.inner.insert(k, v))
67
111
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>>>::insert
Line
Count
Source
64
25.4k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
25.4k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
25.4k
        Ok(self.inner.insert(k, v))
67
25.4k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::insert
Line
Count
Source
64
26.3k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
26.3k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
26.3k
        Ok(self.inner.insert(k, v))
67
26.3k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::insert
Line
Count
Source
64
6.03k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
6.03k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
6.03k
        Ok(self.inner.insert(k, v))
67
6.03k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::insert
Line
Count
Source
64
474
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
474
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
474
        Ok(self.inner.insert(k, v))
67
474
    }
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
691
    pub fn len(&self) -> usize {
76
691
        self.inner.len()
77
691
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::len
Line
Count
Source
75
691
    pub fn len(&self) -> usize {
76
691
        self.inner.len()
77
691
    }
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
630k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
630k
        self.inner
91
630k
            .try_reserve(additional)
92
630k
            .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, usize), fallible_collections::vec::TryVec<u8>>>::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<_, _>>::reserve::{closure#0}
93
630k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::reserve
Line
Count
Source
89
567k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
567k
        self.inner
91
567k
            .try_reserve(additional)
92
567k
            .map_err(|_| make_try_reserve_error())
93
567k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::reserve
Line
Count
Source
89
3.18k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
3.18k
        self.inner
91
3.18k
            .try_reserve(additional)
92
3.18k
            .map_err(|_| make_try_reserve_error())
93
3.18k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::reserve
Line
Count
Source
89
1.64k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
1.64k
        self.inner
91
1.64k
            .try_reserve(additional)
92
1.64k
            .map_err(|_| make_try_reserve_error())
93
1.64k
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>>>::reserve
Line
Count
Source
89
25.4k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
25.4k
        self.inner
91
25.4k
            .try_reserve(additional)
92
25.4k
            .map_err(|_| make_try_reserve_error())
93
25.4k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::reserve
Line
Count
Source
89
26.3k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
26.3k
        self.inner
91
26.3k
            .try_reserve(additional)
92
26.3k
            .map_err(|_| make_try_reserve_error())
93
26.3k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::reserve
Line
Count
Source
89
6.03k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
6.03k
        self.inner
91
6.03k
            .try_reserve(additional)
92
6.03k
            .map_err(|_| make_try_reserve_error())
93
6.03k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::reserve
Line
Count
Source
89
474
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
474
        self.inner
91
474
            .try_reserve(additional)
92
474
            .map_err(|_| make_try_reserve_error())
93
474
    }
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
936
    fn into_iter(self) -> Self::IntoIter {
102
936
        self.inner.into_iter()
103
936
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
101
936
    fn into_iter(self) -> Self::IntoIter {
102
936
        self.inner.into_iter()
103
936
    }
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
}