Coverage Report

Created: 2026-01-17 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
2.18k
    fn default() -> Self {
28
2.18k
        Self {
29
2.18k
            inner: Default::default(),
30
2.18k
        }
31
2.18k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
240
    fn default() -> Self {
28
240
        Self {
29
240
            inner: Default::default(),
30
240
        }
31
240
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
240
    fn default() -> Self {
28
240
        Self {
29
240
            inner: Default::default(),
30
240
        }
31
240
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>> as core::default::Default>::default
Line
Count
Source
27
589
    fn default() -> Self {
28
589
        Self {
29
589
            inner: Default::default(),
30
589
        }
31
589
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>> as core::default::Default>::default
Line
Count
Source
27
240
    fn default() -> Self {
28
240
        Self {
29
240
            inner: Default::default(),
30
240
        }
31
240
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty> as core::default::Default>::default
Line
Count
Source
27
602
    fn default() -> Self {
28
602
        Self {
29
602
            inner: Default::default(),
30
602
        }
31
602
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::default::Default>::default
Line
Count
Source
27
276
    fn default() -> Self {
28
276
        Self {
29
276
            inner: Default::default(),
30
276
        }
31
276
    }
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
1.96k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
1.96k
        let mut map = Self {
48
1.96k
            inner: HashMap::new(),
49
1.96k
        };
50
1.96k
        map.reserve(capacity)?;
51
1.96k
        Ok(map)
52
1.96k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::with_capacity
Line
Count
Source
46
596
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
596
        let mut map = Self {
48
596
            inner: HashMap::new(),
49
596
        };
50
596
        map.reserve(capacity)?;
51
596
        Ok(map)
52
596
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::with_capacity
Line
Count
Source
46
374
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
374
        let mut map = Self {
48
374
            inner: HashMap::new(),
49
374
        };
50
374
        map.reserve(capacity)?;
51
374
        Ok(map)
52
374
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::with_capacity
Line
Count
Source
46
992
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
992
        let mut map = Self {
48
992
            inner: HashMap::new(),
49
992
        };
50
992
        map.reserve(capacity)?;
51
992
        Ok(map)
52
992
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::with_capacity
53
54
    #[inline(always)]
55
35.7k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
35.7k
    where
57
35.7k
        K: Borrow<Q>,
58
35.7k
        Q: Hash + Eq,
59
    {
60
35.7k
        self.inner.get(k)
61
35.7k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::get::<u32>
Line
Count
Source
55
12.0k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
12.0k
    where
57
12.0k
        K: Borrow<Q>,
58
12.0k
        Q: Hash + Eq,
59
    {
60
12.0k
        self.inner.get(k)
61
12.0k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::get::<u32>
Line
Count
Source
55
4.02k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
4.02k
    where
57
4.02k
        K: Borrow<Q>,
58
4.02k
        Q: Hash + Eq,
59
    {
60
4.02k
        self.inner.get(k)
61
4.02k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::get::<u32>
Line
Count
Source
55
767
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
767
    where
57
767
        K: Borrow<Q>,
58
767
        Q: Hash + Eq,
59
    {
60
767
        self.inner.get(k)
61
767
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::get::<u32>
Line
Count
Source
55
11.2k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
11.2k
    where
57
11.2k
        K: Borrow<Q>,
58
11.2k
        Q: Hash + Eq,
59
    {
60
11.2k
        self.inner.get(k)
61
11.2k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
3.67k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
3.67k
    where
57
3.67k
        K: Borrow<Q>,
58
3.67k
        Q: Hash + Eq,
59
    {
60
3.67k
        self.inner.get(k)
61
3.67k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::get::<mp4parse::boxes::FourCC>
Line
Count
Source
55
112
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
112
    where
57
112
        K: Borrow<Q>,
58
112
        Q: Hash + Eq,
59
    {
60
112
        self.inner.get(k)
61
112
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
3.91k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
3.91k
    where
57
3.91k
        K: Borrow<Q>,
58
3.91k
        Q: Hash + Eq,
59
    {
60
3.91k
        self.inner.get(k)
61
3.91k
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::get::<_>
62
63
    #[inline]
64
352k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
352k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
352k
        Ok(self.inner.insert(k, v))
67
352k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::insert
Line
Count
Source
64
323k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
323k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
323k
        Ok(self.inner.insert(k, v))
67
323k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::insert
Line
Count
Source
64
1.39k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
1.39k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
1.39k
        Ok(self.inner.insert(k, v))
67
1.39k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::insert
Line
Count
Source
64
109
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
109
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
109
        Ok(self.inner.insert(k, v))
67
109
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::insert
Line
Count
Source
64
12.0k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
12.0k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
12.0k
        Ok(self.inner.insert(k, v))
67
12.0k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::insert
Line
Count
Source
64
4.02k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
4.02k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
4.02k
        Ok(self.inner.insert(k, v))
67
4.02k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::insert
Line
Count
Source
64
298
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
298
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
298
        Ok(self.inner.insert(k, v))
67
298
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::insert
Line
Count
Source
64
11.2k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
11.2k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
11.2k
        Ok(self.inner.insert(k, v))
67
11.2k
    }
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
272
    pub fn len(&self) -> usize {
76
272
        self.inner.len()
77
272
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::len
Line
Count
Source
75
272
    pub fn len(&self) -> usize {
76
272
        self.inner.len()
77
272
    }
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
354k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
354k
        self.inner
91
354k
            .try_reserve(additional)
92
354k
            .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
354k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::reserve
Line
Count
Source
89
323k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
323k
        self.inner
91
323k
            .try_reserve(additional)
92
323k
            .map_err(|_| make_try_reserve_error())
93
323k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::reserve
Line
Count
Source
89
1.76k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
1.76k
        self.inner
91
1.76k
            .try_reserve(additional)
92
1.76k
            .map_err(|_| make_try_reserve_error())
93
1.76k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::reserve
Line
Count
Source
89
1.10k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
1.10k
        self.inner
91
1.10k
            .try_reserve(additional)
92
1.10k
            .map_err(|_| make_try_reserve_error())
93
1.10k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::reserve
Line
Count
Source
89
12.0k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
12.0k
        self.inner
91
12.0k
            .try_reserve(additional)
92
12.0k
            .map_err(|_| make_try_reserve_error())
93
12.0k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::reserve
Line
Count
Source
89
4.02k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
4.02k
        self.inner
91
4.02k
            .try_reserve(additional)
92
4.02k
            .map_err(|_| make_try_reserve_error())
93
4.02k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::reserve
Line
Count
Source
89
298
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
298
        self.inner
91
298
            .try_reserve(additional)
92
298
            .map_err(|_| make_try_reserve_error())
93
298
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<u8>>>::reserve
Line
Count
Source
89
11.2k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
11.2k
        self.inner
91
11.2k
            .try_reserve(additional)
92
11.2k
            .map_err(|_| make_try_reserve_error())
93
11.2k
    }
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
522
    fn into_iter(self) -> Self::IntoIter {
102
522
        self.inner.into_iter()
103
522
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
101
522
    fn into_iter(self) -> Self::IntoIter {
102
522
        self.inner.into_iter()
103
522
    }
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
}