Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gimli/src/read/util.rs
Line
Count
Source
1
#[cfg(feature = "read")]
2
use alloc::boxed::Box;
3
#[cfg(feature = "read")]
4
use alloc::vec::Vec;
5
use core::fmt;
6
use core::mem::MaybeUninit;
7
use core::ops;
8
use core::ptr;
9
use core::slice;
10
11
mod sealed {
12
    /// # Safety
13
    /// Implementer must not modify the content in storage.
14
    pub unsafe trait Sealed {
15
        type Storage;
16
17
        fn new_storage() -> Self::Storage;
18
19
5.04k
        fn grow(_storage: &mut Self::Storage, _additional: usize) -> Result<(), CapacityFull> {
20
5.04k
            Err(CapacityFull)
21
5.04k
        }
<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192] as gimli::read::util::sealed::Sealed>::grow
Line
Count
Source
19
1.32k
        fn grow(_storage: &mut Self::Storage, _additional: usize) -> Result<(), CapacityFull> {
20
1.32k
            Err(CapacityFull)
21
1.32k
        }
<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]> as gimli::read::util::sealed::Sealed>::grow
Line
Count
Source
19
3.71k
        fn grow(_storage: &mut Self::Storage, _additional: usize) -> Result<(), CapacityFull> {
20
3.71k
            Err(CapacityFull)
21
3.71k
        }
22
    }
23
24
    #[derive(Clone, Copy, Debug)]
25
    pub struct CapacityFull;
26
}
27
28
use sealed::*;
29
30
/// Marker trait for types that can be used as backing storage when a growable array type is needed.
31
///
32
/// This trait is sealed and cannot be implemented for types outside this crate.
33
pub trait ArrayLike: Sealed {
34
    /// Type of the elements being stored.
35
    type Item;
36
37
    #[doc(hidden)]
38
    fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<Self::Item>];
39
40
    #[doc(hidden)]
41
    fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<Self::Item>];
42
}
43
44
// SAFETY: does not modify the content in storage.
45
unsafe impl<T, const N: usize> Sealed for [T; N] {
46
    type Storage = [MaybeUninit<T>; N];
47
48
68.7k
    fn new_storage() -> Self::Storage {
49
        [const { MaybeUninit::uninit() }; N]
50
68.7k
    }
<[gimli::read::cfi::UnwindTableRow<usize>; 4] as gimli::read::util::sealed::Sealed>::new_storage
Line
Count
Source
48
6.45k
    fn new_storage() -> Self::Storage {
49
        [const { MaybeUninit::uninit() }; N]
50
6.45k
    }
<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192] as gimli::read::util::sealed::Sealed>::new_storage
Line
Count
Source
48
62.2k
    fn new_storage() -> Self::Storage {
49
        [const { MaybeUninit::uninit() }; N]
50
62.2k
    }
51
}
52
53
impl<T, const N: usize> ArrayLike for [T; N] {
54
    type Item = T;
55
56
223k
    fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
57
223k
        storage
58
223k
    }
59
60
1.51M
    fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
61
1.51M
        storage
62
1.51M
    }
63
}
64
65
// SAFETY: does not modify the content in storage.
66
#[cfg(feature = "read")]
67
unsafe impl<T, const N: usize> Sealed for Box<[T; N]> {
68
    type Storage = Box<[MaybeUninit<T>; N]>;
69
70
6.45k
    fn new_storage() -> Self::Storage {
71
6.45k
        Box::new(<[T; N]>::new_storage())
72
6.45k
    }
73
}
74
75
#[cfg(feature = "read")]
76
impl<T, const N: usize> ArrayLike for Box<[T; N]> {
77
    type Item = T;
78
79
763k
    fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
80
763k
        &storage[..]
81
763k
    }
82
83
1.22M
    fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
84
1.22M
        &mut storage[..]
85
1.22M
    }
