/rust/registry/src/index.crates.io-6f17d22bba15001f/gimli-0.29.0/src/read/util.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | fn grow(_storage: &mut Self::Storage, _additional: usize) -> Result<(), CapacityFull> { |
20 | 0 | Err(CapacityFull) |
21 | 0 | } |
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 | | // Use macro since const generics can't be used due to MSRV. |
45 | | macro_rules! impl_array { |
46 | | () => {}; |
47 | | ($n:literal $($rest:tt)*) => { |
48 | | // SAFETY: does not modify the content in storage. |
49 | | unsafe impl<T> Sealed for [T; $n] { |
50 | | type Storage = [MaybeUninit<T>; $n]; |
51 | | |
52 | 0 | fn new_storage() -> Self::Storage { |
53 | 0 | // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. |
54 | 0 | unsafe { MaybeUninit::uninit().assume_init() } |
55 | 0 | } Unexecuted instantiation: <[_; 0] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 1] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 2] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 3] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 4] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 8] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 16] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 32] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 64] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 128] as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <[_; 192] as gimli::read::util::sealed::Sealed>::new_storage |
56 | | } |
57 | | |
58 | | impl<T> ArrayLike for [T; $n] { |
59 | | type Item = T; |
60 | | |
61 | 0 | fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] { |
62 | 0 | storage |
63 | 0 | } Unexecuted instantiation: <[_; 0] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 1] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 2] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 3] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 4] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 8] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 16] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 32] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 64] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 128] as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <[_; 192] as gimli::read::util::ArrayLike>::as_slice |
64 | | |
65 | 0 | fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] { |
66 | 0 | storage |
67 | 0 | } Unexecuted instantiation: <[_; 0] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 1] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 2] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 3] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 4] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 8] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 16] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 32] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 64] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 128] as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <[_; 192] as gimli::read::util::ArrayLike>::as_mut_slice |
68 | | } |
69 | | |
70 | | impl_array!($($rest)*); |
71 | | } |
72 | | } |
73 | | |
74 | | #[cfg(feature = "read")] |
75 | | macro_rules! impl_box { |
76 | | () => {}; |
77 | | ($n:literal $($rest:tt)*) => { |
78 | | // SAFETY: does not modify the content in storage. |
79 | | unsafe impl<T> Sealed for Box<[T; $n]> { |
80 | | type Storage = Box<[MaybeUninit<T>; $n]>; |
81 | | |
82 | 0 | fn new_storage() -> Self::Storage { |
83 | 0 | // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. |
84 | 0 | Box::new(unsafe { MaybeUninit::uninit().assume_init() }) |
85 | 0 | } Unexecuted instantiation: <alloc::boxed::Box<[_; 0]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 1]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 2]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 3]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 4]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 8]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 16]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 32]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 64]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 128]> as gimli::read::util::sealed::Sealed>::new_storage Unexecuted instantiation: <alloc::boxed::Box<[_; 192]> as gimli::read::util::sealed::Sealed>::new_storage |
86 | | } |
87 | | |
88 | | impl<T> ArrayLike for Box<[T; $n]> { |
89 | | type Item = T; |
90 | | |
91 | 0 | fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] { |
92 | 0 | &storage[..] |
93 | 0 | } Unexecuted instantiation: <alloc::boxed::Box<[_; 0]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 1]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 2]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 3]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 4]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 8]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 16]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 32]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 64]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 128]> as gimli::read::util::ArrayLike>::as_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 192]> as gimli::read::util::ArrayLike>::as_slice |
94 | | |
95 | 0 | fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] { |
96 | 0 | &mut storage[..] |
97 | 0 | } Unexecuted instantiation: <alloc::boxed::Box<[_; 0]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 1]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 2]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 3]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 4]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 8]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 16]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 32]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 64]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 128]> as gimli::read::util::ArrayLike>::as_mut_slice Unexecuted instantiation: <alloc::boxed::Box<[_; 192]> as gimli::read::util::ArrayLike>::as_mut_slice |
98 | | } |
99 | | |
100 | | impl_box!($($rest)*); |
101 | | } |
102 | | } |
103 | | |
104 | | impl_array!(0 1 2 3 4 8 16 32 64 128 192); |
105 | | #[cfg(feature = "read")] |
106 | | impl_box!(0 1 2 3 4 8 16 32 64 128 192); |
107 | | |
108 | | #[cfg(feature = "read")] |
109 | | unsafe impl<T> Sealed for Vec<T> { |
110 | | type Storage = Box<[MaybeUninit<T>]>; |
111 | | |
112 | 0 | fn new_storage() -> Self::Storage { |
113 | 0 | Box::new([]) |
114 | 0 | } |
115 | | |
116 | 0 | fn grow(storage: &mut Self::Storage, additional: usize) -> Result<(), CapacityFull> { |
117 | 0 | let mut vec: Vec<_> = core::mem::replace(storage, Box::new([])).into(); |
118 | 0 | vec.reserve(additional); |
119 | 0 | // SAFETY: This is a `Vec` of `MaybeUninit`. |
120 | 0 | unsafe { vec.set_len(vec.capacity()) }; |
121 | 0 | *storage = vec.into_boxed_slice(); |
122 | 0 | Ok(()) |
123 | 0 | } |
124 | | } |
125 | | |
126 | | #[cfg(feature = "read")] |
127 | | impl<T> ArrayLike for Vec<T> { |
128 | | type Item = T; |
129 | | |
130 | 0 | fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] { |
131 | 0 | storage |
132 | 0 | } |
133 | | |
134 | 0 | fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] { |
135 | 0 | storage |
136 | 0 | } |
137 | | } |
138 | | |
139 | | pub(crate) struct ArrayVec<A: ArrayLike> { |
140 | | storage: A::Storage, |
141 | | len: usize, |
142 | | } |
143 | | |
144 | | impl<A: ArrayLike> ArrayVec<A> { |
145 | 0 | pub fn new() -> Self { |
146 | 0 | Self { |
147 | 0 | storage: A::new_storage(), |
148 | 0 | len: 0, |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | 0 | pub fn clear(&mut self) { |
153 | 0 | let ptr: *mut [A::Item] = &mut **self; |
154 | 0 | // Set length first so the type invariant is upheld even if `drop_in_place` panicks. |
155 | 0 | self.len = 0; |
156 | 0 | // SAFETY: `ptr` contains valid elements only and we "forget" them by setting the length. |
157 | 0 | unsafe { ptr::drop_in_place(ptr) }; |
158 | 0 | } |
159 | | |
160 | 0 | pub fn try_push(&mut self, value: A::Item) -> Result<(), CapacityFull> { |
161 | 0 | let mut storage = A::as_mut_slice(&mut self.storage); |
162 | 0 | if self.len >= storage.len() { |
163 | 0 | A::grow(&mut self.storage, 1)?; |
164 | 0 | storage = A::as_mut_slice(&mut self.storage); |
165 | 0 | } |
166 | | |
167 | 0 | storage[self.len] = MaybeUninit::new(value); |
168 | 0 | self.len += 1; |
169 | 0 | Ok(()) |
170 | 0 | } |
171 | | |
172 | 0 | pub fn try_insert(&mut self, index: usize, element: A::Item) -> Result<(), CapacityFull> { |
173 | 0 | assert!(index <= self.len); |
174 | | |
175 | 0 | let mut storage = A::as_mut_slice(&mut self.storage); |
176 | 0 | if self.len >= storage.len() { |
177 | 0 | A::grow(&mut self.storage, 1)?; |
178 | 0 | storage = A::as_mut_slice(&mut self.storage); |
179 | 0 | } |
180 | | |
181 | | // SAFETY: storage[index] is filled later. |
182 | 0 | unsafe { |
183 | 0 | let p = storage.as_mut_ptr().add(index); |
184 | 0 | core::ptr::copy(p as *const _, p.add(1), self.len - index); |
185 | 0 | } |
186 | 0 | storage[index] = MaybeUninit::new(element); |
187 | 0 | self.len += 1; |
188 | 0 | Ok(()) |
189 | 0 | } |
190 | | |
191 | 0 | pub fn pop(&mut self) -> Option<A::Item> { |
192 | 0 | if self.len == 0 { |
193 | 0 | None |
194 | | } else { |
195 | 0 | self.len -= 1; |
196 | 0 | // SAFETY: this element is valid and we "forget" it by setting the length. |
197 | 0 | Some(unsafe { A::as_slice(&self.storage)[self.len].as_ptr().read() }) |
198 | | } |
199 | 0 | } |
200 | | |
201 | 0 | pub fn swap_remove(&mut self, index: usize) -> A::Item { |
202 | 0 | assert!(self.len > 0); |
203 | 0 | A::as_mut_slice(&mut self.storage).swap(index, self.len - 1); |
204 | 0 | self.pop().unwrap() |
205 | 0 | } |
206 | | } |
207 | | |
208 | | #[cfg(feature = "read")] |
209 | | impl<T> ArrayVec<Vec<T>> { |
210 | 0 | pub fn into_vec(mut self) -> Vec<T> { |
211 | 0 | let len = core::mem::replace(&mut self.len, 0); |
212 | 0 | let storage = core::mem::replace(&mut self.storage, Box::new([])); |
213 | 0 | let slice = Box::leak(storage); |
214 | 0 | debug_assert!(len <= slice.len()); |
215 | | // SAFETY: valid elements. |
216 | 0 | unsafe { Vec::from_raw_parts(slice.as_mut_ptr() as *mut T, len, slice.len()) } |
217 | 0 | } |
218 | | } |
219 | | |
220 | | impl<A: ArrayLike> Drop for ArrayVec<A> { |
221 | 0 | fn drop(&mut self) { |
222 | 0 | self.clear(); |
223 | 0 | } |
224 | | } |
225 | | |
226 | | impl<A: ArrayLike> Default for ArrayVec<A> { |
227 | 0 | fn default() -> Self { |
228 | 0 | Self::new() |
229 | 0 | } |
230 | | } |
231 | | |
232 | | impl<A: ArrayLike> ops::Deref for ArrayVec<A> { |
233 | | type Target = [A::Item]; |
234 | | |
235 | 0 | fn deref(&self) -> &[A::Item] { |
236 | 0 | let slice = &A::as_slice(&self.storage); |
237 | 0 | debug_assert!(self.len <= slice.len()); |
238 | | // SAFETY: valid elements. |
239 | 0 | unsafe { slice::from_raw_parts(slice.as_ptr() as _, self.len) } |
240 | 0 | } |
241 | | } |
242 | | |
243 | | impl<A: ArrayLike> ops::DerefMut for ArrayVec<A> { |
244 | 0 | fn deref_mut(&mut self) -> &mut [A::Item] { |
245 | 0 | let slice = &mut A::as_mut_slice(&mut self.storage); |
246 | 0 | debug_assert!(self.len <= slice.len()); |
247 | | // SAFETY: valid elements. |
248 | 0 | unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as _, self.len) } |
249 | 0 | } |
250 | | } |
251 | | |
252 | | impl<A: ArrayLike> Clone for ArrayVec<A> |
253 | | where |
254 | | A::Item: Clone, |
255 | | { |
256 | 0 | fn clone(&self) -> Self { |
257 | 0 | let mut new = Self::default(); |
258 | 0 | for value in &**self { |
259 | 0 | new.try_push(value.clone()).unwrap(); |
260 | 0 | } |
261 | 0 | new |
262 | 0 | } |
263 | | } |
264 | | |
265 | | impl<A: ArrayLike> PartialEq for ArrayVec<A> |
266 | | where |
267 | | A::Item: PartialEq, |
268 | | { |
269 | 0 | fn eq(&self, other: &Self) -> bool { |
270 | 0 | **self == **other |
271 | 0 | } |
272 | | } |
273 | | |
274 | | impl<A: ArrayLike> Eq for ArrayVec<A> where A::Item: Eq {} |
275 | | |
276 | | impl<A: ArrayLike> fmt::Debug for ArrayVec<A> |
277 | | where |
278 | | A::Item: fmt::Debug, |
279 | | { |
280 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
281 | 0 | fmt::Debug::fmt(&**self, f) |
282 | 0 | } |
283 | | } |