Coverage Report

Created: 2026-08-31 07:00

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
2.16k
    fn default() -> Self {
28
2.16k
        Self {
29
2.16k
            inner: Default::default(),
30
2.16k
        }
31
2.16k
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>> as core::default::Default>::default
Line
Count
Source
27
103
    fn default() -> Self {
28
103
        Self {
29
103
            inner: Default::default(),
30
103
        }
31
103
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
103
    fn default() -> Self {
28
103
        Self {
29
103
            inner: Default::default(),
30
103
        }
31
103
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>> as core::default::Default>::default
Line
Count
Source
27
103
    fn default() -> Self {
28
103
        Self {
29
103
            inner: Default::default(),
30
103
        }
31
103
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>> as core::default::Default>::default
Line
Count
Source
27
523
    fn default() -> Self {
28
523
        Self {
29
523
            inner: Default::default(),
30
523
        }
31
523
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::default::Default>::default
Line
Count
Source
27
305
    fn default() -> Self {
28
305
        Self {
29
305
            inner: Default::default(),
30
305
        }
31
305
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty> as core::default::Default>::default
Line
Count
Source
27
1.02k
    fn default() -> Self {
28
1.02k
        Self {
29
1.02k
            inner: Default::default(),
30
1.02k
        }
31
1.02k
    }
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.51k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
3.51k
        let mut map = Self {
48
3.51k
            inner: HashMap::new(),
49
3.51k
        };
50
3.51k
        map.reserve(capacity)?;
51
3.51k
        Ok(map)
52
3.51k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::with_capacity
Line
Count
Source
46
1.06k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
1.06k
        let mut map = Self {
48
1.06k
            inner: HashMap::new(),
49
1.06k
        };
50
1.06k
        map.reserve(capacity)?;
51
1.06k
        Ok(map)
52
1.06k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::with_capacity
Line
Count
Source
46
810
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
810
        let mut map = Self {
48
810
            inner: HashMap::new(),
49
810
        };
50
810
        map.reserve(capacity)?;
51
810
        Ok(map)
52
810
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::with_capacity
Line
Count
Source
46
1.64k
    pub fn with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
47
1.64k
        let mut map = Self {
48
1.64k
            inner: HashMap::new(),
49
1.64k
        };
50
1.64k
        map.reserve(capacity)?;
51
1.64k
        Ok(map)
52
1.64k
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::with_capacity
53
54
    #[inline(always)]
55
82.2k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
82.2k
    where
57
82.2k
        K: Borrow<Q>,
58
82.2k
        Q: Hash + Eq,
59
    {
60
82.2k
        self.inner.get(k)
61
82.2k
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>>>::get::<(u32, usize)>
Line
Count
Source
55
18.1k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
18.1k
    where
57
18.1k
        K: Borrow<Q>,
58
18.1k
        Q: Hash + Eq,
59
    {
60
18.1k
        self.inner.get(k)
61
18.1k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::get::<u32>
Line
Count
Source
55
41.1k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
41.1k
    where
57
41.1k
        K: Borrow<Q>,
58
41.1k
        Q: Hash + Eq,
59
    {
60
41.1k
        self.inner.get(k)
61
41.1k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::get::<u32>
Line
Count
Source
55
11.7k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
11.7k
    where
57
11.7k
        K: Borrow<Q>,
58
11.7k
        Q: Hash + Eq,
59
    {
60
11.7k
        self.inner.get(k)
61
11.7k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::get::<u32>
Line
Count
Source
55
608
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
608
    where
57
608
        K: Borrow<Q>,
58
608
        Q: Hash + Eq,
59
    {
60
608
        self.inner.get(k)
61
608
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
5.90k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
5.90k
    where
57
5.90k
        K: Borrow<Q>,
58
5.90k
        Q: Hash + Eq,
59
    {
60
5.90k
        self.inner.get(k)
61
5.90k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::get::<mp4parse::boxes::FourCC>
Line
Count
Source
55
132
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
132
    where
57
132
        K: Borrow<Q>,
58
132
        Q: Hash + Eq,
59
    {
60
132
        self.inner.get(k)
61
132
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::get::<mp4parse::PropertyIndex>
Line
Count
Source
55
4.58k
    pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
56
4.58k
    where
57
4.58k
        K: Borrow<Q>,
58
4.58k
        Q: Hash + Eq,
59
    {
60
4.58k
        self.inner.get(k)
61
4.58k
    }
Unexecuted instantiation: <fallible_collections::hashmap::TryHashMap<_, _>>::get::<_>
62
63
    #[inline]
64
473k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
473k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
473k
        Ok(self.inner.insert(k, v))
67
473k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::insert
Line
Count
Source
64
429k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
429k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
429k
        Ok(self.inner.insert(k, v))
67
429k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::insert
Line
Count
Source
64
2.03k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
2.03k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
2.03k
        Ok(self.inner.insert(k, v))
67
2.03k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::insert
Line
Count
Source
64
124
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
124
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
124
        Ok(self.inner.insert(k, v))
67
124
    }
<fallible_collections::hashmap::TryHashMap<(u32, usize), fallible_collections::vec::TryVec<u8>>>::insert
Line
Count
Source
64
18.1k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
18.1k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
18.1k
        Ok(self.inner.insert(k, v))
67
18.1k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::insert
Line
Count
Source
64
18.5k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
18.5k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
18.5k
        Ok(self.inner.insert(k, v))
67
18.5k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::insert
Line
Count
Source
64
5.20k
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
5.20k
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
5.20k
        Ok(self.inner.insert(k, v))
67
5.20k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::insert
Line
Count
Source
64
184
    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, TryReserveError> {
65
184
        self.reserve(if self.inner.capacity() == 0 { 4 } else { 1 })?;
66
184
        Ok(self.inner.insert(k, v))
67
184
    }
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
545
    pub fn len(&self) -> usize {
76
545
        self.inner.len()
77
545
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::len
Line
Count
Source
75
545
    pub fn len(&self) -> usize {
76
545
        self.inner.len()
77
545
    }
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
477k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
477k
        self.inner
91
477k
            .try_reserve(additional)
92
477k
            .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
477k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::PropertyIndex, mp4parse::ItemProperty>>::reserve
Line
Count
Source
89
430k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
430k
        self.inner
91
430k
            .try_reserve(additional)
92
430k
            .map_err(|_| make_try_reserve_error())
93
430k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem>>::reserve
Line
Count
Source
89
2.84k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
2.84k
        self.inner
91
2.84k
            .try_reserve(additional)
92
2.84k
            .map_err(|_| make_try_reserve_error())
93
2.84k
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::boxes::FourCC, mp4parse::PropertyIndex>>::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<(u32, usize), fallible_collections::vec::TryVec<u8>>>::reserve
Line
Count
Source
89
18.1k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
18.1k
        self.inner
91
18.1k
            .try_reserve(additional)
92
18.1k
            .map_err(|_| make_try_reserve_error())
93
18.1k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackAudioSampleInfo>>>::reserve
Line
Count
Source
89
18.5k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
18.5k
        self.inner
91
18.5k
            .try_reserve(additional)
92
18.5k
            .map_err(|_| make_try_reserve_error())
93
18.5k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse_capi::Mp4parseTrackVideoSampleInfo>>>::reserve
Line
Count
Source
89
5.20k
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
5.20k
        self.inner
91
5.20k
            .try_reserve(additional)
92
5.20k
            .map_err(|_| make_try_reserve_error())
93
5.20k
    }
<fallible_collections::hashmap::TryHashMap<u32, fallible_collections::vec::TryVec<mp4parse::unstable::Indice>>>::reserve
Line
Count
Source
89
184
    fn reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
90
184
        self.inner
91
184
            .try_reserve(additional)
92
184
            .map_err(|_| make_try_reserve_error())
93
184
    }
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
922
    fn into_iter(self) -> Self::IntoIter {
102
922
        self.inner.into_iter()
103
922
    }
<fallible_collections::hashmap::TryHashMap<mp4parse::ItemId, mp4parse::ItemLocationBoxItem> as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
101
922
    fn into_iter(self) -> Self::IntoIter {
102
922
        self.inner.into_iter()
103
922
    }
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
}