86
}
87
88
#[cfg(feature = "read")]
89
unsafe impl<T> Sealed for Vec<T> {
90
    type Storage = Box<[MaybeUninit<T>]>;
91
92
    fn new_storage() -> Self::Storage {
93
        Box::new([])
94
    }
95
96
    fn grow(storage: &mut Self::Storage, additional: usize) -> Result<(), CapacityFull> {
97
        let mut vec: Vec<_> = core::mem::replace(storage, Box::new([])).into();
98
        vec.reserve(additional);
99
        // SAFETY: This is a `Vec` of `MaybeUninit`.
100
        unsafe { vec.set_len(vec.capacity()) };
101
        *storage = vec.into_boxed_slice();
102
        Ok(())
103
    }
104
}
105
106
#[cfg(feature = "read")]
107
impl<T> ArrayLike for Vec<T> {
108
    type Item = T;
109
110
    fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
111
        storage
112
    }
113
114
    fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
115
        storage
116
    }
117
}
118
119
pub(crate) struct ArrayVec<A: ArrayLike> {
120
    storage: A::Storage,
121
    len: usize,
122
}
123
124
impl<A: ArrayLike> ArrayVec<A> {
125
68.7k
    pub fn new() -> Self {
126
68.7k
        Self {
127
68.7k
            storage: A::new_storage(),
128
68.7k
            len: 0,
129
68.7k
        }
130
68.7k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]>>::new
Line
Count
Source
125
62.2k
    pub fn new() -> Self {
126
62.2k
        Self {
127
62.2k
            storage: A::new_storage(),
128
62.2k
            len: 0,
129
62.2k
        }
130
62.2k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>>>::new
Line
Count
Source
125
6.45k
    pub fn new() -> Self {
126
6.45k
        Self {
127
6.45k
            storage: A::new_storage(),
128
6.45k
            len: 0,
129
6.45k
        }
130
6.45k
    }
131
132
100k
    pub fn clear(&mut self) {
133
100k
        let ptr: *mut [A::Item] = &mut **self;
134
        // Set length first so the type invariant is upheld even if `drop_in_place` panicks.
135
100k
        self.len = 0;
136
        // SAFETY: `ptr` contains valid elements only and we "forget" them by setting the length.
137
100k
        unsafe { ptr::drop_in_place(ptr) };
138
100k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]>>::clear
Line
Count
Source
132
62.2k
    pub fn clear(&mut self) {
133
62.2k
        let ptr: *mut [A::Item] = &mut **self;
134
        // Set length first so the type invariant is upheld even if `drop_in_place` panicks.
135
62.2k
        self.len = 0;
136
        // SAFETY: `ptr` contains valid elements only and we "forget" them by setting the length.
137
62.2k
        unsafe { ptr::drop_in_place(ptr) };
138
62.2k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>>>::clear
Line
Count
Source
132
38.4k
    pub fn clear(&mut self) {
133
38.4k
        let ptr: *mut [A::Item] = &mut **self;
134
        // Set length first so the type invariant is upheld even if `drop_in_place` panicks.
135
38.4k
        self.len = 0;
136
        // SAFETY: `ptr` contains valid elements only and we "forget" them by setting the length.
137
38.4k
        unsafe { ptr::drop_in_place(ptr) };
138
38.4k
    }
139
140
833k
    pub fn try_push(&mut self, value: A::Item) -> Result<(), CapacityFull> {
141
833k
        let mut storage = A::as_mut_slice(&mut self.storage);
142
833k
        if self.len >= storage.len() {
143
4.83k
            A::grow(&mut self.storage, 1)?;
144
0
            storage = A::as_mut_slice(&mut self.storage);
145
828k
        }
146
147
828k
        storage[self.len] = MaybeUninit::new(value);
148
828k
        self.len += 1;
149
828k
        Ok(())
150
833k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]>>::try_push
Line
Count
Source
140
773k
    pub fn try_push(&mut self, value: A::Item) -> Result<(), CapacityFull> {
141
773k
        let mut storage = A::as_mut_slice(&mut self.storage);
142
773k
        if self.len >= storage.len() {
143
1.32k
            A::grow(&mut self.storage, 1)?;
144
0
            storage = A::as_mut_slice(&mut self.storage);
145
772k
        }
146
147
772k
        storage[self.len] = MaybeUninit::new(value);
148
772k
        self.len += 1;
149
772k
        Ok(())
150
773k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>>>::try_push
Line
Count
Source
140
59.6k
    pub fn try_push(&mut self, value: A::Item) -> Result<(), CapacityFull> {
141
59.6k
        let mut storage = A::as_mut_slice(&mut self.storage);
142
59.6k
        if self.len >= storage.len() {
143
3.51k
            A::grow(&mut self.storage, 1)?;
144
0
            storage = A::as_mut_slice(&mut self.storage);
145
56.1k
        }
146
147
56.1k
        storage[self.len] = MaybeUninit::new(value);
148
56.1k
        self.len += 1;
149
56.1k
        Ok(())
150
59.6k
    }
151
152
2.61k
    pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), CapacityFull> {
153
2.61k
        assert!(index <= self.len);
154
155
2.61k
        let mut storage = A::as_mut_slice(&mut self.storage);
156
2.61k
        if self.len >= storage.len() {
157
208
            A::grow(&mut self.storage, 1)?;
158
0
            storage = A::as_mut_slice(&mut self.storage);
159
2.40k
        }
160
161
2.40k
        storage[index..=self.len].rotate_right(1);
162
2.40k
        storage[index] = MaybeUninit::new(element);
163
2.40k
        self.len += 1;
164
2.40k
        Ok(())
165
2.61k
    }
166
167
15.7k
    pub fn pop(&mut self) -> Option<A::Item> {
168
15.7k
        if self.len == 0 {
169
0
            None
170
        } else {
171
15.7k
            self.len -= 1;
172
            // SAFETY: this element is valid and we "forget" it by setting the length.
173
15.7k
            Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() })
174
        }
175
15.7k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]>>::pop
Line
Count
Source
167
8.54k
    pub fn pop(&mut self) -> Option<A::Item> {
168
8.54k
        if self.len == 0 {
169
0
            None
170
        } else {
171
8.54k
            self.len -= 1;
172
            // SAFETY: this element is valid and we "forget" it by setting the length.
173
8.54k
            Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() })
174
        }
175
8.54k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>>>::pop
Line
Count
Source
167
7.17k
    pub fn pop(&mut self) -> Option<A::Item> {
168
7.17k
        if self.len == 0 {
169
0
            None
170
        } else {
171
7.17k
            self.len -= 1;
172
            // SAFETY: this element is valid and we "forget" it by setting the length.
173
7.17k
            Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() })
174
        }
175
7.17k
    }
176
177
8.54k
    pub fn swap_remove(&mut self, index: usize) -> A::Item {
178
8.54k
        assert!(index < self.len);
179
8.54k
        A::as_mut_slice(&mut self.storage).swap(index, self.len - 1);
180
8.54k
        self.pop().unwrap()
181
8.54k
    }
182
}
183
184
#[cfg(feature = "read")]
185
impl<T> ArrayVec<Vec<T>> {
186
    pub fn into_vec(mut self) -> Vec<T> {
187
        let len = core::mem::replace(&mut self.len, 0);
188
        let storage = core::mem::replace(&mut self.storage, Box::new([]));
189
        let slice = Box::leak(storage);
190
        debug_assert!(len <= slice.len());
191
        // SAFETY: valid elements.
192
        unsafe { Vec::from_raw_parts(slice.as_mut_ptr() as *mut T, len, slice.len()) }
193
    }
194
}
195
196
impl<A: ArrayLike> Drop for ArrayVec<A> {
197
68.7k
    fn drop(&mut self) {
198
68.7k
        self.clear();
199
68.7k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]> as core::ops::drop::Drop>::drop
Line
Count
Source
197
62.2k
    fn drop(&mut self) {
198
62.2k
        self.clear();
199
62.2k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>> as core::ops::drop::Drop>::drop
Line
Count
Source
197
6.45k
    fn drop(&mut self) {
198
6.45k
        self.clear();
199
6.45k
    }
200
}
201
202
impl<A: ArrayLike> Default for ArrayVec<A> {
203
68.7k
    fn default() -> Self {
204
68.7k
        Self::new()
205
68.7k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]> as core::default::Default>::default
Line
Count
Source
203
62.2k
    fn default() -> Self {
204
62.2k
        Self::new()
205
62.2k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>> as core::default::Default>::default
Line
Count
Source
203
6.45k
    fn default() -> Self {
204
6.45k
        Self::new()
205
6.45k
    }
206
}
207
208
impl<A: ArrayLike> ops::Deref for ArrayVec<A> {
209
    type Target = [A::Item];
210
211
971k
    fn deref(&self) -> &[A::Item] {
212
971k
        let slice = &A::as_slice(&self.storage);
213
971k
        debug_assert!(self.len <= slice.len());
214
        // SAFETY: valid elements.
215
971k
        unsafe { slice::from_raw_parts(slice.as_ptr() as _, self.len) }
216
971k
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]> as core::ops::deref::Deref>::deref
Line
Count
Source
211
215k
    fn deref(&self) -> &[A::Item] {
212
215k
        let slice = &A::as_slice(&self.storage);
213
215k
        debug_assert!(self.len <= slice.len());
214
        // SAFETY: valid elements.
215
215k
        unsafe { slice::from_raw_parts(slice.as_ptr() as _, self.len) }
216
215k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>> as core::ops::deref::Deref>::deref
Line
Count
Source
211
756k
    fn deref(&self) -> &[A::Item] {
212
756k
        let slice = &A::as_slice(&self.storage);
213
756k
        debug_assert!(self.len <= slice.len());
214
        // SAFETY: valid elements.
215
756k
        unsafe { slice::from_raw_parts(slice.as_ptr() as _, self.len) }
216
756k
    }
217
}
218
219
impl<A: ArrayLike> ops::DerefMut for ArrayVec<A> {
220
1.88M
    fn deref_mut(&mut self) -> &mut [A::Item] {
221
1.88M
        let slice = &mut A::as_mut_slice(&mut self.storage);
222
1.88M
        debug_assert!(self.len <= slice.len());
223
        // SAFETY: valid elements.
224
1.88M
        unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as _, self.len) }
225
1.88M
    }
<gimli::read::util::ArrayVec<[(gimli::common::Register, gimli::read::cfi::RegisterRule<usize>); 192]> as core::ops::deref::DerefMut>::deref_mut
Line
Count
Source
220
727k
    fn deref_mut(&mut self) -> &mut [A::Item] {
221
727k
        let slice = &mut A::as_mut_slice(&mut self.storage);
222
727k
        debug_assert!(self.len <= slice.len());
223
        // SAFETY: valid elements.
224
727k
        unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as _, self.len) }
225
727k
    }
<gimli::read::util::ArrayVec<alloc::boxed::Box<[gimli::read::cfi::UnwindTableRow<usize>; 4]>> as core::ops::deref::DerefMut>::deref_mut
Line
Count
Source
220
1.16M
    fn deref_mut(&mut self) -> &mut [A::Item] {
221
1.16M
        let slice = &mut A::as_mut_slice(&mut self.storage);
222
1.16M
        debug_assert!(self.len <= slice.len());
223
        // SAFETY: valid elements.
224
1.16M
        unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as _, self.len) }
225
1.16M
    }
226
}
227
228
impl<A: ArrayLike> Clone for ArrayVec<A>
229
where
230
    A::Item: Clone,
231
{
232
30.2k
    fn clone(&self) -> Self {
233
30.2k
        let mut new = Self::default();
234
435k
        for value in &**self {
235
435k
            new.try_push(value.clone()).unwrap();
236
435k
        }
237
30.2k
        new
238
30.2k
    }
239
}
240
241
impl<A: ArrayLike> PartialEq for ArrayVec<A>
242
where
243
    A::Item: PartialEq,
244
{
245
    fn eq(&self, other: &Self) -> bool {
246
        **self == **other
247
    }
248
}
249
250
impl<A: ArrayLike> Eq for ArrayVec<A> where A::Item: Eq {}
251
252
impl<A: ArrayLike> fmt::Debug for ArrayVec<A>
253
where
254
    A::Item: fmt::Debug,
255
{
256
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257
        fmt::Debug::fmt(&**self, f)
258
    }
259
}