/rust/registry/src/index.crates.io-1949cf8c6b5b557f/smallvec-1.15.2/src/lib.rs
Line | Count | Source |
1 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
2 | | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
3 | | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
4 | | // option. This file may not be copied, modified, or distributed |
5 | | // except according to those terms. |
6 | | |
7 | | //! Small vectors in various sizes. These store a certain number of elements inline, and fall back |
8 | | //! to the heap for larger allocations. This can be a useful optimization for improving cache |
9 | | //! locality and reducing allocator traffic for workloads that fit within the inline buffer. |
10 | | //! |
11 | | //! ## `no_std` support |
12 | | //! |
13 | | //! By default, `smallvec` does not depend on `std`. However, the optional |
14 | | //! `write` feature implements the `std::io::Write` trait for vectors of `u8`. |
15 | | //! When this feature is enabled, `smallvec` depends on `std`. |
16 | | //! |
17 | | //! ## Optional features |
18 | | //! |
19 | | //! ### `serde` |
20 | | //! |
21 | | //! When this optional dependency is enabled, `SmallVec` implements the `serde::Serialize` and |
22 | | //! `serde::Deserialize` traits. |
23 | | //! |
24 | | //! ### `write` |
25 | | //! |
26 | | //! When this feature is enabled, `SmallVec<[u8; _]>` implements the `std::io::Write` trait. |
27 | | //! This feature is not compatible with `#![no_std]` programs. |
28 | | //! |
29 | | //! ### `union` |
30 | | //! |
31 | | //! **This feature requires Rust 1.49.** |
32 | | //! |
33 | | //! When the `union` feature is enabled `smallvec` will track its state (inline or spilled) |
34 | | //! without the use of an enum tag, reducing the size of the `smallvec` by one machine word. |
35 | | //! This means that there is potentially no space overhead compared to `Vec`. |
36 | | //! Note that `smallvec` can still be larger than `Vec` if the inline buffer is larger than two |
37 | | //! machine words. |
38 | | //! |
39 | | //! To use this feature add `features = ["union"]` in the `smallvec` section of Cargo.toml. |
40 | | //! Note that this feature requires Rust 1.49. |
41 | | //! |
42 | | //! Tracking issue: [rust-lang/rust#55149](https://github.com/rust-lang/rust/issues/55149) |
43 | | //! |
44 | | //! ### `const_generics` |
45 | | //! |
46 | | //! **This feature requires Rust 1.51.** |
47 | | //! |
48 | | //! When this feature is enabled, `SmallVec` works with any arrays of any size, not just a fixed |
49 | | //! list of sizes. |
50 | | //! |
51 | | //! ### `const_new` |
52 | | //! |
53 | | //! **This feature requires Rust 1.51.** |
54 | | //! |
55 | | //! This feature exposes the functions [`SmallVec::new_const`], [`SmallVec::from_const`], and [`smallvec_inline`] which enables the `SmallVec` to be initialized from a const context. |
56 | | //! For details, see the |
57 | | //! [Rust Reference](https://doc.rust-lang.org/reference/const_eval.html#const-functions). |
58 | | //! |
59 | | //! ### `drain_filter` |
60 | | //! |
61 | | //! **This feature is unstable.** It may change to match the unstable `drain_filter` method in libstd. |
62 | | //! |
63 | | //! Enables the `drain_filter` method, which produces an iterator that calls a user-provided |
64 | | //! closure to determine which elements of the vector to remove and yield from the iterator. |
65 | | //! |
66 | | //! ### `drain_keep_rest` |
67 | | //! |
68 | | //! **This feature is unstable.** It may change to match the unstable `drain_keep_rest` method in libstd. |
69 | | //! |
70 | | //! Enables the `DrainFilter::keep_rest` method. |
71 | | //! |
72 | | //! ### `specialization` |
73 | | //! |
74 | | //! **This feature is unstable and requires a nightly build of the Rust toolchain.** |
75 | | //! |
76 | | //! When this feature is enabled, `SmallVec::from(slice)` has improved performance for slices |
77 | | //! of `Copy` types. (Without this feature, you can use `SmallVec::from_slice` to get optimal |
78 | | //! performance for `Copy` types.) |
79 | | //! |
80 | | //! Tracking issue: [rust-lang/rust#31844](https://github.com/rust-lang/rust/issues/31844) |
81 | | //! |
82 | | //! ### `may_dangle` |
83 | | //! |
84 | | //! **This feature is unstable and requires a nightly build of the Rust toolchain.** |
85 | | //! |
86 | | //! This feature makes the Rust compiler less strict about use of vectors that contain borrowed |
87 | | //! references. For details, see the |
88 | | //! [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch). |
89 | | //! |
90 | | //! Tracking issue: [rust-lang/rust#34761](https://github.com/rust-lang/rust/issues/34761) |
91 | | |
92 | | #![no_std] |
93 | | #![cfg_attr(docsrs, feature(doc_cfg))] |
94 | | #![cfg_attr(feature = "specialization", allow(incomplete_features))] |
95 | | #![cfg_attr(feature = "specialization", feature(specialization))] |
96 | | #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))] |
97 | | #![cfg_attr( |
98 | | feature = "debugger_visualizer", |
99 | | feature(debugger_visualizer), |
100 | | debugger_visualizer(natvis_file = "../debug_metadata/smallvec.natvis") |
101 | | )] |
102 | | #![deny(missing_docs)] |
103 | | |
104 | | #[doc(hidden)] |
105 | | pub extern crate alloc; |
106 | | |
107 | | #[cfg(any(test, feature = "write"))] |
108 | | extern crate std; |
109 | | |
110 | | #[cfg(test)] |
111 | | mod tests; |
112 | | |
113 | | #[allow(deprecated)] |
114 | | use alloc::alloc::{Layout, LayoutErr}; |
115 | | use alloc::boxed::Box; |
116 | | use alloc::{vec, vec::Vec}; |
117 | | use core::borrow::{Borrow, BorrowMut}; |
118 | | use core::cmp; |
119 | | use core::fmt; |
120 | | use core::hash::{Hash, Hasher}; |
121 | | use core::hint::unreachable_unchecked; |
122 | | use core::iter::{repeat, FromIterator, FusedIterator, IntoIterator}; |
123 | | use core::mem; |
124 | | use core::mem::MaybeUninit; |
125 | | use core::ops::{self, Range, RangeBounds}; |
126 | | use core::ptr::{self, NonNull}; |
127 | | use core::slice::{self, SliceIndex}; |
128 | | |
129 | | #[cfg(feature = "malloc_size_of")] |
130 | | use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; |
131 | | |
132 | | #[cfg(feature = "serde")] |
133 | | use serde::{ |
134 | | de::{Deserialize, Deserializer, SeqAccess, Visitor}, |
135 | | ser::{Serialize, SerializeSeq, Serializer}, |
136 | | }; |
137 | | |
138 | | #[cfg(feature = "serde")] |
139 | | use core::marker::PhantomData; |
140 | | |
141 | | #[cfg(feature = "write")] |
142 | | use std::io; |
143 | | |
144 | | #[cfg(feature = "drain_keep_rest")] |
145 | | use core::mem::ManuallyDrop; |
146 | | |
147 | | /// Creates a [`SmallVec`] containing the arguments. |
148 | | /// |
149 | | /// `smallvec!` allows `SmallVec`s to be defined with the same syntax as array expressions. |
150 | | /// There are two forms of this macro: |
151 | | /// |
152 | | /// - Create a [`SmallVec`] containing a given list of elements: |
153 | | /// |
154 | | /// ``` |
155 | | /// # use smallvec::{smallvec, SmallVec}; |
156 | | /// # fn main() { |
157 | | /// let v: SmallVec<[_; 128]> = smallvec![1, 2, 3]; |
158 | | /// assert_eq!(v[0], 1); |
159 | | /// assert_eq!(v[1], 2); |
160 | | /// assert_eq!(v[2], 3); |
161 | | /// # } |
162 | | /// ``` |
163 | | /// |
164 | | /// - Create a [`SmallVec`] from a given element and size: |
165 | | /// |
166 | | /// ``` |
167 | | /// # use smallvec::{smallvec, SmallVec}; |
168 | | /// # fn main() { |
169 | | /// let v: SmallVec<[_; 10]> = smallvec![1; 3]; |
170 | | /// assert_eq!(v, SmallVec::from_buf([1, 1, 1])); |
171 | | /// # } |
172 | | /// ``` |
173 | | /// |
174 | | /// Note that unlike array expressions this syntax supports all elements |
175 | | /// which implement [`Clone`] and the number of elements doesn't have to be |
176 | | /// a constant. |
177 | | /// |
178 | | /// This will use `clone` to duplicate an expression, so one should be careful |
179 | | /// using this with types having a nonstandard `Clone` implementation. For |
180 | | /// example, `smallvec![Rc::new(1); 5]` will create a vector of five references |
181 | | /// to the same boxed integer value, not five references pointing to independently |
182 | | /// boxed integers. |
183 | | #[macro_export] |
184 | | macro_rules! smallvec { |
185 | | // count helper: transform any expression into 1 |
186 | | (@one $x:expr) => (1usize); |
187 | | () => ( |
188 | | $crate::SmallVec::new() |
189 | | ); |
190 | | ($elem:expr; $n:expr) => ({ |
191 | | $crate::SmallVec::from_elem($elem, $n) |
192 | | }); |
193 | | ($($x:expr),+$(,)?) => ({ |
194 | | let count = 0usize $(+ $crate::smallvec!(@one $x))+; |
195 | | let mut vec = $crate::SmallVec::new(); |
196 | | if count <= vec.inline_size() { |
197 | | $(vec.push($x);)* |
198 | | vec |
199 | | } else { |
200 | | $crate::SmallVec::from_vec($crate::alloc::vec![$($x,)+]) |
201 | | } |
202 | | }); |
203 | | } |
204 | | |
205 | | /// Creates an inline [`SmallVec`] containing the arguments. This macro is enabled by the feature `const_new`. |
206 | | /// |
207 | | /// `smallvec_inline!` allows `SmallVec`s to be defined with the same syntax as array expressions in `const` contexts. |
208 | | /// The inline storage `A` will always be an array of the size specified by the arguments. |
209 | | /// There are two forms of this macro: |
210 | | /// |
211 | | /// - Create a [`SmallVec`] containing a given list of elements: |
212 | | /// |
213 | | /// ``` |
214 | | /// # use smallvec::{smallvec_inline, SmallVec}; |
215 | | /// # fn main() { |
216 | | /// const V: SmallVec<[i32; 3]> = smallvec_inline![1, 2, 3]; |
217 | | /// assert_eq!(V[0], 1); |
218 | | /// assert_eq!(V[1], 2); |
219 | | /// assert_eq!(V[2], 3); |
220 | | /// # } |
221 | | /// ``` |
222 | | /// |
223 | | /// - Create a [`SmallVec`] from a given element and size: |
224 | | /// |
225 | | /// ``` |
226 | | /// # use smallvec::{smallvec_inline, SmallVec}; |
227 | | /// # fn main() { |
228 | | /// const V: SmallVec<[i32; 3]> = smallvec_inline![1; 3]; |
229 | | /// assert_eq!(V, SmallVec::from_buf([1, 1, 1])); |
230 | | /// # } |
231 | | /// ``` |
232 | | /// |
233 | | /// Note that the behavior mimics that of array expressions, in contrast to [`smallvec`]. |
234 | | #[cfg(feature = "const_new")] |
235 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
236 | | #[macro_export] |
237 | | macro_rules! smallvec_inline { |
238 | | // count helper: transform any expression into 1 |
239 | | (@one $x:expr) => (1usize); |
240 | | ($elem:expr; $n:expr) => ({ |
241 | | $crate::SmallVec::<[_; $n]>::from_const([$elem; $n]) |
242 | | }); |
243 | | ($($x:expr),+ $(,)?) => ({ |
244 | | const N: usize = 0usize $(+ $crate::smallvec_inline!(@one $x))*; |
245 | | $crate::SmallVec::<[_; N]>::from_const([$($x,)*]) |
246 | | }); |
247 | | } |
248 | | |
249 | | /// `panic!()` in debug builds, optimization hint in release. |
250 | | #[cfg(not(feature = "union"))] |
251 | | macro_rules! debug_unreachable { |
252 | | () => { |
253 | | debug_unreachable!("entered unreachable code") |
254 | | }; |
255 | | ($e:expr) => { |
256 | | if cfg!(debug_assertions) { |
257 | | panic!($e); |
258 | | } else { |
259 | | unreachable_unchecked(); |
260 | | } |
261 | | }; |
262 | | } |
263 | | |
264 | | /// Trait to be implemented by a collection that can be extended from a slice |
265 | | /// |
266 | | /// ## Example |
267 | | /// |
268 | | /// ```rust |
269 | | /// use smallvec::{ExtendFromSlice, SmallVec}; |
270 | | /// |
271 | | /// fn initialize<V: ExtendFromSlice<u8>>(v: &mut V) { |
272 | | /// v.extend_from_slice(b"Test!"); |
273 | | /// } |
274 | | /// |
275 | | /// let mut vec = Vec::new(); |
276 | | /// initialize(&mut vec); |
277 | | /// assert_eq!(&vec, b"Test!"); |
278 | | /// |
279 | | /// let mut small_vec = SmallVec::<[u8; 8]>::new(); |
280 | | /// initialize(&mut small_vec); |
281 | | /// assert_eq!(&small_vec as &[_], b"Test!"); |
282 | | /// ``` |
283 | | #[doc(hidden)] |
284 | | #[deprecated] |
285 | | pub trait ExtendFromSlice<T> { |
286 | | /// Extends a collection from a slice of its element type |
287 | | fn extend_from_slice(&mut self, other: &[T]); |
288 | | } |
289 | | |
290 | | #[allow(deprecated)] |
291 | | impl<T: Clone> ExtendFromSlice<T> for Vec<T> { |
292 | 0 | fn extend_from_slice(&mut self, other: &[T]) { |
293 | 0 | Vec::extend_from_slice(self, other) |
294 | 0 | } |
295 | | } |
296 | | |
297 | | /// Error type for APIs with fallible heap allocation |
298 | | #[derive(Debug)] |
299 | | pub enum CollectionAllocErr { |
300 | | /// Overflow `usize::MAX` or other error during size computation |
301 | | CapacityOverflow, |
302 | | /// The allocator return an error |
303 | | AllocErr { |
304 | | /// The layout that was passed to the allocator |
305 | | layout: Layout, |
306 | | }, |
307 | | } |
308 | | |
309 | | impl fmt::Display for CollectionAllocErr { |
310 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
311 | 0 | write!(f, "Allocation error: {:?}", self) |
312 | 0 | } |
313 | | } |
314 | | |
315 | | #[allow(deprecated)] |
316 | | impl From<LayoutErr> for CollectionAllocErr { |
317 | 0 | fn from(_: LayoutErr) -> Self { |
318 | 0 | CollectionAllocErr::CapacityOverflow |
319 | 0 | } |
320 | | } |
321 | | |
322 | 9.44M | fn infallible<T>(result: Result<T, CollectionAllocErr>) -> T { |
323 | 0 | match result { |
324 | 9.44M | Ok(x) => x, |
325 | 0 | Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), |
326 | 0 | Err(CollectionAllocErr::AllocErr { layout }) => alloc::alloc::handle_alloc_error(layout), |
327 | | } |
328 | 9.44M | } smallvec::infallible::<()> Line | Count | Source | 322 | 9.44M | fn infallible<T>(result: Result<T, CollectionAllocErr>) -> T { | 323 | 0 | match result { | 324 | 9.44M | Ok(x) => x, | 325 | 0 | Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), | 326 | 0 | Err(CollectionAllocErr::AllocErr { layout }) => alloc::alloc::handle_alloc_error(layout), | 327 | | } | 328 | 9.44M | } |
Unexecuted instantiation: smallvec::infallible::<_> |
329 | | |
330 | | /// FIXME: use `Layout::array` when we require a Rust version where it’s stable |
331 | | /// <https://github.com/rust-lang/rust/issues/55724> |
332 | 1.21M | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { |
333 | 1.21M | let size = mem::size_of::<T>() |
334 | 1.21M | .checked_mul(n) |
335 | 1.21M | .ok_or(CollectionAllocErr::CapacityOverflow)?; |
336 | 1.21M | let align = mem::align_of::<T>(); |
337 | 1.21M | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) |
338 | 1.21M | } smallvec::layout_array::<alloc::vec::Vec<u64>> Line | Count | Source | 332 | 2.58k | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | 333 | 2.58k | let size = mem::size_of::<T>() | 334 | 2.58k | .checked_mul(n) | 335 | 2.58k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 336 | 2.58k | let align = mem::align_of::<T>(); | 337 | 2.58k | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | 338 | 2.58k | } |
smallvec::layout_array::<exr::meta::header::Header> Line | Count | Source | 332 | 5.69k | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | 333 | 5.69k | let size = mem::size_of::<T>() | 334 | 5.69k | .checked_mul(n) | 335 | 5.69k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 336 | 5.69k | let align = mem::align_of::<T>(); | 337 | 5.69k | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | 338 | 5.69k | } |
smallvec::layout_array::<exr::meta::attribute::ChannelDescription> Line | Count | Source | 332 | 5.45k | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | 333 | 5.45k | let size = mem::size_of::<T>() | 334 | 5.45k | .checked_mul(n) | 335 | 5.45k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 336 | 5.45k | let align = mem::align_of::<T>(); | 337 | 5.45k | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | 338 | 5.45k | } |
Unexecuted instantiation: smallvec::layout_array::<exr::image::AnyChannel<exr::image::FlatSamples>> Unexecuted instantiation: smallvec::layout_array::<exr::compression::piz::ChannelData> Unexecuted instantiation: smallvec::layout_array::<exr::block::samples::Sample> smallvec::layout_array::<u8> Line | Count | Source | 332 | 1.20M | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | 333 | 1.20M | let size = mem::size_of::<T>() | 334 | 1.20M | .checked_mul(n) | 335 | 1.20M | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 336 | 1.20M | let align = mem::align_of::<T>(); | 337 | 1.20M | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | 338 | 1.20M | } |
Unexecuted instantiation: smallvec::layout_array::<usize> smallvec::layout_array::<u32> Line | Count | Source | 332 | 302 | fn layout_array<T>(n: usize) -> Result<Layout, CollectionAllocErr> { | 333 | 302 | let size = mem::size_of::<T>() | 334 | 302 | .checked_mul(n) | 335 | 302 | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 336 | 302 | let align = mem::align_of::<T>(); | 337 | 302 | Layout::from_size_align(size, align).map_err(|_| CollectionAllocErr::CapacityOverflow) | 338 | 302 | } |
Unexecuted instantiation: smallvec::layout_array::<_> |
339 | | |
340 | 0 | unsafe fn deallocate<T>(ptr: NonNull<T>, capacity: usize) { |
341 | | // This unwrap should succeed since the same did when allocating. |
342 | 0 | let layout = layout_array::<T>(capacity).unwrap(); |
343 | 0 | alloc::alloc::dealloc(ptr.as_ptr() as *mut u8, layout) |
344 | 0 | } Unexecuted instantiation: smallvec::deallocate::<alloc::vec::Vec<u64>> Unexecuted instantiation: smallvec::deallocate::<exr::meta::header::Header> Unexecuted instantiation: smallvec::deallocate::<exr::meta::attribute::ChannelDescription> Unexecuted instantiation: smallvec::deallocate::<exr::image::AnyChannel<exr::image::FlatSamples>> Unexecuted instantiation: smallvec::deallocate::<exr::compression::piz::ChannelData> Unexecuted instantiation: smallvec::deallocate::<exr::block::samples::Sample> Unexecuted instantiation: smallvec::deallocate::<u8> Unexecuted instantiation: smallvec::deallocate::<usize> Unexecuted instantiation: smallvec::deallocate::<u32> Unexecuted instantiation: smallvec::deallocate::<_> |
345 | | |
346 | | /// An iterator that removes the items from a `SmallVec` and yields them by value. |
347 | | /// |
348 | | /// Returned from [`SmallVec::drain`][1]. |
349 | | /// |
350 | | /// [1]: struct.SmallVec.html#method.drain |
351 | | pub struct Drain<'a, T: 'a + Array> { |
352 | | tail_start: usize, |
353 | | tail_len: usize, |
354 | | iter: slice::Iter<'a, T::Item>, |
355 | | vec: NonNull<SmallVec<T>>, |
356 | | } |
357 | | |
358 | | impl<'a, T: 'a + Array> fmt::Debug for Drain<'a, T> |
359 | | where |
360 | | T::Item: fmt::Debug, |
361 | | { |
362 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
363 | 0 | f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() |
364 | 0 | } |
365 | | } |
366 | | |
367 | | unsafe impl<'a, T: Sync + Array> Sync for Drain<'a, T> {} |
368 | | unsafe impl<'a, T: Send + Array> Send for Drain<'a, T> {} |
369 | | |
370 | | impl<'a, T: 'a + Array> Iterator for Drain<'a, T> { |
371 | | type Item = T::Item; |
372 | | |
373 | | #[inline] |
374 | 0 | fn next(&mut self) -> Option<T::Item> { |
375 | 0 | self.iter |
376 | 0 | .next() |
377 | 0 | .map(|reference| unsafe { ptr::read(reference) }) |
378 | 0 | } |
379 | | |
380 | | #[inline] |
381 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
382 | 0 | self.iter.size_hint() |
383 | 0 | } |
384 | | } |
385 | | |
386 | | impl<'a, T: 'a + Array> DoubleEndedIterator for Drain<'a, T> { |
387 | | #[inline] |
388 | 0 | fn next_back(&mut self) -> Option<T::Item> { |
389 | 0 | self.iter |
390 | 0 | .next_back() |
391 | 0 | .map(|reference| unsafe { ptr::read(reference) }) |
392 | 0 | } |
393 | | } |
394 | | |
395 | | impl<'a, T: Array> ExactSizeIterator for Drain<'a, T> { |
396 | | #[inline] |
397 | 0 | fn len(&self) -> usize { |
398 | 0 | self.iter.len() |
399 | 0 | } |
400 | | } |
401 | | |
402 | | impl<'a, T: Array> FusedIterator for Drain<'a, T> {} |
403 | | |
404 | | impl<'a, T: 'a + Array> Drop for Drain<'a, T> { |
405 | 0 | fn drop(&mut self) { |
406 | 0 | self.for_each(drop); |
407 | | |
408 | 0 | if self.tail_len > 0 { |
409 | | unsafe { |
410 | 0 | let source_vec = self.vec.as_mut(); |
411 | | |
412 | | // memmove back untouched tail, update to new length |
413 | 0 | let start = source_vec.len(); |
414 | 0 | let tail = self.tail_start; |
415 | 0 | if tail != start { |
416 | 0 | // as_mut_ptr creates a &mut, invalidating other pointers. |
417 | 0 | // This pattern avoids calling it with a pointer already present. |
418 | 0 | let ptr = source_vec.as_mut_ptr(); |
419 | 0 | let src = ptr.add(tail); |
420 | 0 | let dst = ptr.add(start); |
421 | 0 | ptr::copy(src, dst, self.tail_len); |
422 | 0 | } |
423 | 0 | source_vec.set_len(start + self.tail_len); |
424 | | } |
425 | 0 | } |
426 | 0 | } |
427 | | } |
428 | | |
429 | | #[cfg(feature = "drain_filter")] |
430 | | /// An iterator which uses a closure to determine if an element should be removed. |
431 | | /// |
432 | | /// Returned from [`SmallVec::drain_filter`][1]. |
433 | | /// |
434 | | /// [1]: struct.SmallVec.html#method.drain_filter |
435 | | pub struct DrainFilter<'a, T, F> |
436 | | where |
437 | | F: FnMut(&mut T::Item) -> bool, |
438 | | T: Array, |
439 | | { |
440 | | vec: &'a mut SmallVec<T>, |
441 | | /// The index of the item that will be inspected by the next call to `next`. |
442 | | idx: usize, |
443 | | /// The number of items that have been drained (removed) thus far. |
444 | | del: usize, |
445 | | /// The original length of `vec` prior to draining. |
446 | | old_len: usize, |
447 | | /// The filter test predicate. |
448 | | pred: F, |
449 | | /// A flag that indicates a panic has occurred in the filter test predicate. |
450 | | /// This is used as a hint in the drop implementation to prevent consumption |
451 | | /// of the remainder of the `DrainFilter`. Any unprocessed items will be |
452 | | /// backshifted in the `vec`, but no further items will be dropped or |
453 | | /// tested by the filter predicate. |
454 | | panic_flag: bool, |
455 | | } |
456 | | |
457 | | #[cfg(feature = "drain_filter")] |
458 | | impl <T, F> fmt::Debug for DrainFilter<'_, T, F> |
459 | | where |
460 | | F: FnMut(&mut T::Item) -> bool, |
461 | | T: Array, |
462 | | T::Item: fmt::Debug, |
463 | | { |
464 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
465 | | f.debug_tuple("DrainFilter").field(&self.vec.as_slice()).finish() |
466 | | } |
467 | | } |
468 | | |
469 | | #[cfg(feature = "drain_filter")] |
470 | | impl <T, F> Iterator for DrainFilter<'_, T, F> |
471 | | where |
472 | | F: FnMut(&mut T::Item) -> bool, |
473 | | T: Array, |
474 | | { |
475 | | type Item = T::Item; |
476 | | |
477 | | fn next(&mut self) -> Option<T::Item> |
478 | | { |
479 | | unsafe { |
480 | | while self.idx < self.old_len { |
481 | | let i = self.idx; |
482 | | let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len); |
483 | | self.panic_flag = true; |
484 | | let drained = (self.pred)(&mut v[i]); |
485 | | self.panic_flag = false; |
486 | | // Update the index *after* the predicate is called. If the index |
487 | | // is updated prior and the predicate panics, the element at this |
488 | | // index would be leaked. |
489 | | self.idx += 1; |
490 | | if drained { |
491 | | self.del += 1; |
492 | | return Some(ptr::read(&v[i])); |
493 | | } else if self.del > 0 { |
494 | | let del = self.del; |
495 | | let src: *const Self::Item = &v[i]; |
496 | | let dst: *mut Self::Item = &mut v[i - del]; |
497 | | ptr::copy_nonoverlapping(src, dst, 1); |
498 | | } |
499 | | } |
500 | | None |
501 | | } |
502 | | } |
503 | | |
504 | | fn size_hint(&self) -> (usize, Option<usize>) { |
505 | | (0, Some(self.old_len - self.idx)) |
506 | | } |
507 | | } |
508 | | |
509 | | #[cfg(feature = "drain_filter")] |
510 | | impl <T, F> Drop for DrainFilter<'_, T, F> |
511 | | where |
512 | | F: FnMut(&mut T::Item) -> bool, |
513 | | T: Array, |
514 | | { |
515 | | fn drop(&mut self) { |
516 | | struct BackshiftOnDrop<'a, 'b, T, F> |
517 | | where |
518 | | F: FnMut(&mut T::Item) -> bool, |
519 | | T: Array |
520 | | { |
521 | | drain: &'b mut DrainFilter<'a, T, F>, |
522 | | } |
523 | | |
524 | | impl<'a, 'b, T, F> Drop for BackshiftOnDrop<'a, 'b, T, F> |
525 | | where |
526 | | F: FnMut(&mut T::Item) -> bool, |
527 | | T: Array |
528 | | { |
529 | | fn drop(&mut self) { |
530 | | unsafe { |
531 | | if self.drain.idx < self.drain.old_len && self.drain.del > 0 { |
532 | | // This is a pretty messed up state, and there isn't really an |
533 | | // obviously right thing to do. We don't want to keep trying |
534 | | // to execute `pred`, so we just backshift all the unprocessed |
535 | | // elements and tell the vec that they still exist. The backshift |
536 | | // is required to prevent a double-drop of the last successfully |
537 | | // drained item prior to a panic in the predicate. |
538 | | let ptr = self.drain.vec.as_mut_ptr(); |
539 | | let src = ptr.add(self.drain.idx); |
540 | | let dst = src.sub(self.drain.del); |
541 | | let tail_len = self.drain.old_len - self.drain.idx; |
542 | | src.copy_to(dst, tail_len); |
543 | | } |
544 | | self.drain.vec.set_len(self.drain.old_len - self.drain.del); |
545 | | } |
546 | | } |
547 | | } |
548 | | |
549 | | let backshift = BackshiftOnDrop { drain: self }; |
550 | | |
551 | | // Attempt to consume any remaining elements if the filter predicate |
552 | | // has not yet panicked. We'll backshift any remaining elements |
553 | | // whether we've already panicked or if the consumption here panics. |
554 | | if !backshift.drain.panic_flag { |
555 | | backshift.drain.for_each(drop); |
556 | | } |
557 | | } |
558 | | } |
559 | | |
560 | | #[cfg(feature = "drain_keep_rest")] |
561 | | impl <T, F> DrainFilter<'_, T, F> |
562 | | where |
563 | | F: FnMut(&mut T::Item) -> bool, |
564 | | T: Array |
565 | | { |
566 | | /// Keep unyielded elements in the source `Vec`. |
567 | | /// |
568 | | /// # Examples |
569 | | /// |
570 | | /// ``` |
571 | | /// # use smallvec::{smallvec, SmallVec}; |
572 | | /// |
573 | | /// let mut vec: SmallVec<[char; 2]> = smallvec!['a', 'b', 'c']; |
574 | | /// let mut drain = vec.drain_filter(|_| true); |
575 | | /// |
576 | | /// assert_eq!(drain.next().unwrap(), 'a'); |
577 | | /// |
578 | | /// // This call keeps 'b' and 'c' in the vec. |
579 | | /// drain.keep_rest(); |
580 | | /// |
581 | | /// // If we wouldn't call `keep_rest()`, |
582 | | /// // `vec` would be empty. |
583 | | /// assert_eq!(vec, SmallVec::<[char; 2]>::from_slice(&['b', 'c'])); |
584 | | /// ``` |
585 | | pub fn keep_rest(self) |
586 | | { |
587 | | // At this moment layout looks like this: |
588 | | // |
589 | | // _____________________/-- old_len |
590 | | // / \ |
591 | | // [kept] [yielded] [tail] |
592 | | // \_______/ ^-- idx |
593 | | // \-- del |
594 | | // |
595 | | // Normally `Drop` impl would drop [tail] (via .for_each(drop), ie still calling `pred`) |
596 | | // |
597 | | // 1. Move [tail] after [kept] |
598 | | // 2. Update length of the original vec to `old_len - del` |
599 | | // a. In case of ZST, this is the only thing we want to do |
600 | | // 3. Do *not* drop self, as everything is put in a consistent state already, there is nothing to do |
601 | | let mut this = ManuallyDrop::new(self); |
602 | | |
603 | | unsafe { |
604 | | // ZSTs have no identity, so we don't need to move them around. |
605 | | let needs_move = mem::size_of::<T::Item>() != 0; |
606 | | |
607 | | if needs_move && this.idx < this.old_len && this.del > 0 { |
608 | | let ptr = this.vec.as_mut_ptr(); |
609 | | let src = ptr.add(this.idx); |
610 | | let dst = src.sub(this.del); |
611 | | let tail_len = this.old_len - this.idx; |
612 | | src.copy_to(dst, tail_len); |
613 | | } |
614 | | |
615 | | let new_len = this.old_len - this.del; |
616 | | this.vec.set_len(new_len); |
617 | | } |
618 | | } |
619 | | } |
620 | | |
621 | | #[cfg(feature = "union")] |
622 | | union SmallVecData<A: Array> { |
623 | | inline: core::mem::ManuallyDrop<MaybeUninit<A>>, |
624 | | heap: (NonNull<A::Item>, usize), |
625 | | } |
626 | | |
627 | | #[cfg(all(feature = "union", feature = "const_new"))] |
628 | | impl<T, const N: usize> SmallVecData<[T; N]> { |
629 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
630 | | #[inline] |
631 | | const fn from_const(inline: MaybeUninit<[T; N]>) -> Self { |
632 | | SmallVecData { |
633 | | inline: core::mem::ManuallyDrop::new(inline), |
634 | | } |
635 | | } |
636 | | } |
637 | | |
638 | | #[cfg(feature = "union")] |
639 | | impl<A: Array> SmallVecData<A> { |
640 | | #[inline] |
641 | | unsafe fn inline(&self) -> ConstNonNull<A::Item> { |
642 | | ConstNonNull::new(self.inline.as_ptr() as *const A::Item).unwrap() |
643 | | } |
644 | | #[inline] |
645 | | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { |
646 | | NonNull::new(self.inline.as_mut_ptr() as *mut A::Item).unwrap() |
647 | | } |
648 | | #[inline] |
649 | | fn from_inline(inline: MaybeUninit<A>) -> SmallVecData<A> { |
650 | | SmallVecData { |
651 | | inline: core::mem::ManuallyDrop::new(inline), |
652 | | } |
653 | | } |
654 | | // Workaround for https://github.com/rust-lang/rust/issues/157743: when from_inline is |
655 | | // called with MaybeUninit::uninit(), rustc 1.93+ GVN propagates const <uninit> into the |
656 | | // ManuallyDrop::new() aggregate, causing LLVM to materialize a global constant that |
657 | | // MemCpyOpt then collapses into a memset over the whole struct. Using assume_init() of a |
658 | | // doubly-wrapped MaybeUninit produces Immediate::Uninit instead of const <uninit>, which |
659 | | // codegen handles as undef without emitting any global. This function also avoids |
660 | | // introducing an intermediate local that would inflate stack frames in debug builds. |
661 | | #[inline] |
662 | | fn empty() -> SmallVecData<A> { |
663 | | // SAFETY: ManuallyDrop<MaybeUninit<A>> is valid for any bit pattern including |
664 | | // uninitialized bytes, so assume_init() on a MaybeUninit of that type is sound. |
665 | | SmallVecData { inline: unsafe { MaybeUninit::uninit().assume_init() } } |
666 | | } |
667 | | #[inline] |
668 | | unsafe fn into_inline(self) -> MaybeUninit<A> { |
669 | | core::mem::ManuallyDrop::into_inner(self.inline) |
670 | | } |
671 | | #[inline] |
672 | | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { |
673 | | (ConstNonNull(self.heap.0), self.heap.1) |
674 | | } |
675 | | #[inline] |
676 | | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { |
677 | | let h = &mut self.heap; |
678 | | (h.0, &mut h.1) |
679 | | } |
680 | | #[inline] |
681 | | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { |
682 | | SmallVecData { heap: (ptr, len) } |
683 | | } |
684 | | } |
685 | | |
686 | | #[cfg(not(feature = "union"))] |
687 | | enum SmallVecData<A: Array> { |
688 | | Inline(MaybeUninit<A>), |
689 | | // Using NonNull and NonZero here allows to reduce size of `SmallVec`. |
690 | | Heap { |
691 | | // Since we never allocate on heap |
692 | | // unless our capacity is bigger than inline capacity |
693 | | // heap capacity cannot be less than 1. |
694 | | // Therefore, pointer cannot be null too. |
695 | | ptr: NonNull<A::Item>, |
696 | | len: usize, |
697 | | }, |
698 | | } |
699 | | |
700 | | #[cfg(all(not(feature = "union"), feature = "const_new"))] |
701 | | impl<T, const N: usize> SmallVecData<[T; N]> { |
702 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
703 | | #[inline] |
704 | | const fn from_const(inline: MaybeUninit<[T; N]>) -> Self { |
705 | | SmallVecData::Inline(inline) |
706 | | } |
707 | | } |
708 | | |
709 | | #[cfg(not(feature = "union"))] |
710 | | impl<A: Array> SmallVecData<A> { |
711 | | #[inline] |
712 | 60.9M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { |
713 | 60.9M | match self { |
714 | 60.9M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), |
715 | 0 | _ => debug_unreachable!(), |
716 | | } |
717 | 60.9M | } <smallvec::SmallVecData<[u8; 64]>>::inline Line | Count | Source | 712 | 10.3M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 10.3M | match self { | 714 | 10.3M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 10.3M | } |
<smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::inline Line | Count | Source | 712 | 1.60M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 1.60M | match self { | 714 | 1.60M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 1.60M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::inline Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::inline <smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::inline Line | Count | Source | 712 | 5.36M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 5.36M | match self { | 714 | 5.36M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 5.36M | } |
<smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::inline Line | Count | Source | 712 | 1.10M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 1.10M | match self { | 714 | 1.10M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 1.10M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::inline <smallvec::SmallVecData<[u8; 16]>>::inline Line | Count | Source | 712 | 131k | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 131k | match self { | 714 | 131k | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 131k | } |
<smallvec::SmallVecData<[u8; 24]>>::inline Line | Count | Source | 712 | 42.4M | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 42.4M | match self { | 714 | 42.4M | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 42.4M | } |
<smallvec::SmallVecData<[u8; 8]>>::inline Line | Count | Source | 712 | 22.4k | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 22.4k | match self { | 714 | 22.4k | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::inline <smallvec::SmallVecData<[u32; 2]>>::inline Line | Count | Source | 712 | 990 | unsafe fn inline(&self) -> ConstNonNull<A::Item> { | 713 | 990 | match self { | 714 | 990 | SmallVecData::Inline(a) => ConstNonNull::new(a.as_ptr() as *const A::Item).unwrap(), | 715 | 0 | _ => debug_unreachable!(), | 716 | | } | 717 | 990 | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::inline |
718 | | #[inline] |
719 | 66.8M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { |
720 | 66.8M | match self { |
721 | 66.8M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), |
722 | 0 | _ => debug_unreachable!(), |
723 | | } |
724 | 66.8M | } <smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::inline_mut Line | Count | Source | 719 | 766k | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 766k | match self { | 721 | 766k | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 766k | } |
<smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::inline_mut Line | Count | Source | 719 | 2.29M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 2.29M | match self { | 721 | 2.29M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 2.29M | } |
<smallvec::SmallVecData<[u8; 64]>>::inline_mut Line | Count | Source | 719 | 6.45M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 6.45M | match self { | 721 | 6.45M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 6.45M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::inline_mut Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::inline_mut <smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::inline_mut Line | Count | Source | 719 | 2.60M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 2.60M | match self { | 721 | 2.60M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 2.60M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::inline_mut <smallvec::SmallVecData<[u8; 16]>>::inline_mut Line | Count | Source | 719 | 3.00M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 3.00M | match self { | 721 | 3.00M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 3.00M | } |
<smallvec::SmallVecData<[u8; 24]>>::inline_mut Line | Count | Source | 719 | 51.7M | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 51.7M | match self { | 721 | 51.7M | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 51.7M | } |
<smallvec::SmallVecData<[u8; 8]>>::inline_mut Line | Count | Source | 719 | 44.8k | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 44.8k | match self { | 721 | 44.8k | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 44.8k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::inline_mut <smallvec::SmallVecData<[u32; 2]>>::inline_mut Line | Count | Source | 719 | 134 | unsafe fn inline_mut(&mut self) -> NonNull<A::Item> { | 720 | 134 | match self { | 721 | 134 | SmallVecData::Inline(a) => NonNull::new(a.as_mut_ptr() as *mut A::Item).unwrap(), | 722 | 0 | _ => debug_unreachable!(), | 723 | | } | 724 | 134 | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::inline_mut |
725 | | #[inline] |
726 | 204k | fn from_inline(inline: MaybeUninit<A>) -> SmallVecData<A> { |
727 | 204k | SmallVecData::Inline(inline) |
728 | 204k | } <smallvec::SmallVecData<[u8; 24]>>::from_inline Line | Count | Source | 726 | 204k | fn from_inline(inline: MaybeUninit<A>) -> SmallVecData<A> { | 727 | 204k | SmallVecData::Inline(inline) | 728 | 204k | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::from_inline |
729 | | // See the comment on the union variant's empty() for why this exists. |
730 | | #[inline] |
731 | 13.1M | fn empty() -> SmallVecData<A> { |
732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, |
733 | | // so assume_init() on a MaybeUninit of that type is sound. |
734 | 13.1M | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) |
735 | 13.1M | } <smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::empty Line | Count | Source | 731 | 2.86k | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 2.86k | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 2.86k | } |
<smallvec::SmallVecData<[u8; 16]>>::empty Line | Count | Source | 731 | 1.07M | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 1.07M | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 1.07M | } |
<smallvec::SmallVecData<[u8; 64]>>::empty Line | Count | Source | 731 | 1.88M | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 1.88M | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 1.88M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::empty Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::empty <smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::empty Line | Count | Source | 731 | 767k | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 767k | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 767k | } |
<smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::empty Line | Count | Source | 731 | 859k | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 859k | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 859k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::empty <smallvec::SmallVecData<[u8; 24]>>::empty Line | Count | Source | 731 | 8.51M | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 8.51M | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 8.51M | } |
<smallvec::SmallVecData<[u8; 8]>>::empty Line | Count | Source | 731 | 22.4k | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 22.4k | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::empty <smallvec::SmallVecData<[u32; 2]>>::empty Line | Count | Source | 731 | 37 | fn empty() -> SmallVecData<A> { | 732 | | // SAFETY: MaybeUninit<A> is valid for any bit pattern including uninitialized bytes, | 733 | | // so assume_init() on a MaybeUninit of that type is sound. | 734 | 37 | SmallVecData::Inline(unsafe { MaybeUninit::uninit().assume_init() }) | 735 | 37 | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::empty |
736 | | #[inline] |
737 | 22.4k | unsafe fn into_inline(self) -> MaybeUninit<A> { |
738 | 22.4k | match self { |
739 | 22.4k | SmallVecData::Inline(a) => a, |
740 | 0 | _ => debug_unreachable!(), |
741 | | } |
742 | 22.4k | } <smallvec::SmallVecData<[u8; 8]>>::into_inline Line | Count | Source | 737 | 22.4k | unsafe fn into_inline(self) -> MaybeUninit<A> { | 738 | 22.4k | match self { | 739 | 22.4k | SmallVecData::Inline(a) => a, | 740 | 0 | _ => debug_unreachable!(), | 741 | | } | 742 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::into_inline |
743 | | #[inline] |
744 | 9.88M | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { |
745 | 9.88M | match self { |
746 | 9.88M | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), |
747 | 0 | _ => debug_unreachable!(), |
748 | | } |
749 | 9.88M | } <smallvec::SmallVecData<[u8; 64]>>::heap Line | Count | Source | 744 | 694k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 694k | match self { | 746 | 694k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 694k | } |
<smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::heap Line | Count | Source | 744 | 33.6k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 33.6k | match self { | 746 | 33.6k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 33.6k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::heap Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::heap <smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::heap Line | Count | Source | 744 | 10.0k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 10.0k | match self { | 746 | 10.0k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 10.0k | } |
<smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::heap Line | Count | Source | 744 | 10.5k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 10.5k | match self { | 746 | 10.5k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 10.5k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::heap <smallvec::SmallVecData<[u8; 16]>>::heap Line | Count | Source | 744 | 45.4k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 45.4k | match self { | 746 | 45.4k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 45.4k | } |
<smallvec::SmallVecData<[u8; 24]>>::heap Line | Count | Source | 744 | 9.07M | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 9.07M | match self { | 746 | 9.07M | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 9.07M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[u8; 8]>>::heap Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::heap <smallvec::SmallVecData<[u32; 2]>>::heap Line | Count | Source | 744 | 10.5k | unsafe fn heap(&self) -> (ConstNonNull<A::Item>, usize) { | 745 | 10.5k | match self { | 746 | 10.5k | SmallVecData::Heap { ptr, len } => (ConstNonNull(*ptr), *len), | 747 | 0 | _ => debug_unreachable!(), | 748 | | } | 749 | 10.5k | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::heap |
750 | | #[inline] |
751 | 10.1M | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { |
752 | 10.1M | match self { |
753 | 10.1M | SmallVecData::Heap { ptr, len } => (*ptr, len), |
754 | 0 | _ => debug_unreachable!(), |
755 | | } |
756 | 10.1M | } <smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::heap_mut Line | Count | Source | 751 | 16.2k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 16.2k | match self { | 753 | 16.2k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 16.2k | } |
<smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::heap_mut Line | Count | Source | 751 | 39.7k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 39.7k | match self { | 753 | 39.7k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 39.7k | } |
<smallvec::SmallVecData<[u8; 64]>>::heap_mut Line | Count | Source | 751 | 768k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 768k | match self { | 753 | 768k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 768k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::heap_mut Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::heap_mut <smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::heap_mut Line | Count | Source | 751 | 14.2k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 14.2k | match self { | 753 | 14.2k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 14.2k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::heap_mut <smallvec::SmallVecData<[u8; 16]>>::heap_mut Line | Count | Source | 751 | 406k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 406k | match self { | 753 | 406k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 406k | } |
<smallvec::SmallVecData<[u8; 24]>>::heap_mut Line | Count | Source | 751 | 8.85M | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 8.85M | match self { | 753 | 8.85M | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 8.85M | } |
Unexecuted instantiation: <smallvec::SmallVecData<[u8; 8]>>::heap_mut Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::heap_mut <smallvec::SmallVecData<[u32; 2]>>::heap_mut Line | Count | Source | 751 | 4.10k | unsafe fn heap_mut(&mut self) -> (NonNull<A::Item>, &mut usize) { | 752 | 4.10k | match self { | 753 | 4.10k | SmallVecData::Heap { ptr, len } => (*ptr, len), | 754 | 0 | _ => debug_unreachable!(), | 755 | | } | 756 | 4.10k | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::heap_mut |
757 | | #[inline] |
758 | 947k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { |
759 | 947k | SmallVecData::Heap { ptr, len } |
760 | 947k | } <smallvec::SmallVecData<[alloc::vec::Vec<u64>; 3]>>::from_heap Line | Count | Source | 758 | 1.50k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 1.50k | SmallVecData::Heap { ptr, len } | 760 | 1.50k | } |
<smallvec::SmallVecData<[exr::meta::header::Header; 3]>>::from_heap Line | Count | Source | 758 | 3.41k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 3.41k | SmallVecData::Heap { ptr, len } | 760 | 3.41k | } |
<smallvec::SmallVecData<[exr::meta::attribute::ChannelDescription; 5]>>::from_heap Line | Count | Source | 758 | 4.30k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 4.30k | SmallVecData::Heap { ptr, len } | 760 | 4.30k | } |
<smallvec::SmallVecData<[u8; 16]>>::from_heap Line | Count | Source | 758 | 203k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 203k | SmallVecData::Heap { ptr, len } | 760 | 203k | } |
<smallvec::SmallVecData<[u8; 64]>>::from_heap Line | Count | Source | 758 | 86.1k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 86.1k | SmallVecData::Heap { ptr, len } | 760 | 86.1k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::from_heap Unexecuted instantiation: <smallvec::SmallVecData<[exr::compression::piz::ChannelData; 6]>>::from_heap Unexecuted instantiation: <smallvec::SmallVecData<[exr::block::samples::Sample; 8]>>::from_heap <smallvec::SmallVecData<[u8; 24]>>::from_heap Line | Count | Source | 758 | 648k | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 648k | SmallVecData::Heap { ptr, len } | 760 | 648k | } |
Unexecuted instantiation: <smallvec::SmallVecData<[u8; 8]>>::from_heap Unexecuted instantiation: <smallvec::SmallVecData<[usize; 8]>>::from_heap <smallvec::SmallVecData<[u32; 2]>>::from_heap Line | Count | Source | 758 | 165 | fn from_heap(ptr: NonNull<A::Item>, len: usize) -> SmallVecData<A> { | 759 | 165 | SmallVecData::Heap { ptr, len } | 760 | 165 | } |
Unexecuted instantiation: <smallvec::SmallVecData<_>>::from_heap |
761 | | } |
762 | | |
763 | | unsafe impl<A: Array + Send> Send for SmallVecData<A> {} |
764 | | unsafe impl<A: Array + Sync> Sync for SmallVecData<A> {} |
765 | | |
766 | | /// A `Vec`-like container that can store a small number of elements inline. |
767 | | /// |
768 | | /// `SmallVec` acts like a vector, but can store a limited amount of data inline within the |
769 | | /// `SmallVec` struct rather than in a separate allocation. If the data exceeds this limit, the |
770 | | /// `SmallVec` will "spill" its data onto the heap, allocating a new buffer to hold it. |
771 | | /// |
772 | | /// The amount of data that a `SmallVec` can store inline depends on its backing store. The backing |
773 | | /// store can be any type that implements the `Array` trait; usually it is a small fixed-sized |
774 | | /// array. For example a `SmallVec<[u64; 8]>` can hold up to eight 64-bit integers inline. |
775 | | /// |
776 | | /// ## Example |
777 | | /// |
778 | | /// ```rust |
779 | | /// use smallvec::SmallVec; |
780 | | /// let mut v = SmallVec::<[u8; 4]>::new(); // initialize an empty vector |
781 | | /// |
782 | | /// // The vector can hold up to 4 items without spilling onto the heap. |
783 | | /// v.extend(0..4); |
784 | | /// assert_eq!(v.len(), 4); |
785 | | /// assert!(!v.spilled()); |
786 | | /// |
787 | | /// // Pushing another element will force the buffer to spill: |
788 | | /// v.push(4); |
789 | | /// assert_eq!(v.len(), 5); |
790 | | /// assert!(v.spilled()); |
791 | | /// ``` |
792 | | pub struct SmallVec<A: Array> { |
793 | | // The capacity field is used to determine which of the storage variants is active: |
794 | | // If capacity <= Self::inline_capacity() then the inline variant is used and capacity holds the current length of the vector (number of elements actually in use). |
795 | | // If capacity > Self::inline_capacity() then the heap variant is used and capacity holds the size of the memory allocation. |
796 | | capacity: usize, |
797 | | data: SmallVecData<A>, |
798 | | } |
799 | | |
800 | | impl<A: Array> SmallVec<A> { |
801 | | /// Construct an empty vector |
802 | | #[inline] |
803 | 13.0M | pub fn new() -> SmallVec<A> { |
804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, |
805 | | // this check should be optimized away entirely for valid ones. |
806 | 13.0M | assert!( |
807 | 13.0M | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() |
808 | 13.0M | && mem::align_of::<A>() >= mem::align_of::<A::Item>() |
809 | | ); |
810 | 13.0M | SmallVec { |
811 | 13.0M | capacity: 0, |
812 | 13.0M | data: SmallVecData::empty(), |
813 | 13.0M | } |
814 | 13.0M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::new Line | Count | Source | 803 | 2.86k | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 2.86k | assert!( | 807 | 2.86k | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 2.86k | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 2.86k | SmallVec { | 811 | 2.86k | capacity: 0, | 812 | 2.86k | data: SmallVecData::empty(), | 813 | 2.86k | } | 814 | 2.86k | } |
<smallvec::SmallVec<[u8; 16]>>::new Line | Count | Source | 803 | 1.07M | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 1.07M | assert!( | 807 | 1.07M | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 1.07M | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 1.07M | SmallVec { | 811 | 1.07M | capacity: 0, | 812 | 1.07M | data: SmallVecData::empty(), | 813 | 1.07M | } | 814 | 1.07M | } |
<smallvec::SmallVec<[u8; 64]>>::new Line | Count | Source | 803 | 1.88M | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 1.88M | assert!( | 807 | 1.88M | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 1.88M | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 1.88M | SmallVec { | 811 | 1.88M | capacity: 0, | 812 | 1.88M | data: SmallVecData::empty(), | 813 | 1.88M | } | 814 | 1.88M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::new Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::new <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::new Line | Count | Source | 803 | 767k | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 767k | assert!( | 807 | 767k | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 767k | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 767k | SmallVec { | 811 | 767k | capacity: 0, | 812 | 767k | data: SmallVecData::empty(), | 813 | 767k | } | 814 | 767k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::new Line | Count | Source | 803 | 859k | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 859k | assert!( | 807 | 859k | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 859k | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 859k | SmallVec { | 811 | 859k | capacity: 0, | 812 | 859k | data: SmallVecData::empty(), | 813 | 859k | } | 814 | 859k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::new <smallvec::SmallVec<[u8; 24]>>::new Line | Count | Source | 803 | 8.48M | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 8.48M | assert!( | 807 | 8.48M | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 8.48M | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 8.48M | SmallVec { | 811 | 8.48M | capacity: 0, | 812 | 8.48M | data: SmallVecData::empty(), | 813 | 8.48M | } | 814 | 8.48M | } |
<smallvec::SmallVec<[u8; 8]>>::new Line | Count | Source | 803 | 22.4k | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 22.4k | assert!( | 807 | 22.4k | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 22.4k | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 22.4k | SmallVec { | 811 | 22.4k | capacity: 0, | 812 | 22.4k | data: SmallVecData::empty(), | 813 | 22.4k | } | 814 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::new <smallvec::SmallVec<[u32; 2]>>::new Line | Count | Source | 803 | 37 | pub fn new() -> SmallVec<A> { | 804 | | // Try to detect invalid custom implementations of `Array`. Hopefully, | 805 | | // this check should be optimized away entirely for valid ones. | 806 | 37 | assert!( | 807 | 37 | mem::size_of::<A>() == A::size() * mem::size_of::<A::Item>() | 808 | 37 | && mem::align_of::<A>() >= mem::align_of::<A::Item>() | 809 | | ); | 810 | 37 | SmallVec { | 811 | 37 | capacity: 0, | 812 | 37 | data: SmallVecData::empty(), | 813 | 37 | } | 814 | 37 | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::new |
815 | | |
816 | | /// Construct an empty vector with enough capacity pre-allocated to store at least `n` |
817 | | /// elements. |
818 | | /// |
819 | | /// Will create a heap allocation only if `n` is larger than the inline capacity. |
820 | | /// |
821 | | /// ``` |
822 | | /// # use smallvec::SmallVec; |
823 | | /// |
824 | | /// let v: SmallVec<[u8; 3]> = SmallVec::with_capacity(100); |
825 | | /// |
826 | | /// assert!(v.is_empty()); |
827 | | /// assert!(v.capacity() >= 100); |
828 | | /// ``` |
829 | | #[inline] |
830 | 0 | pub fn with_capacity(n: usize) -> Self { |
831 | 0 | let mut v = SmallVec::new(); |
832 | 0 | v.reserve_exact(n); |
833 | 0 | v |
834 | 0 | } |
835 | | |
836 | | /// Construct a new `SmallVec` from a `Vec<A::Item>`. |
837 | | /// |
838 | | /// Elements will be copied to the inline buffer if `vec.capacity() <= Self::inline_capacity()`. |
839 | | /// |
840 | | /// ```rust |
841 | | /// use smallvec::SmallVec; |
842 | | /// |
843 | | /// let vec = vec![1, 2, 3, 4, 5]; |
844 | | /// let small_vec: SmallVec<[_; 3]> = SmallVec::from_vec(vec); |
845 | | /// |
846 | | /// assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); |
847 | | /// ``` |
848 | | #[inline] |
849 | 55.7k | pub fn from_vec(mut vec: Vec<A::Item>) -> SmallVec<A> { |
850 | 55.7k | if vec.capacity() <= Self::inline_capacity() { |
851 | | // Cannot use Vec with smaller capacity |
852 | | // because we use value of `Self::capacity` field as indicator. |
853 | | unsafe { |
854 | 26.6k | let mut data = SmallVecData::<A>::empty(); |
855 | 26.6k | let len = vec.len(); |
856 | 26.6k | vec.set_len(0); |
857 | 26.6k | ptr::copy_nonoverlapping(vec.as_ptr(), data.inline_mut().as_ptr(), len); |
858 | | |
859 | 26.6k | SmallVec { |
860 | 26.6k | capacity: len, |
861 | 26.6k | data, |
862 | 26.6k | } |
863 | | } |
864 | | } else { |
865 | 29.0k | let (ptr, cap, len) = (vec.as_mut_ptr(), vec.capacity(), vec.len()); |
866 | 29.0k | mem::forget(vec); |
867 | 29.0k | let ptr = NonNull::new(ptr) |
868 | | // See docs: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_ptr |
869 | 29.0k | .expect("Cannot be null by `Vec` invariant"); |
870 | | |
871 | 29.0k | SmallVec { |
872 | 29.0k | capacity: cap, |
873 | 29.0k | data: SmallVecData::from_heap(ptr, len), |
874 | 29.0k | } |
875 | | } |
876 | 55.7k | } Unexecuted instantiation: <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::from_vec <smallvec::SmallVec<[u8; 24]>>::from_vec Line | Count | Source | 849 | 55.7k | pub fn from_vec(mut vec: Vec<A::Item>) -> SmallVec<A> { | 850 | 55.7k | if vec.capacity() <= Self::inline_capacity() { | 851 | | // Cannot use Vec with smaller capacity | 852 | | // because we use value of `Self::capacity` field as indicator. | 853 | | unsafe { | 854 | 26.6k | let mut data = SmallVecData::<A>::empty(); | 855 | 26.6k | let len = vec.len(); | 856 | 26.6k | vec.set_len(0); | 857 | 26.6k | ptr::copy_nonoverlapping(vec.as_ptr(), data.inline_mut().as_ptr(), len); | 858 | | | 859 | 26.6k | SmallVec { | 860 | 26.6k | capacity: len, | 861 | 26.6k | data, | 862 | 26.6k | } | 863 | | } | 864 | | } else { | 865 | 29.0k | let (ptr, cap, len) = (vec.as_mut_ptr(), vec.capacity(), vec.len()); | 866 | 29.0k | mem::forget(vec); | 867 | 29.0k | let ptr = NonNull::new(ptr) | 868 | | // See docs: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_mut_ptr | 869 | 29.0k | .expect("Cannot be null by `Vec` invariant"); | 870 | | | 871 | 29.0k | SmallVec { | 872 | 29.0k | capacity: cap, | 873 | 29.0k | data: SmallVecData::from_heap(ptr, len), | 874 | 29.0k | } | 875 | | } | 876 | 55.7k | } |
Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]>>::from_vec Unexecuted instantiation: <smallvec::SmallVec<_>>::from_vec |
877 | | |
878 | | /// Constructs a new `SmallVec` on the stack from an `A` without |
879 | | /// copying elements. |
880 | | /// |
881 | | /// ```rust |
882 | | /// use smallvec::SmallVec; |
883 | | /// |
884 | | /// let buf = [1, 2, 3, 4, 5]; |
885 | | /// let small_vec: SmallVec<_> = SmallVec::from_buf(buf); |
886 | | /// |
887 | | /// assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); |
888 | | /// ``` |
889 | | #[inline] |
890 | 0 | pub fn from_buf(buf: A) -> SmallVec<A> { |
891 | 0 | SmallVec { |
892 | 0 | capacity: A::size(), |
893 | 0 | data: SmallVecData::from_inline(MaybeUninit::new(buf)), |
894 | 0 | } |
895 | 0 | } |
896 | | |
897 | | /// Constructs a new `SmallVec` on the stack from an `A` without |
898 | | /// copying elements. Also sets the length, which must be less or |
899 | | /// equal to the size of `buf`. |
900 | | /// |
901 | | /// ```rust |
902 | | /// use smallvec::SmallVec; |
903 | | /// |
904 | | /// let buf = [1, 2, 3, 4, 5, 0, 0, 0]; |
905 | | /// let small_vec: SmallVec<_> = SmallVec::from_buf_and_len(buf, 5); |
906 | | /// |
907 | | /// assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); |
908 | | /// ``` |
909 | | #[inline] |
910 | 0 | pub fn from_buf_and_len(buf: A, len: usize) -> SmallVec<A> { |
911 | 0 | assert!(len <= A::size()); |
912 | 0 | unsafe { SmallVec::from_buf_and_len_unchecked(MaybeUninit::new(buf), len) } |
913 | 0 | } |
914 | | |
915 | | /// Constructs a new `SmallVec` on the stack from an `A` without |
916 | | /// copying elements. Also sets the length. The user is responsible |
917 | | /// for ensuring that `len <= A::size()`. |
918 | | /// |
919 | | /// ```rust |
920 | | /// use smallvec::SmallVec; |
921 | | /// use std::mem::MaybeUninit; |
922 | | /// |
923 | | /// let buf = [1, 2, 3, 4, 5, 0, 0, 0]; |
924 | | /// let small_vec: SmallVec<_> = unsafe { |
925 | | /// SmallVec::from_buf_and_len_unchecked(MaybeUninit::new(buf), 5) |
926 | | /// }; |
927 | | /// |
928 | | /// assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); |
929 | | /// ``` |
930 | | #[inline] |
931 | 0 | pub unsafe fn from_buf_and_len_unchecked(buf: MaybeUninit<A>, len: usize) -> SmallVec<A> { |
932 | 0 | SmallVec { |
933 | 0 | capacity: len, |
934 | 0 | data: SmallVecData::from_inline(buf), |
935 | 0 | } |
936 | 0 | } |
937 | | |
938 | | /// Sets the length of a vector. |
939 | | /// |
940 | | /// This will explicitly set the size of the vector, without actually |
941 | | /// modifying its buffers, so it is up to the caller to ensure that the |
942 | | /// vector is actually the specified size. |
943 | 83 | pub unsafe fn set_len(&mut self, new_len: usize) { |
944 | 83 | let (_, len_ptr, _) = self.triple_mut(); |
945 | 83 | *len_ptr = new_len; |
946 | 83 | } Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::set_len Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::set_len Unexecuted instantiation: <smallvec::SmallVec<_>>::set_len Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::set_len <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::set_len Line | Count | Source | 943 | 83 | pub unsafe fn set_len(&mut self, new_len: usize) { | 944 | 83 | let (_, len_ptr, _) = self.triple_mut(); | 945 | 83 | *len_ptr = new_len; | 946 | 83 | } |
|
947 | | |
948 | | /// The maximum number of elements this vector can hold inline |
949 | | #[inline] |
950 | 293M | fn inline_capacity() -> usize { |
951 | 293M | if mem::size_of::<A::Item>() > 0 { |
952 | 293M | A::size() |
953 | | } else { |
954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. |
955 | | // Therefore all items are at the same address, |
956 | | // and any array size has capacity for infinitely many items. |
957 | | // The capacity is limited by the bit width of the length field. |
958 | | // |
959 | | // `Vec` also does this: |
960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 |
961 | | // |
962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, |
963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. |
964 | 0 | core::usize::MAX |
965 | | } |
966 | 293M | } <smallvec::SmallVec<[u8; 64]>>::inline_capacity Line | Count | Source | 950 | 37.0M | fn inline_capacity() -> usize { | 951 | 37.0M | if mem::size_of::<A::Item>() > 0 { | 952 | 37.0M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 37.0M | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::inline_capacity Line | Count | Source | 950 | 4.79M | fn inline_capacity() -> usize { | 951 | 4.79M | if mem::size_of::<A::Item>() > 0 { | 952 | 4.79M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 4.79M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::inline_capacity Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::inline_capacity <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::inline_capacity Line | Count | Source | 950 | 16.1M | fn inline_capacity() -> usize { | 951 | 16.1M | if mem::size_of::<A::Item>() > 0 { | 952 | 16.1M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 16.1M | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::inline_capacity Line | Count | Source | 950 | 8.29M | fn inline_capacity() -> usize { | 951 | 8.29M | if mem::size_of::<A::Item>() > 0 { | 952 | 8.29M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 8.29M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::inline_capacity <smallvec::SmallVec<[u8; 16]>>::inline_capacity Line | Count | Source | 950 | 8.00M | fn inline_capacity() -> usize { | 951 | 8.00M | if mem::size_of::<A::Item>() > 0 { | 952 | 8.00M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 8.00M | } |
<smallvec::SmallVec<[u8; 24]>>::inline_capacity Line | Count | Source | 950 | 219M | fn inline_capacity() -> usize { | 951 | 219M | if mem::size_of::<A::Item>() > 0 { | 952 | 219M | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 219M | } |
<smallvec::SmallVec<[u8; 8]>>::inline_capacity Line | Count | Source | 950 | 156k | fn inline_capacity() -> usize { | 951 | 156k | if mem::size_of::<A::Item>() > 0 { | 952 | 156k | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 156k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::inline_capacity <smallvec::SmallVec<[u32; 2]>>::inline_capacity Line | Count | Source | 950 | 17.0k | fn inline_capacity() -> usize { | 951 | 17.0k | if mem::size_of::<A::Item>() > 0 { | 952 | 17.0k | A::size() | 953 | | } else { | 954 | | // For zero-size items code like `ptr.add(offset)` always returns the same pointer. | 955 | | // Therefore all items are at the same address, | 956 | | // and any array size has capacity for infinitely many items. | 957 | | // The capacity is limited by the bit width of the length field. | 958 | | // | 959 | | // `Vec` also does this: | 960 | | // https://github.com/rust-lang/rust/blob/1.44.0/src/liballoc/raw_vec.rs#L186 | 961 | | // | 962 | | // In our case, this also ensures that a smallvec of zero-size items never spills, | 963 | | // and we never try to allocate zero bytes which `std::alloc::alloc` disallows. | 964 | 0 | core::usize::MAX | 965 | | } | 966 | 17.0k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::inline_capacity |
967 | | |
968 | | /// The maximum number of elements this vector can hold inline |
969 | | #[inline] |
970 | 3.96M | pub fn inline_size(&self) -> usize { |
971 | 3.96M | Self::inline_capacity() |
972 | 3.96M | } <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::inline_size Line | Count | Source | 970 | 5.96k | pub fn inline_size(&self) -> usize { | 971 | 5.96k | Self::inline_capacity() | 972 | 5.96k | } |
<smallvec::SmallVec<[u8; 24]>>::inline_size Line | Count | Source | 970 | 3.96M | pub fn inline_size(&self) -> usize { | 971 | 3.96M | Self::inline_capacity() | 972 | 3.96M | } |
<smallvec::SmallVec<[u32; 2]>>::inline_size Line | Count | Source | 970 | 37 | pub fn inline_size(&self) -> usize { | 971 | 37 | Self::inline_capacity() | 972 | 37 | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::inline_size |
973 | | |
974 | | /// The number of elements stored in the vector |
975 | | #[inline] |
976 | 48.0M | pub fn len(&self) -> usize { |
977 | 48.0M | self.triple().1 |
978 | 48.0M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::len Line | Count | Source | 976 | 1.58k | pub fn len(&self) -> usize { | 977 | 1.58k | self.triple().1 | 978 | 1.58k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::len Line | Count | Source | 976 | 771k | pub fn len(&self) -> usize { | 977 | 771k | self.triple().1 | 978 | 771k | } |
<smallvec::SmallVec<[u8; 64]>>::len Line | Count | Source | 976 | 9.02M | pub fn len(&self) -> usize { | 977 | 9.02M | self.triple().1 | 978 | 9.02M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::len Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::len <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::len Line | Count | Source | 976 | 4.48k | pub fn len(&self) -> usize { | 977 | 4.48k | self.triple().1 | 978 | 4.48k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::len Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]>>::len <smallvec::SmallVec<[u8; 24]>>::len Line | Count | Source | 976 | 38.1M | pub fn len(&self) -> usize { | 977 | 38.1M | self.triple().1 | 978 | 38.1M | } |
<smallvec::SmallVec<[u8; 8]>>::len Line | Count | Source | 976 | 22.4k | pub fn len(&self) -> usize { | 977 | 22.4k | self.triple().1 | 978 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::len <smallvec::SmallVec<[u32; 2]>>::len Line | Count | Source | 976 | 165 | pub fn len(&self) -> usize { | 977 | 165 | self.triple().1 | 978 | 165 | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::len |
979 | | |
980 | | /// Returns `true` if the vector is empty |
981 | | #[inline] |
982 | 0 | pub fn is_empty(&self) -> bool { |
983 | 0 | self.len() == 0 |
984 | 0 | } |
985 | | |
986 | | /// The number of items the vector can hold without reallocating |
987 | | #[inline] |
988 | 0 | pub fn capacity(&self) -> usize { |
989 | 0 | self.triple().2 |
990 | 0 | } |
991 | | |
992 | | /// Returns a tuple with (data ptr, len, capacity) |
993 | | /// Useful to get all `SmallVec` properties with a single check of the current storage variant. |
994 | | #[inline] |
995 | 70.8M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { |
996 | | unsafe { |
997 | 70.8M | if self.spilled() { |
998 | 9.88M | let (ptr, len) = self.data.heap(); |
999 | 9.88M | (ptr, len, self.capacity) |
1000 | | } else { |
1001 | 60.9M | (self.data.inline(), self.capacity, Self::inline_capacity()) |
1002 | | } |
1003 | | } |
1004 | 70.8M | } <smallvec::SmallVec<[u8; 64]>>::triple Line | Count | Source | 995 | 11.0M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 11.0M | if self.spilled() { | 998 | 694k | let (ptr, len) = self.data.heap(); | 999 | 694k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 10.3M | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 11.0M | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::triple Line | Count | Source | 995 | 1.63M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 1.63M | if self.spilled() { | 998 | 33.6k | let (ptr, len) = self.data.heap(); | 999 | 33.6k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 1.60M | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 1.63M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::triple Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::triple <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::triple Line | Count | Source | 995 | 5.37M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 5.37M | if self.spilled() { | 998 | 10.0k | let (ptr, len) = self.data.heap(); | 999 | 10.0k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 5.36M | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 5.37M | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::triple Line | Count | Source | 995 | 1.11M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 1.11M | if self.spilled() { | 998 | 10.5k | let (ptr, len) = self.data.heap(); | 999 | 10.5k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 1.10M | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 1.11M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::triple <smallvec::SmallVec<[u8; 16]>>::triple Line | Count | Source | 995 | 176k | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 176k | if self.spilled() { | 998 | 45.4k | let (ptr, len) = self.data.heap(); | 999 | 45.4k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 131k | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 176k | } |
<smallvec::SmallVec<[u8; 24]>>::triple Line | Count | Source | 995 | 51.5M | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 51.5M | if self.spilled() { | 998 | 9.07M | let (ptr, len) = self.data.heap(); | 999 | 9.07M | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 42.4M | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 51.5M | } |
<smallvec::SmallVec<[u8; 8]>>::triple Line | Count | Source | 995 | 22.4k | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 22.4k | if self.spilled() { | 998 | 0 | let (ptr, len) = self.data.heap(); | 999 | 0 | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 22.4k | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::triple <smallvec::SmallVec<[u32; 2]>>::triple Line | Count | Source | 995 | 11.5k | fn triple(&self) -> (ConstNonNull<A::Item>, usize, usize) { | 996 | | unsafe { | 997 | 11.5k | if self.spilled() { | 998 | 10.5k | let (ptr, len) = self.data.heap(); | 999 | 10.5k | (ptr, len, self.capacity) | 1000 | | } else { | 1001 | 990 | (self.data.inline(), self.capacity, Self::inline_capacity()) | 1002 | | } | 1003 | | } | 1004 | 11.5k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::triple |
1005 | | |
1006 | | /// Returns a tuple with (data ptr, len ptr, capacity) |
1007 | | #[inline] |
1008 | 75.7M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { |
1009 | | unsafe { |
1010 | 75.7M | if self.spilled() { |
1011 | 8.92M | let (ptr, len_ptr) = self.data.heap_mut(); |
1012 | 8.92M | (ptr, len_ptr, self.capacity) |
1013 | | } else { |
1014 | 66.8M | ( |
1015 | 66.8M | self.data.inline_mut(), |
1016 | 66.8M | &mut self.capacity, |
1017 | 66.8M | Self::inline_capacity(), |
1018 | 66.8M | ) |
1019 | | } |
1020 | | } |
1021 | 75.7M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::triple_mut Line | Count | Source | 1008 | 781k | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 781k | if self.spilled() { | 1011 | 14.3k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 14.3k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 766k | ( | 1015 | 766k | self.data.inline_mut(), | 1016 | 766k | &mut self.capacity, | 1017 | 766k | Self::inline_capacity(), | 1018 | 766k | ) | 1019 | | } | 1020 | | } | 1021 | 781k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::triple_mut Line | Count | Source | 1008 | 2.32M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 2.32M | if self.spilled() { | 1011 | 35.5k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 35.5k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 2.29M | ( | 1015 | 2.29M | self.data.inline_mut(), | 1016 | 2.29M | &mut self.capacity, | 1017 | 2.29M | Self::inline_capacity(), | 1018 | 2.29M | ) | 1019 | | } | 1020 | | } | 1021 | 2.32M | } |
<smallvec::SmallVec<[u8; 64]>>::triple_mut Line | Count | Source | 1008 | 7.17M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 7.17M | if self.spilled() { | 1011 | 715k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 715k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 6.45M | ( | 1015 | 6.45M | self.data.inline_mut(), | 1016 | 6.45M | &mut self.capacity, | 1017 | 6.45M | Self::inline_capacity(), | 1018 | 6.45M | ) | 1019 | | } | 1020 | | } | 1021 | 7.17M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::triple_mut Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::triple_mut <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::triple_mut Line | Count | Source | 1008 | 2.61M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 2.61M | if self.spilled() { | 1011 | 6.82k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 6.82k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 2.60M | ( | 1015 | 2.60M | self.data.inline_mut(), | 1016 | 2.60M | &mut self.capacity, | 1017 | 2.60M | Self::inline_capacity(), | 1018 | 2.60M | ) | 1019 | | } | 1020 | | } | 1021 | 2.61M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::triple_mut <smallvec::SmallVec<[u8; 16]>>::triple_mut Line | Count | Source | 1008 | 3.21M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 3.21M | if self.spilled() { | 1011 | 203k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 203k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 3.00M | ( | 1015 | 3.00M | self.data.inline_mut(), | 1016 | 3.00M | &mut self.capacity, | 1017 | 3.00M | Self::inline_capacity(), | 1018 | 3.00M | ) | 1019 | | } | 1020 | | } | 1021 | 3.21M | } |
<smallvec::SmallVec<[u8; 24]>>::triple_mut Line | Count | Source | 1008 | 59.6M | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 59.6M | if self.spilled() { | 1011 | 7.94M | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 7.94M | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 51.6M | ( | 1015 | 51.6M | self.data.inline_mut(), | 1016 | 51.6M | &mut self.capacity, | 1017 | 51.6M | Self::inline_capacity(), | 1018 | 51.6M | ) | 1019 | | } | 1020 | | } | 1021 | 59.6M | } |
<smallvec::SmallVec<[u8; 8]>>::triple_mut Line | Count | Source | 1008 | 44.8k | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 44.8k | if self.spilled() { | 1011 | 0 | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 0 | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 44.8k | ( | 1015 | 44.8k | self.data.inline_mut(), | 1016 | 44.8k | &mut self.capacity, | 1017 | 44.8k | Self::inline_capacity(), | 1018 | 44.8k | ) | 1019 | | } | 1020 | | } | 1021 | 44.8k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::triple_mut <smallvec::SmallVec<[u32; 2]>>::triple_mut Line | Count | Source | 1008 | 4.04k | fn triple_mut(&mut self) -> (NonNull<A::Item>, &mut usize, usize) { | 1009 | | unsafe { | 1010 | 4.04k | if self.spilled() { | 1011 | 3.91k | let (ptr, len_ptr) = self.data.heap_mut(); | 1012 | 3.91k | (ptr, len_ptr, self.capacity) | 1013 | | } else { | 1014 | 134 | ( | 1015 | 134 | self.data.inline_mut(), | 1016 | 134 | &mut self.capacity, | 1017 | 134 | Self::inline_capacity(), | 1018 | 134 | ) | 1019 | | } | 1020 | | } | 1021 | 4.04k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::triple_mut |
1022 | | |
1023 | | /// Returns `true` if the data has spilled into a separate heap-allocated buffer. |
1024 | | #[inline] |
1025 | 160M | pub fn spilled(&self) -> bool { |
1026 | 160M | self.capacity > Self::inline_capacity() |
1027 | 160M | } <smallvec::SmallVec<[u8; 64]>>::spilled Line | Count | Source | 1025 | 20.1M | pub fn spilled(&self) -> bool { | 1026 | 20.1M | self.capacity > Self::inline_capacity() | 1027 | 20.1M | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::spilled Line | Count | Source | 1025 | 2.42M | pub fn spilled(&self) -> bool { | 1026 | 2.42M | self.capacity > Self::inline_capacity() | 1027 | 2.42M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::spilled Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::spilled <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::spilled Line | Count | Source | 1025 | 8.47M | pub fn spilled(&self) -> bool { | 1026 | 8.47M | self.capacity > Self::inline_capacity() | 1027 | 8.47M | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::spilled Line | Count | Source | 1025 | 4.58M | pub fn spilled(&self) -> bool { | 1026 | 4.58M | self.capacity > Self::inline_capacity() | 1027 | 4.58M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::spilled <smallvec::SmallVec<[u8; 16]>>::spilled Line | Count | Source | 1025 | 4.66M | pub fn spilled(&self) -> bool { | 1026 | 4.66M | self.capacity > Self::inline_capacity() | 1027 | 4.66M | } |
<smallvec::SmallVec<[u8; 24]>>::spilled Line | Count | Source | 1025 | 120M | pub fn spilled(&self) -> bool { | 1026 | 120M | self.capacity > Self::inline_capacity() | 1027 | 120M | } |
<smallvec::SmallVec<[u8; 8]>>::spilled Line | Count | Source | 1025 | 89.7k | pub fn spilled(&self) -> bool { | 1026 | 89.7k | self.capacity > Self::inline_capacity() | 1027 | 89.7k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::spilled <smallvec::SmallVec<[u32; 2]>>::spilled Line | Count | Source | 1025 | 15.7k | pub fn spilled(&self) -> bool { | 1026 | 15.7k | self.capacity > Self::inline_capacity() | 1027 | 15.7k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::spilled |
1028 | | |
1029 | | /// Creates a draining iterator that removes the specified range in the vector |
1030 | | /// and yields the removed items. |
1031 | | /// |
1032 | | /// Note 1: The element range is removed even if the iterator is only |
1033 | | /// partially consumed or not consumed at all. |
1034 | | /// |
1035 | | /// Note 2: It is unspecified how many elements are removed from the vector |
1036 | | /// if the `Drain` value is leaked. |
1037 | | /// |
1038 | | /// # Panics |
1039 | | /// |
1040 | | /// Panics if the starting point is greater than the end point or if |
1041 | | /// the end point is greater than the length of the vector. |
1042 | 0 | pub fn drain<R>(&mut self, range: R) -> Drain<'_, A> |
1043 | 0 | where |
1044 | 0 | R: RangeBounds<usize>, |
1045 | | { |
1046 | | use core::ops::Bound::*; |
1047 | | |
1048 | 0 | let len = self.len(); |
1049 | 0 | let start = match range.start_bound() { |
1050 | 0 | Included(&n) => n, |
1051 | 0 | Excluded(&n) => n.checked_add(1).expect("Range start out of bounds"), |
1052 | 0 | Unbounded => 0, |
1053 | | }; |
1054 | 0 | let end = match range.end_bound() { |
1055 | 0 | Included(&n) => n.checked_add(1).expect("Range end out of bounds"), |
1056 | 0 | Excluded(&n) => n, |
1057 | 0 | Unbounded => len, |
1058 | | }; |
1059 | | |
1060 | 0 | assert!(start <= end); |
1061 | 0 | assert!(end <= len); |
1062 | | |
1063 | | unsafe { |
1064 | 0 | self.set_len(start); |
1065 | | |
1066 | 0 | let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start); |
1067 | | |
1068 | 0 | Drain { |
1069 | 0 | tail_start: end, |
1070 | 0 | tail_len: len - end, |
1071 | 0 | iter: range_slice.iter(), |
1072 | 0 | // Since self is a &mut, passing it to a function would invalidate the slice iterator. |
1073 | 0 | vec: NonNull::new_unchecked(self as *mut _), |
1074 | 0 | } |
1075 | | } |
1076 | 0 | } |
1077 | | |
1078 | | #[cfg(feature = "drain_filter")] |
1079 | | /// Creates an iterator which uses a closure to determine if an element should be removed. |
1080 | | /// |
1081 | | /// If the closure returns true, the element is removed and yielded. If the closure returns |
1082 | | /// false, the element will remain in the vector and will not be yielded by the iterator. |
1083 | | /// |
1084 | | /// Using this method is equivalent to the following code: |
1085 | | /// ``` |
1086 | | /// # use smallvec::SmallVec; |
1087 | | /// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 }; |
1088 | | /// # let mut vec: SmallVec<[i32; 8]> = SmallVec::from_slice(&[1i32, 2, 3, 4, 5, 6]); |
1089 | | /// let mut i = 0; |
1090 | | /// while i < vec.len() { |
1091 | | /// if some_predicate(&mut vec[i]) { |
1092 | | /// let val = vec.remove(i); |
1093 | | /// // your code here |
1094 | | /// } else { |
1095 | | /// i += 1; |
1096 | | /// } |
1097 | | /// } |
1098 | | /// |
1099 | | /// # assert_eq!(vec, SmallVec::<[i32; 8]>::from_slice(&[1i32, 4, 5])); |
1100 | | /// ``` |
1101 | | /// /// |
1102 | | /// But `drain_filter` is easier to use. `drain_filter` is also more efficient, |
1103 | | /// because it can backshift the elements of the array in bulk. |
1104 | | /// |
1105 | | /// Note that `drain_filter` also lets you mutate every element in the filter closure, |
1106 | | /// regardless of whether you choose to keep or remove it. |
1107 | | /// |
1108 | | /// # Examples |
1109 | | /// |
1110 | | /// Splitting an array into evens and odds, reusing the original allocation: |
1111 | | /// |
1112 | | /// ``` |
1113 | | /// # use smallvec::SmallVec; |
1114 | | /// let mut numbers: SmallVec<[i32; 16]> = SmallVec::from_slice(&[1i32, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]); |
1115 | | /// |
1116 | | /// let evens = numbers.drain_filter(|x| *x % 2 == 0).collect::<SmallVec<[i32; 16]>>(); |
1117 | | /// let odds = numbers; |
1118 | | /// |
1119 | | /// assert_eq!(evens, SmallVec::<[i32; 16]>::from_slice(&[2i32, 4, 6, 8, 14])); |
1120 | | /// assert_eq!(odds, SmallVec::<[i32; 16]>::from_slice(&[1i32, 3, 5, 9, 11, 13, 15])); |
1121 | | /// ``` |
1122 | | pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<'_, A, F,> |
1123 | | where |
1124 | | F: FnMut(&mut A::Item) -> bool, |
1125 | | { |
1126 | | let old_len = self.len(); |
1127 | | |
1128 | | // Guard against us getting leaked (leak amplification) |
1129 | | unsafe { |
1130 | | self.set_len(0); |
1131 | | } |
1132 | | |
1133 | | DrainFilter { vec: self, idx: 0, del: 0, old_len, pred: filter, panic_flag: false } |
1134 | | } |
1135 | | |
1136 | | /// Append an item to the vector. |
1137 | | #[inline] |
1138 | 41.8M | pub fn push(&mut self, value: A::Item) { |
1139 | | unsafe { |
1140 | 41.8M | let (mut ptr, mut len, cap) = self.triple_mut(); |
1141 | 41.8M | if *len == cap { |
1142 | 534k | self.reserve_one_unchecked(); |
1143 | 534k | let (heap_ptr, heap_len) = self.data.heap_mut(); |
1144 | 534k | ptr = heap_ptr; |
1145 | 534k | len = heap_len; |
1146 | 41.3M | } |
1147 | 41.8M | ptr::write(ptr.as_ptr().add(*len), value); |
1148 | 41.8M | *len += 1; |
1149 | | } |
1150 | 41.8M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::push Line | Count | Source | 1138 | 13.6k | pub fn push(&mut self, value: A::Item) { | 1139 | | unsafe { | 1140 | 13.6k | let (mut ptr, mut len, cap) = self.triple_mut(); | 1141 | 13.6k | if *len == cap { | 1142 | 1.50k | self.reserve_one_unchecked(); | 1143 | 1.50k | let (heap_ptr, heap_len) = self.data.heap_mut(); | 1144 | 1.50k | ptr = heap_ptr; | 1145 | 1.50k | len = heap_len; | 1146 | 12.1k | } | 1147 | 13.6k | ptr::write(ptr.as_ptr().add(*len), value); | 1148 | 13.6k | *len += 1; | 1149 | | } | 1150 | 13.6k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::push Line | Count | Source | 1138 | 38.9k | pub fn push(&mut self, value: A::Item) { | 1139 | | unsafe { | 1140 | 38.9k | let (mut ptr, mut len, cap) = self.triple_mut(); | 1141 | 38.9k | if *len == cap { | 1142 | 3.06k | self.reserve_one_unchecked(); | 1143 | 3.06k | let (heap_ptr, heap_len) = self.data.heap_mut(); | 1144 | 3.06k | ptr = heap_ptr; | 1145 | 3.06k | len = heap_len; | 1146 | 35.8k | } | 1147 | 38.9k | ptr::write(ptr.as_ptr().add(*len), value); | 1148 | 38.9k | *len += 1; | 1149 | | } | 1150 | 38.9k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::push Line | Count | Source | 1138 | 197k | pub fn push(&mut self, value: A::Item) { | 1139 | | unsafe { | 1140 | 197k | let (mut ptr, mut len, cap) = self.triple_mut(); | 1141 | 197k | if *len == cap { | 1142 | 4.30k | self.reserve_one_unchecked(); | 1143 | 4.30k | let (heap_ptr, heap_len) = self.data.heap_mut(); | 1144 | 4.30k | ptr = heap_ptr; | 1145 | 4.30k | len = heap_len; | 1146 | 192k | } | 1147 | 197k | ptr::write(ptr.as_ptr().add(*len), value); | 1148 | 197k | *len += 1; | 1149 | | } | 1150 | 197k | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]>>::push Unexecuted instantiation: <smallvec::SmallVec<[u8; 64]>>::push Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::push Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::push Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::push <smallvec::SmallVec<[u8; 24]>>::push Line | Count | Source | 1138 | 41.5M | pub fn push(&mut self, value: A::Item) { | 1139 | | unsafe { | 1140 | 41.5M | let (mut ptr, mut len, cap) = self.triple_mut(); | 1141 | 41.5M | if *len == cap { | 1142 | 525k | self.reserve_one_unchecked(); | 1143 | 525k | let (heap_ptr, heap_len) = self.data.heap_mut(); | 1144 | 525k | ptr = heap_ptr; | 1145 | 525k | len = heap_len; | 1146 | 41.0M | } | 1147 | 41.5M | ptr::write(ptr.as_ptr().add(*len), value); | 1148 | 41.5M | *len += 1; | 1149 | | } | 1150 | 41.5M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]>>::push Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::push <smallvec::SmallVec<[u32; 2]>>::push Line | Count | Source | 1138 | 3.87k | pub fn push(&mut self, value: A::Item) { | 1139 | | unsafe { | 1140 | 3.87k | let (mut ptr, mut len, cap) = self.triple_mut(); | 1141 | 3.87k | if *len == cap { | 1142 | 165 | self.reserve_one_unchecked(); | 1143 | 165 | let (heap_ptr, heap_len) = self.data.heap_mut(); | 1144 | 165 | ptr = heap_ptr; | 1145 | 165 | len = heap_len; | 1146 | 3.70k | } | 1147 | 3.87k | ptr::write(ptr.as_ptr().add(*len), value); | 1148 | 3.87k | *len += 1; | 1149 | | } | 1150 | 3.87k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::push |
1151 | | |
1152 | | /// Remove an item from the end of the vector and return it, or None if empty. |
1153 | | #[inline] |
1154 | 0 | pub fn pop(&mut self) -> Option<A::Item> { |
1155 | | unsafe { |
1156 | 0 | let (ptr, len_ptr, _) = self.triple_mut(); |
1157 | 0 | let ptr: *const _ = ptr.as_ptr(); |
1158 | 0 | if *len_ptr == 0 { |
1159 | 0 | return None; |
1160 | 0 | } |
1161 | 0 | let last_index = *len_ptr - 1; |
1162 | 0 | *len_ptr = last_index; |
1163 | 0 | Some(ptr::read(ptr.add(last_index))) |
1164 | | } |
1165 | 0 | } |
1166 | | |
1167 | | /// Moves all the elements of `other` into `self`, leaving `other` empty. |
1168 | | /// |
1169 | | /// # Example |
1170 | | /// |
1171 | | /// ``` |
1172 | | /// # use smallvec::{SmallVec, smallvec}; |
1173 | | /// let mut v0: SmallVec<[u8; 16]> = smallvec![1, 2, 3]; |
1174 | | /// let mut v1: SmallVec<[u8; 32]> = smallvec![4, 5, 6]; |
1175 | | /// v0.append(&mut v1); |
1176 | | /// assert_eq!(*v0, [1, 2, 3, 4, 5, 6]); |
1177 | | /// assert_eq!(*v1, []); |
1178 | | /// ``` |
1179 | 0 | pub fn append<B>(&mut self, other: &mut SmallVec<B>) |
1180 | 0 | where |
1181 | 0 | B: Array<Item = A::Item>, |
1182 | | { |
1183 | 0 | self.extend(other.drain(..)) |
1184 | 0 | } |
1185 | | |
1186 | | /// Re-allocate to set the capacity to `max(new_cap, inline_size())`. |
1187 | | /// |
1188 | | /// Panics if `new_cap` is less than the vector's length |
1189 | | /// or if the capacity computation overflows `usize`. |
1190 | 0 | pub fn grow(&mut self, new_cap: usize) { |
1191 | 0 | infallible(self.try_grow(new_cap)) |
1192 | 0 | } |
1193 | | |
1194 | | /// Re-allocate to set the capacity to `max(new_cap, inline_size())`. |
1195 | | /// |
1196 | | /// Panics if `new_cap` is less than the vector's length |
1197 | 918k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { |
1198 | | unsafe { |
1199 | 918k | let unspilled = !self.spilled(); |
1200 | 918k | let (ptr, &mut len, cap) = self.triple_mut(); |
1201 | 918k | assert!(new_cap >= len); |
1202 | 918k | if new_cap <= Self::inline_capacity() { |
1203 | 0 | if unspilled { |
1204 | 0 | return Ok(()); |
1205 | 0 | } |
1206 | 0 | self.data = SmallVecData::empty(); |
1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); |
1208 | 0 | self.capacity = len; |
1209 | 0 | deallocate(ptr, cap); |
1210 | 918k | } else if new_cap != cap { |
1211 | 918k | let layout = layout_array::<A::Item>(new_cap)?; |
1212 | 918k | debug_assert!(layout.size() > 0); |
1213 | | let new_alloc; |
1214 | 918k | if unspilled { |
1215 | 616k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) |
1216 | 616k | .ok_or(CollectionAllocErr::AllocErr { layout })? |
1217 | 616k | .cast(); |
1218 | 616k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); |
1219 | | } else { |
1220 | | // This should never fail since the same succeeded |
1221 | | // when previously allocating `ptr`. |
1222 | 301k | let old_layout = layout_array::<A::Item>(cap)?; |
1223 | | |
1224 | 301k | let new_ptr = |
1225 | 301k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); |
1226 | 301k | new_alloc = NonNull::new(new_ptr) |
1227 | 301k | .ok_or(CollectionAllocErr::AllocErr { layout })? |
1228 | 301k | .cast(); |
1229 | | } |
1230 | 918k | self.data = SmallVecData::from_heap(new_alloc, len); |
1231 | 918k | self.capacity = new_cap; |
1232 | 0 | } |
1233 | 918k | Ok(()) |
1234 | | } |
1235 | 918k | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::try_grow Line | Count | Source | 1197 | 1.50k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 1.50k | let unspilled = !self.spilled(); | 1200 | 1.50k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 1.50k | assert!(new_cap >= len); | 1202 | 1.50k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 1.50k | } else if new_cap != cap { | 1211 | 1.50k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 1.50k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 1.50k | if unspilled { | 1215 | 417 | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 417 | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 417 | .cast(); | 1218 | 417 | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 1.08k | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 1.08k | let new_ptr = | 1225 | 1.08k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 1.08k | new_alloc = NonNull::new(new_ptr) | 1227 | 1.08k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 1.08k | .cast(); | 1229 | | } | 1230 | 1.50k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 1.50k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 1.50k | Ok(()) | 1234 | | } | 1235 | 1.50k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::try_grow Line | Count | Source | 1197 | 3.41k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 3.41k | let unspilled = !self.spilled(); | 1200 | 3.41k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 3.41k | assert!(new_cap >= len); | 1202 | 3.41k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 3.41k | } else if new_cap != cap { | 1211 | 3.41k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 3.41k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 3.41k | if unspilled { | 1215 | 1.12k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 1.12k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 1.12k | .cast(); | 1218 | 1.12k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 2.28k | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 2.28k | let new_ptr = | 1225 | 2.28k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 2.28k | new_alloc = NonNull::new(new_ptr) | 1227 | 2.28k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 2.28k | .cast(); | 1229 | | } | 1230 | 3.41k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 3.41k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 3.41k | Ok(()) | 1234 | | } | 1235 | 3.41k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::try_grow Line | Count | Source | 1197 | 4.30k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 4.30k | let unspilled = !self.spilled(); | 1200 | 4.30k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 4.30k | assert!(new_cap >= len); | 1202 | 4.30k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 4.30k | } else if new_cap != cap { | 1211 | 4.30k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 4.30k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 4.30k | if unspilled { | 1215 | 3.16k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 3.16k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 3.16k | .cast(); | 1218 | 3.16k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 1.14k | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 1.14k | let new_ptr = | 1225 | 1.14k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 1.14k | new_alloc = NonNull::new(new_ptr) | 1227 | 1.14k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 1.14k | .cast(); | 1229 | | } | 1230 | 4.30k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 4.30k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 4.30k | Ok(()) | 1234 | | } | 1235 | 4.30k | } |
<smallvec::SmallVec<[u8; 16]>>::try_grow Line | Count | Source | 1197 | 203k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 203k | let unspilled = !self.spilled(); | 1200 | 203k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 203k | assert!(new_cap >= len); | 1202 | 203k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 203k | } else if new_cap != cap { | 1211 | 203k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 203k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 203k | if unspilled { | 1215 | 203k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 203k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 203k | .cast(); | 1218 | 203k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 0 | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 0 | let new_ptr = | 1225 | 0 | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 0 | new_alloc = NonNull::new(new_ptr) | 1227 | 0 | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 0 | .cast(); | 1229 | | } | 1230 | 203k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 203k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 203k | Ok(()) | 1234 | | } | 1235 | 203k | } |
<smallvec::SmallVec<[u8; 64]>>::try_grow Line | Count | Source | 1197 | 86.1k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 86.1k | let unspilled = !self.spilled(); | 1200 | 86.1k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 86.1k | assert!(new_cap >= len); | 1202 | 86.1k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 86.1k | } else if new_cap != cap { | 1211 | 86.1k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 86.1k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 86.1k | if unspilled { | 1215 | 52.8k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 52.8k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 52.8k | .cast(); | 1218 | 52.8k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 33.3k | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 33.3k | let new_ptr = | 1225 | 33.3k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 33.3k | new_alloc = NonNull::new(new_ptr) | 1227 | 33.3k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 33.3k | .cast(); | 1229 | | } | 1230 | 86.1k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 86.1k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 86.1k | Ok(()) | 1234 | | } | 1235 | 86.1k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::try_grow Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::try_grow Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::try_grow <smallvec::SmallVec<[u8; 24]>>::try_grow Line | Count | Source | 1197 | 619k | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 619k | let unspilled = !self.spilled(); | 1200 | 619k | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 619k | assert!(new_cap >= len); | 1202 | 619k | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 619k | } else if new_cap != cap { | 1211 | 619k | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 619k | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 619k | if unspilled { | 1215 | 355k | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 355k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 355k | .cast(); | 1218 | 355k | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 263k | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 263k | let new_ptr = | 1225 | 263k | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 263k | new_alloc = NonNull::new(new_ptr) | 1227 | 263k | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 263k | .cast(); | 1229 | | } | 1230 | 619k | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 619k | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 619k | Ok(()) | 1234 | | } | 1235 | 619k | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]>>::try_grow Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::try_grow <smallvec::SmallVec<[u32; 2]>>::try_grow Line | Count | Source | 1197 | 165 | pub fn try_grow(&mut self, new_cap: usize) -> Result<(), CollectionAllocErr> { | 1198 | | unsafe { | 1199 | 165 | let unspilled = !self.spilled(); | 1200 | 165 | let (ptr, &mut len, cap) = self.triple_mut(); | 1201 | 165 | assert!(new_cap >= len); | 1202 | 165 | if new_cap <= Self::inline_capacity() { | 1203 | 0 | if unspilled { | 1204 | 0 | return Ok(()); | 1205 | 0 | } | 1206 | 0 | self.data = SmallVecData::empty(); | 1207 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); | 1208 | 0 | self.capacity = len; | 1209 | 0 | deallocate(ptr, cap); | 1210 | 165 | } else if new_cap != cap { | 1211 | 165 | let layout = layout_array::<A::Item>(new_cap)?; | 1212 | 165 | debug_assert!(layout.size() > 0); | 1213 | | let new_alloc; | 1214 | 165 | if unspilled { | 1215 | 28 | new_alloc = NonNull::new(alloc::alloc::alloc(layout)) | 1216 | 28 | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1217 | 28 | .cast(); | 1218 | 28 | ptr::copy_nonoverlapping(ptr.as_ptr(), new_alloc.as_ptr(), len); | 1219 | | } else { | 1220 | | // This should never fail since the same succeeded | 1221 | | // when previously allocating `ptr`. | 1222 | 137 | let old_layout = layout_array::<A::Item>(cap)?; | 1223 | | | 1224 | 137 | let new_ptr = | 1225 | 137 | alloc::alloc::realloc(ptr.as_ptr() as *mut u8, old_layout, layout.size()); | 1226 | 137 | new_alloc = NonNull::new(new_ptr) | 1227 | 137 | .ok_or(CollectionAllocErr::AllocErr { layout })? | 1228 | 137 | .cast(); | 1229 | | } | 1230 | 165 | self.data = SmallVecData::from_heap(new_alloc, len); | 1231 | 165 | self.capacity = new_cap; | 1232 | 0 | } | 1233 | 165 | Ok(()) | 1234 | | } | 1235 | 165 | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::try_grow |
1236 | | |
1237 | | /// Reserve capacity for `additional` more elements to be inserted. |
1238 | | /// |
1239 | | /// May reserve more space to avoid frequent reallocations. |
1240 | | /// |
1241 | | /// Panics if the capacity computation overflows `usize`. |
1242 | | #[inline] |
1243 | 8.91M | pub fn reserve(&mut self, additional: usize) { |
1244 | 8.91M | infallible(self.try_reserve(additional)) |
1245 | 8.91M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::reserve Line | Count | Source | 1243 | 2.86k | pub fn reserve(&mut self, additional: usize) { | 1244 | 2.86k | infallible(self.try_reserve(additional)) | 1245 | 2.86k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::reserve Line | Count | Source | 1243 | 760k | pub fn reserve(&mut self, additional: usize) { | 1244 | 760k | infallible(self.try_reserve(additional)) | 1245 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::reserve Line | Count | Source | 1243 | 776k | pub fn reserve(&mut self, additional: usize) { | 1244 | 776k | infallible(self.try_reserve(additional)) | 1245 | 776k | } |
<smallvec::SmallVec<[u8; 16]>>::reserve Line | Count | Source | 1243 | 1.07M | pub fn reserve(&mut self, additional: usize) { | 1244 | 1.07M | infallible(self.try_reserve(additional)) | 1245 | 1.07M | } |
<smallvec::SmallVec<[u8; 64]>>::reserve Line | Count | Source | 1243 | 1.75M | pub fn reserve(&mut self, additional: usize) { | 1244 | 1.75M | infallible(self.try_reserve(additional)) | 1245 | 1.75M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::reserve Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::reserve Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::reserve <smallvec::SmallVec<[u8; 24]>>::reserve Line | Count | Source | 1243 | 4.52M | pub fn reserve(&mut self, additional: usize) { | 1244 | 4.52M | infallible(self.try_reserve(additional)) | 1245 | 4.52M | } |
<smallvec::SmallVec<[u8; 8]>>::reserve Line | Count | Source | 1243 | 22.4k | pub fn reserve(&mut self, additional: usize) { | 1244 | 22.4k | infallible(self.try_reserve(additional)) | 1245 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::reserve Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]>>::reserve Unexecuted instantiation: <smallvec::SmallVec<_>>::reserve |
1246 | | |
1247 | | /// Internal method used to grow in push() and insert(), where we know already we have to grow. |
1248 | | #[cold] |
1249 | 534k | fn reserve_one_unchecked(&mut self) { |
1250 | 534k | debug_assert_eq!(self.len(), self.capacity()); |
1251 | 534k | let new_cap = self.len() |
1252 | 534k | .checked_add(1) |
1253 | 534k | .and_then(usize::checked_next_power_of_two) |
1254 | 534k | .expect("capacity overflow"); |
1255 | 534k | infallible(self.try_grow(new_cap)) |
1256 | 534k | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::reserve_one_unchecked Line | Count | Source | 1249 | 1.50k | fn reserve_one_unchecked(&mut self) { | 1250 | 1.50k | debug_assert_eq!(self.len(), self.capacity()); | 1251 | 1.50k | let new_cap = self.len() | 1252 | 1.50k | .checked_add(1) | 1253 | 1.50k | .and_then(usize::checked_next_power_of_two) | 1254 | 1.50k | .expect("capacity overflow"); | 1255 | 1.50k | infallible(self.try_grow(new_cap)) | 1256 | 1.50k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::reserve_one_unchecked Line | Count | Source | 1249 | 3.06k | fn reserve_one_unchecked(&mut self) { | 1250 | 3.06k | debug_assert_eq!(self.len(), self.capacity()); | 1251 | 3.06k | let new_cap = self.len() | 1252 | 3.06k | .checked_add(1) | 1253 | 3.06k | .and_then(usize::checked_next_power_of_two) | 1254 | 3.06k | .expect("capacity overflow"); | 1255 | 3.06k | infallible(self.try_grow(new_cap)) | 1256 | 3.06k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::reserve_one_unchecked Line | Count | Source | 1249 | 4.30k | fn reserve_one_unchecked(&mut self) { | 1250 | 4.30k | debug_assert_eq!(self.len(), self.capacity()); | 1251 | 4.30k | let new_cap = self.len() | 1252 | 4.30k | .checked_add(1) | 1253 | 4.30k | .and_then(usize::checked_next_power_of_two) | 1254 | 4.30k | .expect("capacity overflow"); | 1255 | 4.30k | infallible(self.try_grow(new_cap)) | 1256 | 4.30k | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]>>::reserve_one_unchecked Unexecuted instantiation: <smallvec::SmallVec<[u8; 64]>>::reserve_one_unchecked Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::reserve_one_unchecked Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::reserve_one_unchecked Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::reserve_one_unchecked <smallvec::SmallVec<[u8; 24]>>::reserve_one_unchecked Line | Count | Source | 1249 | 525k | fn reserve_one_unchecked(&mut self) { | 1250 | 525k | debug_assert_eq!(self.len(), self.capacity()); | 1251 | 525k | let new_cap = self.len() | 1252 | 525k | .checked_add(1) | 1253 | 525k | .and_then(usize::checked_next_power_of_two) | 1254 | 525k | .expect("capacity overflow"); | 1255 | 525k | infallible(self.try_grow(new_cap)) | 1256 | 525k | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]>>::reserve_one_unchecked Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::reserve_one_unchecked <smallvec::SmallVec<[u32; 2]>>::reserve_one_unchecked Line | Count | Source | 1249 | 165 | fn reserve_one_unchecked(&mut self) { | 1250 | 165 | debug_assert_eq!(self.len(), self.capacity()); | 1251 | 165 | let new_cap = self.len() | 1252 | 165 | .checked_add(1) | 1253 | 165 | .and_then(usize::checked_next_power_of_two) | 1254 | 165 | .expect("capacity overflow"); | 1255 | 165 | infallible(self.try_grow(new_cap)) | 1256 | 165 | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::reserve_one_unchecked |
1257 | | |
1258 | | /// Reserve capacity for `additional` more elements to be inserted. |
1259 | | /// |
1260 | | /// May reserve more space to avoid frequent reallocations. |
1261 | 8.91M | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { |
1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated |
1263 | | // calls to it from callers. |
1264 | 8.91M | let (_, &mut len, cap) = self.triple_mut(); |
1265 | 8.91M | if cap - len >= additional { |
1266 | 8.52M | return Ok(()); |
1267 | 383k | } |
1268 | 383k | let new_cap = len |
1269 | 383k | .checked_add(additional) |
1270 | 383k | .and_then(usize::checked_next_power_of_two) |
1271 | 383k | .ok_or(CollectionAllocErr::CapacityOverflow)?; |
1272 | 383k | self.try_grow(new_cap) |
1273 | 8.91M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::try_reserve Line | Count | Source | 1261 | 2.86k | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 2.86k | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 2.86k | if cap - len >= additional { | 1266 | 2.86k | return Ok(()); | 1267 | 0 | } | 1268 | 0 | let new_cap = len | 1269 | 0 | .checked_add(additional) | 1270 | 0 | .and_then(usize::checked_next_power_of_two) | 1271 | 0 | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 0 | self.try_grow(new_cap) | 1273 | 2.86k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]>>::try_reserve Line | Count | Source | 1261 | 760k | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 760k | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 760k | if cap - len >= additional { | 1266 | 759k | return Ok(()); | 1267 | 341 | } | 1268 | 341 | let new_cap = len | 1269 | 341 | .checked_add(additional) | 1270 | 341 | .and_then(usize::checked_next_power_of_two) | 1271 | 341 | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 341 | self.try_grow(new_cap) | 1273 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::try_reserve Line | Count | Source | 1261 | 776k | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 776k | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 776k | if cap - len >= additional { | 1266 | 776k | return Ok(()); | 1267 | 4 | } | 1268 | 4 | let new_cap = len | 1269 | 4 | .checked_add(additional) | 1270 | 4 | .and_then(usize::checked_next_power_of_two) | 1271 | 4 | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 4 | self.try_grow(new_cap) | 1273 | 776k | } |
<smallvec::SmallVec<[u8; 16]>>::try_reserve Line | Count | Source | 1261 | 1.07M | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 1.07M | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 1.07M | if cap - len >= additional { | 1266 | 867k | return Ok(()); | 1267 | 203k | } | 1268 | 203k | let new_cap = len | 1269 | 203k | .checked_add(additional) | 1270 | 203k | .and_then(usize::checked_next_power_of_two) | 1271 | 203k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 203k | self.try_grow(new_cap) | 1273 | 1.07M | } |
<smallvec::SmallVec<[u8; 64]>>::try_reserve Line | Count | Source | 1261 | 1.75M | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 1.75M | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 1.75M | if cap - len >= additional { | 1266 | 1.66M | return Ok(()); | 1267 | 86.1k | } | 1268 | 86.1k | let new_cap = len | 1269 | 86.1k | .checked_add(additional) | 1270 | 86.1k | .and_then(usize::checked_next_power_of_two) | 1271 | 86.1k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 86.1k | self.try_grow(new_cap) | 1273 | 1.75M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::try_reserve Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::try_reserve Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]>>::try_reserve <smallvec::SmallVec<[u8; 24]>>::try_reserve Line | Count | Source | 1261 | 4.52M | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 4.52M | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 4.52M | if cap - len >= additional { | 1266 | 4.43M | return Ok(()); | 1267 | 93.6k | } | 1268 | 93.6k | let new_cap = len | 1269 | 93.6k | .checked_add(additional) | 1270 | 93.6k | .and_then(usize::checked_next_power_of_two) | 1271 | 93.6k | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 93.6k | self.try_grow(new_cap) | 1273 | 4.52M | } |
<smallvec::SmallVec<[u8; 8]>>::try_reserve Line | Count | Source | 1261 | 22.4k | pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { | 1262 | | // prefer triple_mut() even if triple() would work so that the optimizer removes duplicated | 1263 | | // calls to it from callers. | 1264 | 22.4k | let (_, &mut len, cap) = self.triple_mut(); | 1265 | 22.4k | if cap - len >= additional { | 1266 | 22.4k | return Ok(()); | 1267 | 0 | } | 1268 | 0 | let new_cap = len | 1269 | 0 | .checked_add(additional) | 1270 | 0 | .and_then(usize::checked_next_power_of_two) | 1271 | 0 | .ok_or(CollectionAllocErr::CapacityOverflow)?; | 1272 | 0 | self.try_grow(new_cap) | 1273 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]>>::try_reserve Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]>>::try_reserve Unexecuted instantiation: <smallvec::SmallVec<_>>::try_reserve |
1274 | | |
1275 | | /// Reserve the minimum capacity for `additional` more elements to be inserted. |
1276 | | /// |
1277 | | /// Panics if the new capacity overflows `usize`. |
1278 | 0 | pub fn reserve_exact(&mut self, additional: usize) { |
1279 | 0 | infallible(self.try_reserve_exact(additional)) |
1280 | 0 | } |
1281 | | |
1282 | | /// Reserve the minimum capacity for `additional` more elements to be inserted. |
1283 | 0 | pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { |
1284 | 0 | let (_, &mut len, cap) = self.triple_mut(); |
1285 | 0 | if cap - len >= additional { |
1286 | 0 | return Ok(()); |
1287 | 0 | } |
1288 | 0 | let new_cap = len |
1289 | 0 | .checked_add(additional) |
1290 | 0 | .ok_or(CollectionAllocErr::CapacityOverflow)?; |
1291 | 0 | self.try_grow(new_cap) |
1292 | 0 | } |
1293 | | |
1294 | | /// Shrink the capacity of the vector as much as possible. |
1295 | | /// |
1296 | | /// When possible, this will move data from an external heap buffer to the vector's inline |
1297 | | /// storage. |
1298 | 0 | pub fn shrink_to_fit(&mut self) { |
1299 | 0 | if !self.spilled() { |
1300 | 0 | return; |
1301 | 0 | } |
1302 | 0 | let len = self.len(); |
1303 | 0 | if self.inline_size() >= len { |
1304 | 0 | unsafe { |
1305 | 0 | let (ptr, len) = self.data.heap(); |
1306 | 0 | self.data = SmallVecData::empty(); |
1307 | 0 | ptr::copy_nonoverlapping(ptr.as_ptr(), self.data.inline_mut().as_ptr(), len); |
1308 | 0 | deallocate(ptr.0, self.capacity); |
1309 | 0 | self.capacity = len; |
1310 | 0 | } |
1311 | 0 | } else if self.capacity() > len { |
1312 | 0 | self.grow(len); |
1313 | 0 | } |
1314 | 0 | } |
1315 | | |
1316 | | /// Shorten the vector, keeping the first `len` elements and dropping the rest. |
1317 | | /// |
1318 | | /// If `len` is greater than or equal to the vector's current length, this has no |
1319 | | /// effect. |
1320 | | /// |
1321 | | /// This does not re-allocate. If you want the vector's capacity to shrink, call |
1322 | | /// `shrink_to_fit` after truncating. |
1323 | 0 | pub fn truncate(&mut self, len: usize) { |
1324 | | unsafe { |
1325 | 0 | let (ptr, len_ptr, _) = self.triple_mut(); |
1326 | 0 | let ptr = ptr.as_ptr(); |
1327 | 0 | while len < *len_ptr { |
1328 | 0 | let last_index = *len_ptr - 1; |
1329 | 0 | *len_ptr = last_index; |
1330 | 0 | ptr::drop_in_place(ptr.add(last_index)); |
1331 | 0 | } |
1332 | | } |
1333 | 0 | } Unexecuted instantiation: <smallvec::SmallVec<[u8; 64]>>::truncate Unexecuted instantiation: <smallvec::SmallVec<_>>::truncate |
1334 | | |
1335 | | /// Extracts a slice containing the entire vector. |
1336 | | /// |
1337 | | /// Equivalent to `&s[..]`. |
1338 | 12.5M | pub fn as_slice(&self) -> &[A::Item] { |
1339 | 12.5M | self |
1340 | 12.5M | } <smallvec::SmallVec<[exr::meta::header::Header; 3]>>::as_slice Line | Count | Source | 1338 | 763k | pub fn as_slice(&self) -> &[A::Item] { | 1339 | 763k | self | 1340 | 763k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]>>::as_slice Line | Count | Source | 1338 | 776k | pub fn as_slice(&self) -> &[A::Item] { | 1339 | 776k | self | 1340 | 776k | } |
<smallvec::SmallVec<[u8; 16]>>::as_slice Line | Count | Source | 1338 | 176k | pub fn as_slice(&self) -> &[A::Item] { | 1339 | 176k | self | 1340 | 176k | } |
<smallvec::SmallVec<[u8; 64]>>::as_slice Line | Count | Source | 1338 | 1.99M | pub fn as_slice(&self) -> &[A::Item] { | 1339 | 1.99M | self | 1340 | 1.99M | } |
<smallvec::SmallVec<[u8; 24]>>::as_slice Line | Count | Source | 1338 | 8.81M | pub fn as_slice(&self) -> &[A::Item] { | 1339 | 8.81M | self | 1340 | 8.81M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]>>::as_slice Unexecuted instantiation: <smallvec::SmallVec<_>>::as_slice |
1341 | | |
1342 | | /// Extracts a mutable slice of the entire vector. |
1343 | | /// |
1344 | | /// Equivalent to `&mut s[..]`. |
1345 | 0 | pub fn as_mut_slice(&mut self) -> &mut [A::Item] { |
1346 | 0 | self |
1347 | 0 | } |
1348 | | |
1349 | | /// Remove the element at position `index`, replacing it with the last element. |
1350 | | /// |
1351 | | /// This does not preserve ordering, but is O(1). |
1352 | | /// |
1353 | | /// Panics if `index` is out of bounds. |
1354 | | #[inline] |
1355 | 0 | pub fn swap_remove(&mut self, index: usize) -> A::Item { |
1356 | 0 | let len = self.len(); |
1357 | 0 | self.swap(len - 1, index); |
1358 | 0 | self.pop() |
1359 | 0 | .unwrap_or_else(|| unsafe { unreachable_unchecked() }) |
1360 | 0 | } |
1361 | | |
1362 | | /// Remove all elements from the vector. |
1363 | | #[inline] |
1364 | 0 | pub fn clear(&mut self) { |
1365 | 0 | self.truncate(0); |
1366 | 0 | } |
1367 | | |
1368 | | /// Remove and return the element at position `index`, shifting all elements after it to the |
1369 | | /// left. |
1370 | | /// |
1371 | | /// Panics if `index` is out of bounds. |
1372 | 0 | pub fn remove(&mut self, index: usize) -> A::Item { |
1373 | | unsafe { |
1374 | 0 | let (ptr, len_ptr, _) = self.triple_mut(); |
1375 | 0 | let len = *len_ptr; |
1376 | 0 | assert!(index < len); |
1377 | 0 | *len_ptr = len - 1; |
1378 | 0 | let ptr = ptr.as_ptr().add(index); |
1379 | 0 | let item = ptr::read(ptr); |
1380 | 0 | ptr::copy(ptr.add(1), ptr, len - index - 1); |
1381 | 0 | item |
1382 | | } |
1383 | 0 | } |
1384 | | |
1385 | | /// Insert an element at position `index`, shifting all elements after it to the right. |
1386 | | /// |
1387 | | /// Panics if `index > len`. |
1388 | 0 | pub fn insert(&mut self, index: usize, element: A::Item) { |
1389 | | unsafe { |
1390 | 0 | let (mut ptr, mut len_ptr, cap) = self.triple_mut(); |
1391 | 0 | if *len_ptr == cap { |
1392 | 0 | self.reserve_one_unchecked(); |
1393 | 0 | let (heap_ptr, heap_len_ptr) = self.data.heap_mut(); |
1394 | 0 | ptr = heap_ptr; |
1395 | 0 | len_ptr = heap_len_ptr; |
1396 | 0 | } |
1397 | 0 | let mut ptr = ptr.as_ptr(); |
1398 | 0 | let len = *len_ptr; |
1399 | 0 | if index > len { |
1400 | 0 | panic!("index exceeds length"); |
1401 | 0 | } |
1402 | | // SAFETY: add is UB if index > len, but we panicked first |
1403 | 0 | ptr = ptr.add(index); |
1404 | 0 | if index < len { |
1405 | 0 | // Shift element to the right of `index`. |
1406 | 0 | ptr::copy(ptr, ptr.add(1), len - index); |
1407 | 0 | } |
1408 | 0 | *len_ptr = len + 1; |
1409 | 0 | ptr::write(ptr, element); |
1410 | | } |
1411 | 0 | } |
1412 | | |
1413 | | /// Insert multiple elements at position `index`, shifting all following elements toward the |
1414 | | /// back. |
1415 | 0 | pub fn insert_many<I: IntoIterator<Item = A::Item>>(&mut self, index: usize, iterable: I) { |
1416 | 0 | let mut iter = iterable.into_iter(); |
1417 | 0 | if index == self.len() { |
1418 | 0 | return self.extend(iter); |
1419 | 0 | } |
1420 | | |
1421 | 0 | let (lower_size_bound, _) = iter.size_hint(); |
1422 | 0 | assert!(lower_size_bound <= core::isize::MAX as usize); // Ensure offset is indexable |
1423 | 0 | assert!(index + lower_size_bound >= index); // Protect against overflow |
1424 | | |
1425 | 0 | let mut num_added = 0; |
1426 | 0 | let old_len = self.len(); |
1427 | 0 | assert!(index <= old_len); |
1428 | | |
1429 | | unsafe { |
1430 | | // Reserve space for `lower_size_bound` elements. |
1431 | 0 | self.reserve(lower_size_bound); |
1432 | 0 | let start = self.as_mut_ptr(); |
1433 | 0 | let ptr = start.add(index); |
1434 | | |
1435 | | // Move the trailing elements. |
1436 | 0 | ptr::copy(ptr, ptr.add(lower_size_bound), old_len - index); |
1437 | | |
1438 | | // In case the iterator panics, don't double-drop the items we just copied above. |
1439 | 0 | self.set_len(0); |
1440 | 0 | let mut guard = DropOnPanic { |
1441 | 0 | start, |
1442 | 0 | skip: index..(index + lower_size_bound), |
1443 | 0 | len: old_len + lower_size_bound, |
1444 | 0 | }; |
1445 | | |
1446 | | // The set_len above invalidates the previous pointers, so we must re-create them. |
1447 | 0 | let start = self.as_mut_ptr(); |
1448 | 0 | let ptr = start.add(index); |
1449 | | |
1450 | 0 | while num_added < lower_size_bound { |
1451 | 0 | let element = match iter.next() { |
1452 | 0 | Some(x) => x, |
1453 | 0 | None => break, |
1454 | | }; |
1455 | 0 | let cur = ptr.add(num_added); |
1456 | 0 | ptr::write(cur, element); |
1457 | 0 | guard.skip.start += 1; |
1458 | 0 | num_added += 1; |
1459 | | } |
1460 | | |
1461 | 0 | if num_added < lower_size_bound { |
1462 | 0 | // Iterator provided fewer elements than the hint. Move the tail backward. |
1463 | 0 | ptr::copy( |
1464 | 0 | ptr.add(lower_size_bound), |
1465 | 0 | ptr.add(num_added), |
1466 | 0 | old_len - index, |
1467 | 0 | ); |
1468 | 0 | } |
1469 | | // There are no more duplicate or uninitialized slots, so the guard is not needed. |
1470 | 0 | self.set_len(old_len + num_added); |
1471 | 0 | mem::forget(guard); |
1472 | | } |
1473 | | |
1474 | | // Insert any remaining elements one-by-one. |
1475 | 0 | for element in iter { |
1476 | 0 | self.insert(index + num_added, element); |
1477 | 0 | num_added += 1; |
1478 | 0 | } |
1479 | | |
1480 | | struct DropOnPanic<T> { |
1481 | | start: *mut T, |
1482 | | skip: Range<usize>, // Space we copied-out-of, but haven't written-to yet. |
1483 | | len: usize, |
1484 | | } |
1485 | | |
1486 | | impl<T> Drop for DropOnPanic<T> { |
1487 | 0 | fn drop(&mut self) { |
1488 | 0 | for i in 0..self.len { |
1489 | 0 | if !self.skip.contains(&i) { |
1490 | 0 | unsafe { |
1491 | 0 | ptr::drop_in_place(self.start.add(i)); |
1492 | 0 | } |
1493 | 0 | } |
1494 | | } |
1495 | 0 | } |
1496 | | } |
1497 | 0 | } |
1498 | | |
1499 | | /// Convert a `SmallVec` to a `Vec`, without reallocating if the `SmallVec` has already spilled onto |
1500 | | /// the heap. |
1501 | 0 | pub fn into_vec(mut self) -> Vec<A::Item> { |
1502 | 0 | if self.spilled() { |
1503 | | unsafe { |
1504 | 0 | let (ptr, &mut len) = self.data.heap_mut(); |
1505 | 0 | let v = Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity); |
1506 | 0 | mem::forget(self); |
1507 | 0 | v |
1508 | | } |
1509 | | } else { |
1510 | 0 | self.into_iter().collect() |
1511 | | } |
1512 | 0 | } |
1513 | | |
1514 | | /// Converts a `SmallVec` into a `Box<[T]>` without reallocating if the `SmallVec` has already spilled |
1515 | | /// onto the heap. |
1516 | | /// |
1517 | | /// Note that this will drop any excess capacity. |
1518 | 0 | pub fn into_boxed_slice(self) -> Box<[A::Item]> { |
1519 | 0 | self.into_vec().into_boxed_slice() |
1520 | 0 | } |
1521 | | |
1522 | | /// Convert the `SmallVec` into an `A` if possible. Otherwise return `Err(Self)`. |
1523 | | /// |
1524 | | /// This method returns `Err(Self)` if the `SmallVec` is too short (and the `A` contains uninitialized elements), |
1525 | | /// or if the `SmallVec` is too long (and all the elements were spilled to the heap). |
1526 | 22.4k | pub fn into_inner(self) -> Result<A, Self> { |
1527 | 22.4k | if self.spilled() || self.len() != A::size() { |
1528 | | // Note: A::size, not Self::inline_capacity |
1529 | 0 | Err(self) |
1530 | | } else { |
1531 | | unsafe { |
1532 | 22.4k | let data = ptr::read(&self.data); |
1533 | 22.4k | mem::forget(self); |
1534 | 22.4k | Ok(data.into_inline().assume_init()) |
1535 | | } |
1536 | | } |
1537 | 22.4k | } <smallvec::SmallVec<[u8; 8]>>::into_inner Line | Count | Source | 1526 | 22.4k | pub fn into_inner(self) -> Result<A, Self> { | 1527 | 22.4k | if self.spilled() || self.len() != A::size() { | 1528 | | // Note: A::size, not Self::inline_capacity | 1529 | 0 | Err(self) | 1530 | | } else { | 1531 | | unsafe { | 1532 | 22.4k | let data = ptr::read(&self.data); | 1533 | 22.4k | mem::forget(self); | 1534 | 22.4k | Ok(data.into_inline().assume_init()) | 1535 | | } | 1536 | | } | 1537 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::into_inner |
1538 | | |
1539 | | /// Retains only the elements specified by the predicate. |
1540 | | /// |
1541 | | /// In other words, remove all elements `e` such that `f(&e)` returns `false`. |
1542 | | /// This method operates in place and preserves the order of the retained |
1543 | | /// elements. |
1544 | 0 | pub fn retain<F: FnMut(&mut A::Item) -> bool>(&mut self, mut f: F) { |
1545 | 0 | let mut del = 0; |
1546 | 0 | let len = self.len(); |
1547 | 0 | for i in 0..len { |
1548 | 0 | if !f(&mut self[i]) { |
1549 | 0 | del += 1; |
1550 | 0 | } else if del > 0 { |
1551 | 0 | self.swap(i - del, i); |
1552 | 0 | } |
1553 | | } |
1554 | 0 | self.truncate(len - del); |
1555 | 0 | } |
1556 | | |
1557 | | /// Retains only the elements specified by the predicate. |
1558 | | /// |
1559 | | /// This method is identical in behaviour to [`retain`]; it is included only |
1560 | | /// to maintain api-compatibility with `std::Vec`, where the methods are |
1561 | | /// separate for historical reasons. |
1562 | 0 | pub fn retain_mut<F: FnMut(&mut A::Item) -> bool>(&mut self, f: F) { |
1563 | 0 | self.retain(f) |
1564 | 0 | } |
1565 | | |
1566 | | /// Removes consecutive duplicate elements. |
1567 | 0 | pub fn dedup(&mut self) |
1568 | 0 | where |
1569 | 0 | A::Item: PartialEq<A::Item>, |
1570 | | { |
1571 | 0 | self.dedup_by(|a, b| a == b); |
1572 | 0 | } |
1573 | | |
1574 | | /// Removes consecutive duplicate elements using the given equality relation. |
1575 | 0 | pub fn dedup_by<F>(&mut self, mut same_bucket: F) |
1576 | 0 | where |
1577 | 0 | F: FnMut(&mut A::Item, &mut A::Item) -> bool, |
1578 | | { |
1579 | | // See the implementation of Vec::dedup_by in the |
1580 | | // standard library for an explanation of this algorithm. |
1581 | 0 | let len = self.len(); |
1582 | 0 | if len <= 1 { |
1583 | 0 | return; |
1584 | 0 | } |
1585 | | |
1586 | 0 | let ptr = self.as_mut_ptr(); |
1587 | 0 | let mut w: usize = 1; |
1588 | | |
1589 | | unsafe { |
1590 | 0 | for r in 1..len { |
1591 | 0 | let p_r = ptr.add(r); |
1592 | 0 | let p_wm1 = ptr.add(w - 1); |
1593 | 0 | if !same_bucket(&mut *p_r, &mut *p_wm1) { |
1594 | 0 | if r != w { |
1595 | 0 | let p_w = p_wm1.add(1); |
1596 | 0 | mem::swap(&mut *p_r, &mut *p_w); |
1597 | 0 | } |
1598 | 0 | w += 1; |
1599 | 0 | } |
1600 | | } |
1601 | | } |
1602 | | |
1603 | 0 | self.truncate(w); |
1604 | 0 | } |
1605 | | |
1606 | | /// Removes consecutive elements that map to the same key. |
1607 | 0 | pub fn dedup_by_key<F, K>(&mut self, mut key: F) |
1608 | 0 | where |
1609 | 0 | F: FnMut(&mut A::Item) -> K, |
1610 | 0 | K: PartialEq<K>, |
1611 | | { |
1612 | 0 | self.dedup_by(|a, b| key(a) == key(b)); |
1613 | 0 | } |
1614 | | |
1615 | | /// Resizes the `SmallVec` in-place so that `len` is equal to `new_len`. |
1616 | | /// |
1617 | | /// If `new_len` is greater than `len`, the `SmallVec` is extended by the difference, with each |
1618 | | /// additional slot filled with the result of calling the closure `f`. The return values from `f` |
1619 | | /// will end up in the `SmallVec` in the order they have been generated. |
1620 | | /// |
1621 | | /// If `new_len` is less than `len`, the `SmallVec` is simply truncated. |
1622 | | /// |
1623 | | /// This method uses a closure to create new values on every push. If you'd rather `Clone` a given |
1624 | | /// value, use `resize`. If you want to use the `Default` trait to generate values, you can pass |
1625 | | /// `Default::default()` as the second argument. |
1626 | | /// |
1627 | | /// Added for `std::vec::Vec` compatibility (added in Rust 1.33.0) |
1628 | | /// |
1629 | | /// ``` |
1630 | | /// # use smallvec::{smallvec, SmallVec}; |
1631 | | /// let mut vec : SmallVec<[_; 4]> = smallvec![1, 2, 3]; |
1632 | | /// vec.resize_with(5, Default::default); |
1633 | | /// assert_eq!(&*vec, &[1, 2, 3, 0, 0]); |
1634 | | /// |
1635 | | /// let mut vec : SmallVec<[_; 4]> = smallvec![]; |
1636 | | /// let mut p = 1; |
1637 | | /// vec.resize_with(4, || { p *= 2; p }); |
1638 | | /// assert_eq!(&*vec, &[2, 4, 8, 16]); |
1639 | | /// ``` |
1640 | 0 | pub fn resize_with<F>(&mut self, new_len: usize, f: F) |
1641 | 0 | where |
1642 | 0 | F: FnMut() -> A::Item, |
1643 | | { |
1644 | 0 | let old_len = self.len(); |
1645 | 0 | if old_len < new_len { |
1646 | 0 | let mut f = f; |
1647 | 0 | let additional = new_len - old_len; |
1648 | 0 | self.reserve(additional); |
1649 | 0 | for _ in 0..additional { |
1650 | 0 | self.push(f()); |
1651 | 0 | } |
1652 | 0 | } else if old_len > new_len { |
1653 | 0 | self.truncate(new_len); |
1654 | 0 | } |
1655 | 0 | } |
1656 | | |
1657 | | /// Creates a `SmallVec` directly from the raw components of another |
1658 | | /// `SmallVec`. |
1659 | | /// |
1660 | | /// # Safety |
1661 | | /// |
1662 | | /// This is highly unsafe, due to the number of invariants that aren't |
1663 | | /// checked: |
1664 | | /// |
1665 | | /// * `ptr` needs to have been previously allocated via `SmallVec` for its |
1666 | | /// spilled storage (at least, it's highly likely to be incorrect if it |
1667 | | /// wasn't). |
1668 | | /// * `ptr`'s `A::Item` type needs to be the same size and alignment that |
1669 | | /// it was allocated with |
1670 | | /// * `length` needs to be less than or equal to `capacity`. |
1671 | | /// * `capacity` needs to be the capacity that the pointer was allocated |
1672 | | /// with. |
1673 | | /// |
1674 | | /// Violating these may cause problems like corrupting the allocator's |
1675 | | /// internal data structures. |
1676 | | /// |
1677 | | /// Additionally, `capacity` must be greater than the amount of inline |
1678 | | /// storage `A` has; that is, the new `SmallVec` must need to spill over |
1679 | | /// into heap allocated storage. This condition is asserted against. |
1680 | | /// |
1681 | | /// The ownership of `ptr` is effectively transferred to the |
1682 | | /// `SmallVec` which may then deallocate, reallocate or change the |
1683 | | /// contents of memory pointed to by the pointer at will. Ensure |
1684 | | /// that nothing else uses the pointer after calling this |
1685 | | /// function. |
1686 | | /// |
1687 | | /// # Examples |
1688 | | /// |
1689 | | /// ``` |
1690 | | /// # use smallvec::{smallvec, SmallVec}; |
1691 | | /// use std::mem; |
1692 | | /// use std::ptr; |
1693 | | /// |
1694 | | /// fn main() { |
1695 | | /// let mut v: SmallVec<[_; 1]> = smallvec![1, 2, 3]; |
1696 | | /// |
1697 | | /// // Pull out the important parts of `v`. |
1698 | | /// let p = v.as_mut_ptr(); |
1699 | | /// let len = v.len(); |
1700 | | /// let cap = v.capacity(); |
1701 | | /// let spilled = v.spilled(); |
1702 | | /// |
1703 | | /// unsafe { |
1704 | | /// // Forget all about `v`. The heap allocation that stored the |
1705 | | /// // three values won't be deallocated. |
1706 | | /// mem::forget(v); |
1707 | | /// |
1708 | | /// // Overwrite memory with [4, 5, 6]. |
1709 | | /// // |
1710 | | /// // This is only safe if `spilled` is true! Otherwise, we are |
1711 | | /// // writing into the old `SmallVec`'s inline storage on the |
1712 | | /// // stack. |
1713 | | /// assert!(spilled); |
1714 | | /// for i in 0..len { |
1715 | | /// ptr::write(p.add(i), 4 + i); |
1716 | | /// } |
1717 | | /// |
1718 | | /// // Put everything back together into a SmallVec with a different |
1719 | | /// // amount of inline storage, but which is still less than `cap`. |
1720 | | /// let rebuilt = SmallVec::<[_; 2]>::from_raw_parts(p, len, cap); |
1721 | | /// assert_eq!(&*rebuilt, &[4, 5, 6]); |
1722 | | /// } |
1723 | | /// } |
1724 | | #[inline] |
1725 | 0 | pub unsafe fn from_raw_parts(ptr: *mut A::Item, length: usize, capacity: usize) -> SmallVec<A> { |
1726 | | // SAFETY: We require caller to provide same ptr as we alloc |
1727 | | // and we never alloc null pointer. |
1728 | 0 | let ptr = unsafe { |
1729 | 0 | debug_assert!(!ptr.is_null(), "Called `from_raw_parts` with null pointer."); |
1730 | 0 | NonNull::new_unchecked(ptr) |
1731 | | }; |
1732 | 0 | assert!(capacity > Self::inline_capacity()); |
1733 | 0 | SmallVec { |
1734 | 0 | capacity, |
1735 | 0 | data: SmallVecData::from_heap(ptr, length), |
1736 | 0 | } |
1737 | 0 | } |
1738 | | |
1739 | | /// Returns a raw pointer to the vector's buffer. |
1740 | 83 | pub fn as_ptr(&self) -> *const A::Item { |
1741 | | // We shadow the slice method of the same name to avoid going through |
1742 | | // `deref`, which creates an intermediate reference that may place |
1743 | | // additional safety constraints on the contents of the slice. |
1744 | 83 | self.triple().0.as_ptr() |
1745 | 83 | } Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>>::as_ptr Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]>>::as_ptr Unexecuted instantiation: <smallvec::SmallVec<_>>::as_ptr Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::as_ptr <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]>>::as_ptr Line | Count | Source | 1740 | 83 | pub fn as_ptr(&self) -> *const A::Item { | 1741 | | // We shadow the slice method of the same name to avoid going through | 1742 | | // `deref`, which creates an intermediate reference that may place | 1743 | | // additional safety constraints on the contents of the slice. | 1744 | 83 | self.triple().0.as_ptr() | 1745 | 83 | } |
|
1746 | | |
1747 | | /// Returns a raw mutable pointer to the vector's buffer. |
1748 | 0 | pub fn as_mut_ptr(&mut self) -> *mut A::Item { |
1749 | | // We shadow the slice method of the same name to avoid going through |
1750 | | // `deref_mut`, which creates an intermediate reference that may place |
1751 | | // additional safety constraints on the contents of the slice. |
1752 | 0 | self.triple_mut().0.as_ptr() |
1753 | 0 | } |
1754 | | } |
1755 | | |
1756 | | impl<A: Array> SmallVec<A> |
1757 | | where |
1758 | | A::Item: Copy, |
1759 | | { |
1760 | | /// Copy the elements from a slice into a new `SmallVec`. |
1761 | | /// |
1762 | | /// For slices of `Copy` types, this is more efficient than `SmallVec::from(slice)`. |
1763 | 204k | pub fn from_slice(slice: &[A::Item]) -> Self { |
1764 | 204k | let len = slice.len(); |
1765 | 204k | if len <= Self::inline_capacity() { |
1766 | 204k | SmallVec { |
1767 | 204k | capacity: len, |
1768 | 204k | data: SmallVecData::from_inline(unsafe { |
1769 | 204k | let mut data: MaybeUninit<A> = MaybeUninit::uninit(); |
1770 | 204k | ptr::copy_nonoverlapping( |
1771 | 204k | slice.as_ptr(), |
1772 | 204k | data.as_mut_ptr() as *mut A::Item, |
1773 | 204k | len, |
1774 | 204k | ); |
1775 | 204k | data |
1776 | 204k | }), |
1777 | 204k | } |
1778 | | } else { |
1779 | 0 | let mut b = slice.to_vec(); |
1780 | 0 | let cap = b.capacity(); |
1781 | 0 | let ptr = NonNull::new(b.as_mut_ptr()).expect("Vec always contain non null pointers."); |
1782 | 0 | mem::forget(b); |
1783 | 0 | SmallVec { |
1784 | 0 | capacity: cap, |
1785 | 0 | data: SmallVecData::from_heap(ptr, len), |
1786 | 0 | } |
1787 | | } |
1788 | 204k | } <smallvec::SmallVec<[u8; 24]>>::from_slice Line | Count | Source | 1763 | 204k | pub fn from_slice(slice: &[A::Item]) -> Self { | 1764 | 204k | let len = slice.len(); | 1765 | 204k | if len <= Self::inline_capacity() { | 1766 | 204k | SmallVec { | 1767 | 204k | capacity: len, | 1768 | 204k | data: SmallVecData::from_inline(unsafe { | 1769 | 204k | let mut data: MaybeUninit<A> = MaybeUninit::uninit(); | 1770 | 204k | ptr::copy_nonoverlapping( | 1771 | 204k | slice.as_ptr(), | 1772 | 204k | data.as_mut_ptr() as *mut A::Item, | 1773 | 204k | len, | 1774 | 204k | ); | 1775 | 204k | data | 1776 | 204k | }), | 1777 | 204k | } | 1778 | | } else { | 1779 | 0 | let mut b = slice.to_vec(); | 1780 | 0 | let cap = b.capacity(); | 1781 | 0 | let ptr = NonNull::new(b.as_mut_ptr()).expect("Vec always contain non null pointers."); | 1782 | 0 | mem::forget(b); | 1783 | 0 | SmallVec { | 1784 | 0 | capacity: cap, | 1785 | 0 | data: SmallVecData::from_heap(ptr, len), | 1786 | 0 | } | 1787 | | } | 1788 | 204k | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::from_slice |
1789 | | |
1790 | | /// Copy elements from a slice into the vector at position `index`, shifting any following |
1791 | | /// elements toward the back. |
1792 | | /// |
1793 | | /// For slices of `Copy` types, this is more efficient than `insert`. |
1794 | | #[inline] |
1795 | 0 | pub fn insert_from_slice(&mut self, index: usize, slice: &[A::Item]) { |
1796 | 0 | self.reserve(slice.len()); |
1797 | | |
1798 | 0 | let len = self.len(); |
1799 | 0 | assert!(index <= len); |
1800 | | |
1801 | 0 | unsafe { |
1802 | 0 | let slice_ptr = slice.as_ptr(); |
1803 | 0 | let ptr = self.as_mut_ptr().add(index); |
1804 | 0 | ptr::copy(ptr, ptr.add(slice.len()), len - index); |
1805 | 0 | ptr::copy_nonoverlapping(slice_ptr, ptr, slice.len()); |
1806 | 0 | self.set_len(len + slice.len()); |
1807 | 0 | } |
1808 | 0 | } |
1809 | | |
1810 | | /// Copy elements from a slice and append them to the vector. |
1811 | | /// |
1812 | | /// For slices of `Copy` types, this is more efficient than `extend`. |
1813 | | #[inline] |
1814 | 0 | pub fn extend_from_slice(&mut self, slice: &[A::Item]) { |
1815 | 0 | let len = self.len(); |
1816 | 0 | self.insert_from_slice(len, slice); |
1817 | 0 | } |
1818 | | } |
1819 | | |
1820 | | impl<A: Array> SmallVec<A> |
1821 | | where |
1822 | | A::Item: Clone, |
1823 | | { |
1824 | | /// Resizes the vector so that its length is equal to `len`. |
1825 | | /// |
1826 | | /// If `len` is less than the current length, the vector simply truncated. |
1827 | | /// |
1828 | | /// If `len` is greater than the current length, `value` is appended to the |
1829 | | /// vector until its length equals `len`. |
1830 | 1.75M | pub fn resize(&mut self, len: usize, value: A::Item) { |
1831 | 1.75M | let old_len = self.len(); |
1832 | | |
1833 | 1.75M | if len > old_len { |
1834 | 1.75M | self.extend(repeat(value).take(len - old_len)); |
1835 | 1.75M | } else { |
1836 | 0 | self.truncate(len); |
1837 | 0 | } |
1838 | 1.75M | } <smallvec::SmallVec<[u8; 64]>>::resize Line | Count | Source | 1830 | 1.75M | pub fn resize(&mut self, len: usize, value: A::Item) { | 1831 | 1.75M | let old_len = self.len(); | 1832 | | | 1833 | 1.75M | if len > old_len { | 1834 | 1.75M | self.extend(repeat(value).take(len - old_len)); | 1835 | 1.75M | } else { | 1836 | 0 | self.truncate(len); | 1837 | 0 | } | 1838 | 1.75M | } |
Unexecuted instantiation: <smallvec::SmallVec<_>>::resize |
1839 | | |
1840 | | /// Creates a `SmallVec` with `n` copies of `elem`. |
1841 | | /// ``` |
1842 | | /// use smallvec::SmallVec; |
1843 | | /// |
1844 | | /// let v = SmallVec::<[char; 128]>::from_elem('d', 2); |
1845 | | /// assert_eq!(v, SmallVec::from_buf(['d', 'd'])); |
1846 | | /// ``` |
1847 | 0 | pub fn from_elem(elem: A::Item, n: usize) -> Self { |
1848 | 0 | if n > Self::inline_capacity() { |
1849 | 0 | vec![elem; n].into() |
1850 | | } else { |
1851 | 0 | let mut v = SmallVec::<A>::new(); |
1852 | | unsafe { |
1853 | 0 | let (ptr, len_ptr, _) = v.triple_mut(); |
1854 | 0 | let ptr = ptr.as_ptr(); |
1855 | 0 | let mut local_len = SetLenOnDrop::new(len_ptr); |
1856 | | |
1857 | 0 | for i in 0..n { |
1858 | 0 | ::core::ptr::write(ptr.add(i), elem.clone()); |
1859 | 0 | local_len.increment_len(1); |
1860 | 0 | } |
1861 | | } |
1862 | 0 | v |
1863 | | } |
1864 | 0 | } |
1865 | | } |
1866 | | |
1867 | | impl<A: Array> ops::Deref for SmallVec<A> { |
1868 | | type Target = [A::Item]; |
1869 | | #[inline] |
1870 | 22.8M | fn deref(&self) -> &[A::Item] { |
1871 | | unsafe { |
1872 | 22.8M | let (ptr, len, _) = self.triple(); |
1873 | 22.8M | slice::from_raw_parts(ptr.as_ptr(), len) |
1874 | | } |
1875 | 22.8M | } <smallvec::SmallVec<[u8; 64]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 1.99M | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 1.99M | let (ptr, len, _) = self.triple(); | 1873 | 1.99M | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 1.99M | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 1.63M | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 1.63M | let (ptr, len, _) = self.triple(); | 1873 | 1.63M | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 1.63M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::deref::Deref>::deref Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::ops::deref::Deref>::deref <smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 4.60M | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 4.60M | let (ptr, len, _) = self.triple(); | 1873 | 4.60M | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 4.60M | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 1.10M | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 1.10M | let (ptr, len, _) = self.triple(); | 1873 | 1.10M | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 1.10M | } |
<smallvec::SmallVec<[u8; 16]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 176k | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 176k | let (ptr, len, _) = self.triple(); | 1873 | 176k | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 176k | } |
<smallvec::SmallVec<[u8; 24]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 13.3M | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 13.3M | let (ptr, len, _) = self.triple(); | 1873 | 13.3M | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 13.3M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]> as core::ops::deref::Deref>::deref Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::ops::deref::Deref>::deref <smallvec::SmallVec<[u32; 2]> as core::ops::deref::Deref>::deref Line | Count | Source | 1870 | 11.3k | fn deref(&self) -> &[A::Item] { | 1871 | | unsafe { | 1872 | 11.3k | let (ptr, len, _) = self.triple(); | 1873 | 11.3k | slice::from_raw_parts(ptr.as_ptr(), len) | 1874 | | } | 1875 | 11.3k | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::ops::deref::Deref>::deref |
1876 | | } |
1877 | | |
1878 | | impl<A: Array> ops::DerefMut for SmallVec<A> { |
1879 | | #[inline] |
1880 | 15.1M | fn deref_mut(&mut self) -> &mut [A::Item] { |
1881 | 15.1M | unsafe { |
1882 | 15.1M | let (ptr, &mut len, _) = self.triple_mut(); |
1883 | 15.1M | slice::from_raw_parts_mut(ptr.as_ptr(), len) |
1884 | 15.1M | } |
1885 | 15.1M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 760k | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 760k | unsafe { | 1882 | 760k | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 760k | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 760k | } | 1885 | 760k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 766k | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 766k | unsafe { | 1882 | 766k | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 766k | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 766k | } | 1885 | 766k | } |
<smallvec::SmallVec<[u8; 64]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 3.58M | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 3.58M | unsafe { | 1882 | 3.58M | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 3.58M | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 3.58M | } | 1885 | 3.58M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::ops::deref::DerefMut>::deref_mut <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 856k | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 856k | unsafe { | 1882 | 856k | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 856k | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 856k | } | 1885 | 856k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]> as core::ops::deref::DerefMut>::deref_mut <smallvec::SmallVec<[u8; 16]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 867k | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 867k | unsafe { | 1882 | 867k | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 867k | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 867k | } | 1885 | 867k | } |
<smallvec::SmallVec<[u8; 24]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 8.36M | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 8.36M | unsafe { | 1882 | 8.36M | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 8.36M | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 8.36M | } | 1885 | 8.36M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::ops::deref::DerefMut>::deref_mut <smallvec::SmallVec<[u32; 2]> as core::ops::deref::DerefMut>::deref_mut Line | Count | Source | 1880 | 9 | fn deref_mut(&mut self) -> &mut [A::Item] { | 1881 | 9 | unsafe { | 1882 | 9 | let (ptr, &mut len, _) = self.triple_mut(); | 1883 | 9 | slice::from_raw_parts_mut(ptr.as_ptr(), len) | 1884 | 9 | } | 1885 | 9 | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::ops::deref::DerefMut>::deref_mut |
1886 | | } |
1887 | | |
1888 | | impl<A: Array> AsRef<[A::Item]> for SmallVec<A> { |
1889 | | #[inline] |
1890 | 0 | fn as_ref(&self) -> &[A::Item] { |
1891 | 0 | self |
1892 | 0 | } |
1893 | | } |
1894 | | |
1895 | | impl<A: Array> AsMut<[A::Item]> for SmallVec<A> { |
1896 | | #[inline] |
1897 | 1.75M | fn as_mut(&mut self) -> &mut [A::Item] { |
1898 | 1.75M | self |
1899 | 1.75M | } <smallvec::SmallVec<[u8; 64]> as core::convert::AsMut<[u8]>>::as_mut Line | Count | Source | 1897 | 1.75M | fn as_mut(&mut self) -> &mut [A::Item] { | 1898 | 1.75M | self | 1899 | 1.75M | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::convert::AsMut<[<_ as smallvec::Array>::Item]>>::as_mut |
1900 | | } |
1901 | | |
1902 | | impl<A: Array> Borrow<[A::Item]> for SmallVec<A> { |
1903 | | #[inline] |
1904 | 0 | fn borrow(&self) -> &[A::Item] { |
1905 | 0 | self |
1906 | 0 | } |
1907 | | } |
1908 | | |
1909 | | impl<A: Array> BorrowMut<[A::Item]> for SmallVec<A> { |
1910 | | #[inline] |
1911 | 0 | fn borrow_mut(&mut self) -> &mut [A::Item] { |
1912 | 0 | self |
1913 | 0 | } |
1914 | | } |
1915 | | |
1916 | | #[cfg(feature = "write")] |
1917 | | #[cfg_attr(docsrs, doc(cfg(feature = "write")))] |
1918 | | impl<A: Array<Item = u8>> io::Write for SmallVec<A> { |
1919 | | #[inline] |
1920 | | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
1921 | | self.extend_from_slice(buf); |
1922 | | Ok(buf.len()) |
1923 | | } |
1924 | | |
1925 | | #[inline] |
1926 | | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
1927 | | self.extend_from_slice(buf); |
1928 | | Ok(()) |
1929 | | } |
1930 | | |
1931 | | #[inline] |
1932 | | fn flush(&mut self) -> io::Result<()> { |
1933 | | Ok(()) |
1934 | | } |
1935 | | } |
1936 | | |
1937 | | #[cfg(feature = "serde")] |
1938 | | #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] |
1939 | | impl<A: Array> Serialize for SmallVec<A> |
1940 | | where |
1941 | | A::Item: Serialize, |
1942 | | { |
1943 | | fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
1944 | | let mut state = serializer.serialize_seq(Some(self.len()))?; |
1945 | | for item in self { |
1946 | | state.serialize_element(&item)?; |
1947 | | } |
1948 | | state.end() |
1949 | | } |
1950 | | } |
1951 | | |
1952 | | #[cfg(feature = "serde")] |
1953 | | #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] |
1954 | | impl<'de, A: Array> Deserialize<'de> for SmallVec<A> |
1955 | | where |
1956 | | A::Item: Deserialize<'de>, |
1957 | | { |
1958 | | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
1959 | | deserializer.deserialize_seq(SmallVecVisitor { |
1960 | | phantom: PhantomData, |
1961 | | }) |
1962 | | } |
1963 | | } |
1964 | | |
1965 | | #[cfg(feature = "serde")] |
1966 | | struct SmallVecVisitor<A> { |
1967 | | phantom: PhantomData<A>, |
1968 | | } |
1969 | | |
1970 | | #[cfg(feature = "serde")] |
1971 | | impl<'de, A: Array> Visitor<'de> for SmallVecVisitor<A> |
1972 | | where |
1973 | | A::Item: Deserialize<'de>, |
1974 | | { |
1975 | | type Value = SmallVec<A>; |
1976 | | |
1977 | | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
1978 | | formatter.write_str("a sequence") |
1979 | | } |
1980 | | |
1981 | | fn visit_seq<B>(self, mut seq: B) -> Result<Self::Value, B::Error> |
1982 | | where |
1983 | | B: SeqAccess<'de>, |
1984 | | { |
1985 | | use serde::de::Error; |
1986 | | let len = seq.size_hint().unwrap_or(0); |
1987 | | let mut values = SmallVec::new(); |
1988 | | values.try_reserve(len).map_err(B::Error::custom)?; |
1989 | | |
1990 | | while let Some(value) = seq.next_element()? { |
1991 | | values.push(value); |
1992 | | } |
1993 | | |
1994 | | Ok(values) |
1995 | | } |
1996 | | } |
1997 | | |
1998 | | #[cfg(feature = "malloc_size_of")] |
1999 | | impl<A: Array> MallocShallowSizeOf for SmallVec<A> { |
2000 | | fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { |
2001 | | if self.spilled() { |
2002 | | unsafe { ops.malloc_size_of(self.as_ptr()) } |
2003 | | } else { |
2004 | | 0 |
2005 | | } |
2006 | | } |
2007 | | } |
2008 | | |
2009 | | #[cfg(feature = "malloc_size_of")] |
2010 | | impl<A> MallocSizeOf for SmallVec<A> |
2011 | | where |
2012 | | A: Array, |
2013 | | A::Item: MallocSizeOf, |
2014 | | { |
2015 | | fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { |
2016 | | let mut n = self.shallow_size_of(ops); |
2017 | | for elem in self.iter() { |
2018 | | n += elem.size_of(ops); |
2019 | | } |
2020 | | n |
2021 | | } |
2022 | | } |
2023 | | |
2024 | | #[cfg(feature = "specialization")] |
2025 | | trait SpecFrom<A: Array, S> { |
2026 | | fn spec_from(slice: S) -> SmallVec<A>; |
2027 | | } |
2028 | | |
2029 | | #[cfg(feature = "specialization")] |
2030 | | mod specialization; |
2031 | | |
2032 | | #[cfg(feature = "arbitrary")] |
2033 | | mod arbitrary; |
2034 | | |
2035 | | #[cfg(feature = "specialization")] |
2036 | | impl<'a, A: Array> SpecFrom<A, &'a [A::Item]> for SmallVec<A> |
2037 | | where |
2038 | | A::Item: Copy, |
2039 | | { |
2040 | | #[inline] |
2041 | | fn spec_from(slice: &'a [A::Item]) -> SmallVec<A> { |
2042 | | SmallVec::from_slice(slice) |
2043 | | } |
2044 | | } |
2045 | | |
2046 | | impl<'a, A: Array> From<&'a [A::Item]> for SmallVec<A> |
2047 | | where |
2048 | | A::Item: Clone, |
2049 | | { |
2050 | | #[cfg(not(feature = "specialization"))] |
2051 | | #[inline] |
2052 | 7.09M | fn from(slice: &'a [A::Item]) -> SmallVec<A> { |
2053 | 7.09M | slice.iter().cloned().collect() |
2054 | 7.09M | } <smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::convert::From<&[exr::meta::header::Header]>>::from Line | Count | Source | 2052 | 760k | fn from(slice: &'a [A::Item]) -> SmallVec<A> { | 2053 | 760k | slice.iter().cloned().collect() | 2054 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::convert::From<&[exr::meta::attribute::ChannelDescription]>>::from Line | Count | Source | 2052 | 776k | fn from(slice: &'a [A::Item]) -> SmallVec<A> { | 2053 | 776k | slice.iter().cloned().collect() | 2054 | 776k | } |
<smallvec::SmallVec<[u8; 16]> as core::convert::From<&[u8]>>::from Line | Count | Source | 2052 | 1.07M | fn from(slice: &'a [A::Item]) -> SmallVec<A> { | 2053 | 1.07M | slice.iter().cloned().collect() | 2054 | 1.07M | } |
<smallvec::SmallVec<[u8; 24]> as core::convert::From<&[u8]>>::from Line | Count | Source | 2052 | 4.48M | fn from(slice: &'a [A::Item]) -> SmallVec<A> { | 2053 | 4.48M | slice.iter().cloned().collect() | 2054 | 4.48M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]> as core::convert::From<&[u32]>>::from Unexecuted instantiation: <smallvec::SmallVec<_> as core::convert::From<&[<_ as smallvec::Array>::Item]>>::from |
2055 | | |
2056 | | #[cfg(feature = "specialization")] |
2057 | | #[inline] |
2058 | | fn from(slice: &'a [A::Item]) -> SmallVec<A> { |
2059 | | SmallVec::spec_from(slice) |
2060 | | } |
2061 | | } |
2062 | | |
2063 | | impl<A: Array> From<Vec<A::Item>> for SmallVec<A> { |
2064 | | #[inline] |
2065 | 0 | fn from(vec: Vec<A::Item>) -> SmallVec<A> { |
2066 | 0 | SmallVec::from_vec(vec) |
2067 | 0 | } |
2068 | | } |
2069 | | |
2070 | | impl<A: Array> From<A> for SmallVec<A> { |
2071 | | #[inline] |
2072 | 0 | fn from(array: A) -> SmallVec<A> { |
2073 | 0 | SmallVec::from_buf(array) |
2074 | 0 | } |
2075 | | } |
2076 | | |
2077 | | impl<A: Array, I: SliceIndex<[A::Item]>> ops::Index<I> for SmallVec<A> { |
2078 | | type Output = I::Output; |
2079 | | |
2080 | 4.58M | fn index(&self, index: I) -> &I::Output { |
2081 | 4.58M | &(**self)[index] |
2082 | 4.58M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::index::Index<usize>>::index Line | Count | Source | 2080 | 1.63M | fn index(&self, index: I) -> &I::Output { | 2081 | 1.63M | &(**self)[index] | 2082 | 1.63M | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::ops::index::Index<usize>>::index Line | Count | Source | 2080 | 776k | fn index(&self, index: I) -> &I::Output { | 2081 | 776k | &(**self)[index] | 2082 | 776k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::index::Index<usize>>::index Unexecuted instantiation: <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::index::Index<core::ops::range::RangeFull>>::index Unexecuted instantiation: <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::index::Index<usize>>::index Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]> as core::ops::index::Index<core::ops::range::RangeFull>>::index <smallvec::SmallVec<[u8; 24]> as core::ops::index::Index<core::ops::range::RangeFull>>::index Line | Count | Source | 2080 | 2.17M | fn index(&self, index: I) -> &I::Output { | 2081 | 2.17M | &(**self)[index] | 2082 | 2.17M | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::ops::index::Index<usize>>::index Unexecuted instantiation: <smallvec::SmallVec<_> as core::ops::index::Index<_>>::index |
2083 | | } |
2084 | | |
2085 | | impl<A: Array, I: SliceIndex<[A::Item]>> ops::IndexMut<I> for SmallVec<A> { |
2086 | 13.4M | fn index_mut(&mut self, index: I) -> &mut I::Output { |
2087 | 13.4M | &mut (&mut **self)[index] |
2088 | 13.4M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 2.44k | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 2.44k | &mut (&mut **self)[index] | 2088 | 2.44k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 766k | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 766k | &mut (&mut **self)[index] | 2088 | 766k | } |
<smallvec::SmallVec<[u8; 64]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 1.82M | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 1.82M | &mut (&mut **self)[index] | 2088 | 1.82M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 855k | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 855k | &mut (&mut **self)[index] | 2088 | 855k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut <smallvec::SmallVec<[u8; 16]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 867k | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 867k | &mut (&mut **self)[index] | 2088 | 867k | } |
<smallvec::SmallVec<[u8; 24]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 8.36M | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 8.36M | &mut (&mut **self)[index] | 2088 | 8.36M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut <smallvec::SmallVec<[u32; 2]> as core::ops::index::IndexMut<core::ops::range::RangeFull>>::index_mut Line | Count | Source | 2086 | 9 | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 9 | &mut (&mut **self)[index] | 2088 | 9 | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::ops::index::IndexMut<_>>::index_mut Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::index::IndexMut<usize>>::index_mut <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::index::IndexMut<usize>>::index_mut Line | Count | Source | 2086 | 757k | fn index_mut(&mut self, index: I) -> &mut I::Output { | 2087 | 757k | &mut (&mut **self)[index] | 2088 | 757k | } |
|
2089 | | } |
2090 | | |
2091 | | #[allow(deprecated)] |
2092 | | impl<A: Array> ExtendFromSlice<A::Item> for SmallVec<A> |
2093 | | where |
2094 | | A::Item: Copy, |
2095 | | { |
2096 | 0 | fn extend_from_slice(&mut self, other: &[A::Item]) { |
2097 | 0 | SmallVec::extend_from_slice(self, other) |
2098 | 0 | } |
2099 | | } |
2100 | | |
2101 | | impl<A: Array> FromIterator<A::Item> for SmallVec<A> { |
2102 | | #[inline] |
2103 | 7.15M | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { |
2104 | 7.15M | let mut v = SmallVec::new(); |
2105 | 7.15M | v.extend(iterable); |
2106 | 7.15M | v |
2107 | 7.15M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::FromIterator<alloc::vec::Vec<u64>>>::from_iter::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::meta::MetaData>::read_offset_tables<exr::io::Tracking<std::io::cursor::Cursor<&[u8]>>>::{closure#0}>, core::result::Result<core::convert::Infallible, exr::error::Error>>>Line | Count | Source | 2103 | 2.69k | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 2.69k | let mut v = SmallVec::new(); | 2105 | 2.69k | v.extend(iterable); | 2106 | 2.69k | v | 2107 | 2.69k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::iter::traits::collect::FromIterator<exr::meta::header::Header>>::from_iter::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<exr::meta::header::Header>>> Line | Count | Source | 2103 | 760k | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 760k | let mut v = SmallVec::new(); | 2105 | 760k | v.extend(iterable); | 2106 | 760k | v | 2107 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::iter::traits::collect::FromIterator<exr::meta::attribute::ChannelDescription>>::from_iter::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>>> Line | Count | Source | 2103 | 776k | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 776k | let mut v = SmallVec::new(); | 2105 | 776k | v.extend(iterable); | 2106 | 776k | v | 2107 | 776k | } |
<smallvec::SmallVec<[u8; 16]> as core::iter::traits::collect::FromIterator<u8>>::from_iter::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u8>>> Line | Count | Source | 2103 | 1.07M | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 1.07M | let mut v = SmallVec::new(); | 2105 | 1.07M | v.extend(iterable); | 2106 | 1.07M | v | 2107 | 1.07M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::iter::traits::collect::FromIterator<exr::image::AnyChannel<exr::image::FlatSamples>>>::from_iter::<core::iter::adapters::map::Map<smallvec::IntoIter<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>, <exr::image::Layer<exr::image::crop::CroppedChannels<exr::image::AnyChannels<exr::image::FlatSamples>>> as exr::image::crop::ApplyCroppedView>::reallocate_cropped::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::FromIterator<exr::compression::piz::ChannelData>>::from_iter::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, exr::compression::piz::decompress::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::FromIterator<exr::compression::piz::ChannelData>>::from_iter::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, exr::compression::piz::compress::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]> as core::iter::traits::collect::FromIterator<exr::block::samples::Sample>>::from_iter::<exr::image::FlatSampleIterator> <smallvec::SmallVec<[u8; 24]> as core::iter::traits::collect::FromIterator<u8>>::from_iter::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::str::iter::Chars, <exr::meta::attribute::Text>::new_or_none<&str>::{closure#0}>, core::option::Option<core::convert::Infallible>>>Line | Count | Source | 2103 | 41.3k | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 41.3k | let mut v = SmallVec::new(); | 2105 | 41.3k | v.extend(iterable); | 2106 | 41.3k | v | 2107 | 41.3k | } |
<smallvec::SmallVec<[u8; 24]> as core::iter::traits::collect::FromIterator<u8>>::from_iter::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u8>>> Line | Count | Source | 2103 | 4.48M | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 4.48M | let mut v = SmallVec::new(); | 2105 | 4.48M | v.extend(iterable); | 2106 | 4.48M | v | 2107 | 4.48M | } |
<smallvec::SmallVec<[u8; 8]> as core::iter::traits::collect::FromIterator<u8>>::from_iter::<core::iter::adapters::map::Map<core::ops::range::Range<usize>, <exr::meta::attribute::TimeCode>::unpack_user_data_from_u32::{closure#0}>>Line | Count | Source | 2103 | 22.4k | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 22.4k | let mut v = SmallVec::new(); | 2105 | 22.4k | v.extend(iterable); | 2106 | 22.4k | v | 2107 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::iter::traits::collect::FromIterator<usize>>::from_iter::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, <exr::block::lines::LineIndex>::lines_in_block::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]> as core::iter::traits::collect::FromIterator<u32>>::from_iter::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u32>>> Unexecuted instantiation: <smallvec::SmallVec<_> as core::iter::traits::collect::FromIterator<<_ as smallvec::Array>::Item>>::from_iter::<_> Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::FromIterator<alloc::vec::Vec<u64>>>::from_iter::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::block::writer::ChunkWriter<&mut &mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::new_for_buffered::{closure#1}>><smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::FromIterator<alloc::vec::Vec<u64>>>::from_iter::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::meta::MetaData>::read_offset_tables<exr::io::Tracking<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::{closure#0}>, core::result::Result<core::convert::Infallible, exr::error::Error>>>Line | Count | Source | 2103 | 83 | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 83 | let mut v = SmallVec::new(); | 2105 | 83 | v.extend(iterable); | 2106 | 83 | v | 2107 | 83 | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::FromIterator<alloc::vec::Vec<u64>>>::from_iter::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::block::writer::ChunkWriter<&mut std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>>>::new_for_buffered::{closure#1}>>Line | Count | Source | 2103 | 83 | fn from_iter<I: IntoIterator<Item = A::Item>>(iterable: I) -> SmallVec<A> { | 2104 | 83 | let mut v = SmallVec::new(); | 2105 | 83 | v.extend(iterable); | 2106 | 83 | v | 2107 | 83 | } |
|
2108 | | } |
2109 | | |
2110 | | impl<A: Array> Extend<A::Item> for SmallVec<A> { |
2111 | 8.91M | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { |
2112 | 8.91M | let mut iter = iterable.into_iter(); |
2113 | 8.91M | let (lower_size_bound, _) = iter.size_hint(); |
2114 | 8.91M | self.reserve(lower_size_bound); |
2115 | | |
2116 | | unsafe { |
2117 | 8.91M | let (ptr, len_ptr, cap) = self.triple_mut(); |
2118 | 8.91M | let ptr = ptr.as_ptr(); |
2119 | 8.91M | let mut len = SetLenOnDrop::new(len_ptr); |
2120 | 79.3M | while len.get() < cap { |
2121 | 79.1M | if let Some(out) = iter.next() { |
2122 | 70.4M | ptr::write(ptr.add(len.get()), out); |
2123 | 70.4M | len.increment_len(1); |
2124 | 70.4M | } else { |
2125 | 8.70M | return; |
2126 | | } |
2127 | | } |
2128 | | } |
2129 | | |
2130 | 222k | for elem in iter { |
2131 | 13.6k | self.push(elem); |
2132 | 13.6k | } |
2133 | 8.91M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::Extend<alloc::vec::Vec<u64>>>::extend::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::meta::MetaData>::read_offset_tables<exr::io::Tracking<std::io::cursor::Cursor<&[u8]>>>::{closure#0}>, core::result::Result<core::convert::Infallible, exr::error::Error>>>Line | Count | Source | 2111 | 2.69k | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 2.69k | let mut iter = iterable.into_iter(); | 2113 | 2.69k | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 2.69k | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 2.69k | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 2.69k | let ptr = ptr.as_ptr(); | 2119 | 2.69k | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 6.19k | while len.get() < cap { | 2121 | 5.73k | if let Some(out) = iter.next() { | 2122 | 3.49k | ptr::write(ptr.add(len.get()), out); | 2123 | 3.49k | len.increment_len(1); | 2124 | 3.49k | } else { | 2125 | 2.23k | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 14.1k | for elem in iter { | 2131 | 13.6k | self.push(elem); | 2132 | 13.6k | } | 2133 | 2.69k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::iter::traits::collect::Extend<exr::meta::header::Header>>::extend::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<exr::meta::header::Header>>> Line | Count | Source | 2111 | 760k | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 760k | let mut iter = iterable.into_iter(); | 2113 | 760k | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 760k | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 760k | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 760k | let ptr = ptr.as_ptr(); | 2119 | 760k | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 1.53M | while len.get() < cap { | 2121 | 1.53M | if let Some(out) = iter.next() { | 2122 | 772k | ptr::write(ptr.add(len.get()), out); | 2123 | 772k | len.increment_len(1); | 2124 | 772k | } else { | 2125 | 759k | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 107 | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::iter::traits::collect::Extend<exr::meta::attribute::ChannelDescription>>::extend::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>>> Line | Count | Source | 2111 | 776k | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 776k | let mut iter = iterable.into_iter(); | 2113 | 776k | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 776k | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 776k | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 776k | let ptr = ptr.as_ptr(); | 2119 | 776k | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 3.85M | while len.get() < cap { | 2121 | 3.85M | if let Some(out) = iter.next() { | 2122 | 3.08M | ptr::write(ptr.add(len.get()), out); | 2123 | 3.08M | len.increment_len(1); | 2124 | 3.08M | } else { | 2125 | 776k | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 0 | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 776k | } |
<smallvec::SmallVec<[u8; 16]> as core::iter::traits::collect::Extend<u8>>::extend::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u8>>> Line | Count | Source | 2111 | 1.07M | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 1.07M | let mut iter = iterable.into_iter(); | 2113 | 1.07M | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 1.07M | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 1.07M | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 1.07M | let ptr = ptr.as_ptr(); | 2119 | 1.07M | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 15.1M | while len.get() < cap { | 2121 | 15.0M | if let Some(out) = iter.next() { | 2122 | 14.0M | ptr::write(ptr.add(len.get()), out); | 2123 | 14.0M | len.increment_len(1); | 2124 | 14.0M | } else { | 2125 | 988k | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 82.1k | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 1.07M | } |
<smallvec::SmallVec<[u8; 64]> as core::iter::traits::collect::Extend<u8>>::extend::<core::iter::adapters::take::Take<core::iter::sources::repeat::Repeat<u8>>> Line | Count | Source | 2111 | 1.75M | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 1.75M | let mut iter = iterable.into_iter(); | 2113 | 1.75M | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 1.75M | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 1.75M | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 1.75M | let ptr = ptr.as_ptr(); | 2119 | 1.75M | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 37.2M | while len.get() < cap { | 2121 | 37.1M | if let Some(out) = iter.next() { | 2122 | 35.5M | ptr::write(ptr.add(len.get()), out); | 2123 | 35.5M | len.increment_len(1); | 2124 | 35.5M | } else { | 2125 | 1.65M | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 97.4k | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 1.75M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::iter::traits::collect::Extend<exr::image::AnyChannel<exr::image::FlatSamples>>>::extend::<core::iter::adapters::map::Map<smallvec::IntoIter<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]>, <exr::image::Layer<exr::image::crop::CroppedChannels<exr::image::AnyChannels<exr::image::FlatSamples>>> as exr::image::crop::ApplyCroppedView>::reallocate_cropped::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::Extend<exr::compression::piz::ChannelData>>::extend::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, exr::compression::piz::decompress::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::Extend<exr::compression::piz::ChannelData>>::extend::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, exr::compression::piz::compress::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]> as core::iter::traits::collect::Extend<exr::block::samples::Sample>>::extend::<exr::image::FlatSampleIterator> <smallvec::SmallVec<[u8; 24]> as core::iter::traits::collect::Extend<u8>>::extend::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::str::iter::Chars, <exr::meta::attribute::Text>::new_or_none<&str>::{closure#0}>, core::option::Option<core::convert::Infallible>>>Line | Count | Source | 2111 | 41.3k | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 41.3k | let mut iter = iterable.into_iter(); | 2113 | 41.3k | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 41.3k | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 41.3k | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 41.3k | let ptr = ptr.as_ptr(); | 2119 | 41.3k | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 82.7k | while len.get() < cap { | 2121 | 82.7k | if let Some(out) = iter.next() { | 2122 | 41.3k | ptr::write(ptr.add(len.get()), out); | 2123 | 41.3k | len.increment_len(1); | 2124 | 41.3k | } else { | 2125 | 41.3k | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 0 | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 41.3k | } |
<smallvec::SmallVec<[u8; 24]> as core::iter::traits::collect::Extend<u8>>::extend::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u8>>> Line | Count | Source | 2111 | 4.48M | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 4.48M | let mut iter = iterable.into_iter(); | 2113 | 4.48M | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 4.48M | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 4.48M | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 4.48M | let ptr = ptr.as_ptr(); | 2119 | 4.48M | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 21.2M | while len.get() < cap { | 2121 | 21.2M | if let Some(out) = iter.next() { | 2122 | 16.7M | ptr::write(ptr.add(len.get()), out); | 2123 | 16.7M | len.increment_len(1); | 2124 | 16.7M | } else { | 2125 | 4.47M | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 6.59k | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 4.48M | } |
<smallvec::SmallVec<[u8; 8]> as core::iter::traits::collect::Extend<u8>>::extend::<core::iter::adapters::map::Map<core::ops::range::Range<usize>, <exr::meta::attribute::TimeCode>::unpack_user_data_from_u32::{closure#0}>>Line | Count | Source | 2111 | 22.4k | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 22.4k | let mut iter = iterable.into_iter(); | 2113 | 22.4k | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 22.4k | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 22.4k | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 22.4k | let ptr = ptr.as_ptr(); | 2119 | 22.4k | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 201k | while len.get() < cap { | 2121 | 179k | if let Some(out) = iter.next() { | 2122 | 179k | ptr::write(ptr.add(len.get()), out); | 2123 | 179k | len.increment_len(1); | 2124 | 179k | } else { | 2125 | 0 | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 22.4k | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 22.4k | } |
Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::iter::traits::collect::Extend<usize>>::extend::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::attribute::ChannelDescription>, <exr::block::lines::LineIndex>::lines_in_block::{closure#0}>>Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]> as core::iter::traits::collect::Extend<u32>>::extend::<core::iter::adapters::cloned::Cloned<core::slice::iter::Iter<u32>>> Unexecuted instantiation: <smallvec::SmallVec<_> as core::iter::traits::collect::Extend<<_ as smallvec::Array>::Item>>::extend::<_> Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::Extend<alloc::vec::Vec<u64>>>::extend::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::block::writer::ChunkWriter<&mut &mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::new_for_buffered::{closure#1}>><smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::Extend<alloc::vec::Vec<u64>>>::extend::<core::iter::adapters::GenericShunt<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::meta::MetaData>::read_offset_tables<exr::io::Tracking<std::io::cursor::Cursor<alloc::vec::Vec<u8>>>>::{closure#0}>, core::result::Result<core::convert::Infallible, exr::error::Error>>>Line | Count | Source | 2111 | 83 | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 83 | let mut iter = iterable.into_iter(); | 2113 | 83 | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 83 | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 83 | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 83 | let ptr = ptr.as_ptr(); | 2119 | 83 | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 166 | while len.get() < cap { | 2121 | 166 | if let Some(out) = iter.next() { | 2122 | 83 | ptr::write(ptr.add(len.get()), out); | 2123 | 83 | len.increment_len(1); | 2124 | 83 | } else { | 2125 | 83 | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 0 | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 83 | } |
<smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::Extend<alloc::vec::Vec<u64>>>::extend::<core::iter::adapters::map::Map<core::slice::iter::Iter<exr::meta::header::Header>, <exr::block::writer::ChunkWriter<&mut std::io::cursor::Cursor<&mut alloc::vec::Vec<u8>>>>::new_for_buffered::{closure#1}>>Line | Count | Source | 2111 | 83 | fn extend<I: IntoIterator<Item = A::Item>>(&mut self, iterable: I) { | 2112 | 83 | let mut iter = iterable.into_iter(); | 2113 | 83 | let (lower_size_bound, _) = iter.size_hint(); | 2114 | 83 | self.reserve(lower_size_bound); | 2115 | | | 2116 | | unsafe { | 2117 | 83 | let (ptr, len_ptr, cap) = self.triple_mut(); | 2118 | 83 | let ptr = ptr.as_ptr(); | 2119 | 83 | let mut len = SetLenOnDrop::new(len_ptr); | 2120 | 166 | while len.get() < cap { | 2121 | 166 | if let Some(out) = iter.next() { | 2122 | 83 | ptr::write(ptr.add(len.get()), out); | 2123 | 83 | len.increment_len(1); | 2124 | 83 | } else { | 2125 | 83 | return; | 2126 | | } | 2127 | | } | 2128 | | } | 2129 | | | 2130 | 0 | for elem in iter { | 2131 | 0 | self.push(elem); | 2132 | 0 | } | 2133 | 83 | } |
|
2134 | | } |
2135 | | |
2136 | | impl<A: Array> fmt::Debug for SmallVec<A> |
2137 | | where |
2138 | | A::Item: fmt::Debug, |
2139 | | { |
2140 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
2141 | 0 | f.debug_list().entries(self.iter()).finish() |
2142 | 0 | } Unexecuted instantiation: <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::fmt::Debug>::fmt Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]> as core::fmt::Debug>::fmt Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]> as core::fmt::Debug>::fmt Unexecuted instantiation: <smallvec::SmallVec<_> as core::fmt::Debug>::fmt |
2143 | | } |
2144 | | |
2145 | | impl<A: Array> Default for SmallVec<A> { |
2146 | | #[inline] |
2147 | 0 | fn default() -> SmallVec<A> { |
2148 | 0 | SmallVec::new() |
2149 | 0 | } |
2150 | | } |
2151 | | |
2152 | | #[cfg(feature = "may_dangle")] |
2153 | | unsafe impl<#[may_dangle] A: Array> Drop for SmallVec<A> { |
2154 | | fn drop(&mut self) { |
2155 | | unsafe { |
2156 | | if self.spilled() { |
2157 | | let (ptr, &mut len) = self.data.heap_mut(); |
2158 | | Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity); |
2159 | | } else { |
2160 | | ptr::drop_in_place(&mut self[..]); |
2161 | | } |
2162 | | } |
2163 | | } |
2164 | | } |
2165 | | |
2166 | | #[cfg(not(feature = "may_dangle"))] |
2167 | | impl<A: Array> Drop for SmallVec<A> { |
2168 | 13.3M | fn drop(&mut self) { |
2169 | | unsafe { |
2170 | 13.3M | if self.spilled() { |
2171 | 645k | let (ptr, &mut len) = self.data.heap_mut(); |
2172 | 645k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); |
2173 | 12.6M | } else { |
2174 | 12.6M | ptr::drop_in_place(&mut self[..]); |
2175 | 12.6M | } |
2176 | | } |
2177 | 13.3M | } <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 2.86k | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 2.86k | if self.spilled() { | 2171 | 417 | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 417 | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 2.44k | } else { | 2174 | 2.44k | ptr::drop_in_place(&mut self[..]); | 2175 | 2.44k | } | 2176 | | } | 2177 | 2.86k | } |
<smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 767k | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 767k | if self.spilled() { | 2171 | 1.12k | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 1.12k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 766k | } else { | 2174 | 766k | ptr::drop_in_place(&mut self[..]); | 2175 | 766k | } | 2176 | | } | 2177 | 767k | } |
<smallvec::SmallVec<[u8; 64]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 1.88M | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 1.88M | if self.spilled() { | 2171 | 52.8k | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 52.8k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 1.82M | } else { | 2174 | 1.82M | ptr::drop_in_place(&mut self[..]); | 2175 | 1.82M | } | 2176 | | } | 2177 | 1.88M | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::drop::Drop>::drop Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::ops::drop::Drop>::drop <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 859k | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 859k | if self.spilled() { | 2171 | 3.16k | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 3.16k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 855k | } else { | 2174 | 855k | ptr::drop_in_place(&mut self[..]); | 2175 | 855k | } | 2176 | | } | 2177 | 859k | } |
Unexecuted instantiation: <smallvec::SmallVec<[exr::block::samples::Sample; 8]> as core::ops::drop::Drop>::drop <smallvec::SmallVec<[u8; 16]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 1.07M | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 1.07M | if self.spilled() { | 2171 | 203k | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 203k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 867k | } else { | 2174 | 867k | ptr::drop_in_place(&mut self[..]); | 2175 | 867k | } | 2176 | | } | 2177 | 1.07M | } |
<smallvec::SmallVec<[u8; 24]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 8.74M | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 8.74M | if self.spilled() { | 2171 | 384k | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 384k | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 8.36M | } else { | 2174 | 8.36M | ptr::drop_in_place(&mut self[..]); | 2175 | 8.36M | } | 2176 | | } | 2177 | 8.74M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u8; 8]> as core::ops::drop::Drop>::drop Unexecuted instantiation: <smallvec::SmallVec<[usize; 8]> as core::ops::drop::Drop>::drop <smallvec::SmallVec<[u32; 2]> as core::ops::drop::Drop>::drop Line | Count | Source | 2168 | 37 | fn drop(&mut self) { | 2169 | | unsafe { | 2170 | 37 | if self.spilled() { | 2171 | 28 | let (ptr, &mut len) = self.data.heap_mut(); | 2172 | 28 | drop(Vec::from_raw_parts(ptr.as_ptr(), len, self.capacity)); | 2173 | 28 | } else { | 2174 | 9 | ptr::drop_in_place(&mut self[..]); | 2175 | 9 | } | 2176 | | } | 2177 | 37 | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::ops::drop::Drop>::drop |
2178 | | } |
2179 | | |
2180 | | impl<A: Array> Clone for SmallVec<A> |
2181 | | where |
2182 | | A::Item: Clone, |
2183 | | { |
2184 | | #[inline] |
2185 | 6.19M | fn clone(&self) -> SmallVec<A> { |
2186 | 6.19M | SmallVec::from(self.as_slice()) |
2187 | 6.19M | } <smallvec::SmallVec<[exr::meta::header::Header; 3]> as core::clone::Clone>::clone Line | Count | Source | 2185 | 760k | fn clone(&self) -> SmallVec<A> { | 2186 | 760k | SmallVec::from(self.as_slice()) | 2187 | 760k | } |
<smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::clone::Clone>::clone Line | Count | Source | 2185 | 776k | fn clone(&self) -> SmallVec<A> { | 2186 | 776k | SmallVec::from(self.as_slice()) | 2187 | 776k | } |
<smallvec::SmallVec<[u8; 16]> as core::clone::Clone>::clone Line | Count | Source | 2185 | 176k | fn clone(&self) -> SmallVec<A> { | 2186 | 176k | SmallVec::from(self.as_slice()) | 2187 | 176k | } |
<smallvec::SmallVec<[u8; 24]> as core::clone::Clone>::clone Line | Count | Source | 2185 | 4.48M | fn clone(&self) -> SmallVec<A> { | 2186 | 4.48M | SmallVec::from(self.as_slice()) | 2187 | 4.48M | } |
Unexecuted instantiation: <smallvec::SmallVec<[u32; 2]> as core::clone::Clone>::clone Unexecuted instantiation: <smallvec::SmallVec<_> as core::clone::Clone>::clone |
2188 | | |
2189 | 0 | fn clone_from(&mut self, source: &Self) { |
2190 | | // Inspired from `impl Clone for Vec`. |
2191 | | |
2192 | | // drop anything that will not be overwritten |
2193 | 0 | self.truncate(source.len()); |
2194 | | |
2195 | | // self.len <= other.len due to the truncate above, so the |
2196 | | // slices here are always in-bounds. |
2197 | 0 | let (init, tail) = source.split_at(self.len()); |
2198 | | |
2199 | | // reuse the contained values' allocations/resources. |
2200 | 0 | self.clone_from_slice(init); |
2201 | 0 | self.extend(tail.iter().cloned()); |
2202 | 0 | } |
2203 | | } |
2204 | | |
2205 | | impl<A: Array, B: Array> PartialEq<SmallVec<B>> for SmallVec<A> |
2206 | | where |
2207 | | A::Item: PartialEq<B::Item>, |
2208 | | { |
2209 | | #[inline] |
2210 | 1.08M | fn eq(&self, other: &SmallVec<B>) -> bool { |
2211 | 1.08M | self[..] == other[..] |
2212 | 1.08M | } Unexecuted instantiation: <smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::cmp::PartialEq>::eq Unexecuted instantiation: <smallvec::SmallVec<[u8; 16]> as core::cmp::PartialEq>::eq <smallvec::SmallVec<[u8; 24]> as core::cmp::PartialEq>::eq Line | Count | Source | 2210 | 1.08M | fn eq(&self, other: &SmallVec<B>) -> bool { | 2211 | 1.08M | self[..] == other[..] | 2212 | 1.08M | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::cmp::PartialEq<smallvec::SmallVec<_>>>::eq |
2213 | | } |
2214 | | |
2215 | | impl<A: Array> Eq for SmallVec<A> where A::Item: Eq {} |
2216 | | |
2217 | | impl<A: Array> PartialOrd for SmallVec<A> |
2218 | | where |
2219 | | A::Item: PartialOrd, |
2220 | | { |
2221 | | #[inline] |
2222 | 53.3k | fn partial_cmp(&self, other: &SmallVec<A>) -> Option<cmp::Ordering> { |
2223 | 53.3k | PartialOrd::partial_cmp(&**self, &**other) |
2224 | 53.3k | } <smallvec::SmallVec<[u8; 24]> as core::cmp::PartialOrd>::partial_cmp Line | Count | Source | 2222 | 53.3k | fn partial_cmp(&self, other: &SmallVec<A>) -> Option<cmp::Ordering> { | 2223 | 53.3k | PartialOrd::partial_cmp(&**self, &**other) | 2224 | 53.3k | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::cmp::PartialOrd>::partial_cmp |
2225 | | } |
2226 | | |
2227 | | impl<A: Array> Ord for SmallVec<A> |
2228 | | where |
2229 | | A::Item: Ord, |
2230 | | { |
2231 | | #[inline] |
2232 | 0 | fn cmp(&self, other: &SmallVec<A>) -> cmp::Ordering { |
2233 | 0 | Ord::cmp(&**self, &**other) |
2234 | 0 | } |
2235 | | } |
2236 | | |
2237 | | impl<A: Array> Hash for SmallVec<A> |
2238 | | where |
2239 | | A::Item: Hash, |
2240 | | { |
2241 | 2.22M | fn hash<H: Hasher>(&self, state: &mut H) { |
2242 | 2.22M | (**self).hash(state) |
2243 | 2.22M | } <smallvec::SmallVec<[u8; 24]> as core::hash::Hash>::hash::<std::hash::random::DefaultHasher> Line | Count | Source | 2241 | 2.22M | fn hash<H: Hasher>(&self, state: &mut H) { | 2242 | 2.22M | (**self).hash(state) | 2243 | 2.22M | } |
Unexecuted instantiation: <smallvec::SmallVec<_> as core::hash::Hash>::hash::<_> |
2244 | | } |
2245 | | |
2246 | | unsafe impl<A: Array> Send for SmallVec<A> where A::Item: Send {} |
2247 | | |
2248 | | /// An iterator that consumes a `SmallVec` and yields its items by value. |
2249 | | /// |
2250 | | /// Returned from [`SmallVec::into_iter`][1]. |
2251 | | /// |
2252 | | /// [1]: struct.SmallVec.html#method.into_iter |
2253 | | pub struct IntoIter<A: Array> { |
2254 | | data: SmallVec<A>, |
2255 | | current: usize, |
2256 | | end: usize, |
2257 | | } |
2258 | | |
2259 | | impl<A: Array> fmt::Debug for IntoIter<A> |
2260 | | where |
2261 | | A::Item: fmt::Debug, |
2262 | | { |
2263 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
2264 | 0 | f.debug_tuple("IntoIter").field(&self.as_slice()).finish() |
2265 | 0 | } |
2266 | | } |
2267 | | |
2268 | | impl<A: Array + Clone> Clone for IntoIter<A> |
2269 | | where |
2270 | | A::Item: Clone, |
2271 | | { |
2272 | 0 | fn clone(&self) -> IntoIter<A> { |
2273 | 0 | SmallVec::from(self.as_slice()).into_iter() |
2274 | 0 | } |
2275 | | } |
2276 | | |
2277 | | impl<A: Array> Drop for IntoIter<A> { |
2278 | 83 | fn drop(&mut self) { |
2279 | 83 | for _ in self {} |
2280 | 83 | } Unexecuted instantiation: <smallvec::IntoIter<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::ops::drop::Drop>::drop Unexecuted instantiation: <smallvec::IntoIter<[exr::compression::piz::ChannelData; 6]> as core::ops::drop::Drop>::drop Unexecuted instantiation: <smallvec::IntoIter<_> as core::ops::drop::Drop>::drop Unexecuted instantiation: <smallvec::IntoIter<[alloc::vec::Vec<u64>; 3]> as core::ops::drop::Drop>::drop <smallvec::IntoIter<[alloc::vec::Vec<u64>; 3]> as core::ops::drop::Drop>::drop Line | Count | Source | 2278 | 83 | fn drop(&mut self) { | 2279 | 83 | for _ in self {} | 2280 | 83 | } |
|
2281 | | } |
2282 | | |
2283 | | impl<A: Array> Iterator for IntoIter<A> { |
2284 | | type Item = A::Item; |
2285 | | |
2286 | | #[inline] |
2287 | 249 | fn next(&mut self) -> Option<A::Item> { |
2288 | 249 | if self.current == self.end { |
2289 | 166 | None |
2290 | | } else { |
2291 | | unsafe { |
2292 | 83 | let current = self.current; |
2293 | 83 | self.current += 1; |
2294 | 83 | Some(ptr::read(self.data.as_ptr().add(current))) |
2295 | | } |
2296 | | } |
2297 | 249 | } Unexecuted instantiation: <smallvec::IntoIter<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <smallvec::IntoIter<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <smallvec::IntoIter<_> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <smallvec::IntoIter<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::iterator::Iterator>::next <smallvec::IntoIter<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 2287 | 249 | fn next(&mut self) -> Option<A::Item> { | 2288 | 249 | if self.current == self.end { | 2289 | 166 | None | 2290 | | } else { | 2291 | | unsafe { | 2292 | 83 | let current = self.current; | 2293 | 83 | self.current += 1; | 2294 | 83 | Some(ptr::read(self.data.as_ptr().add(current))) | 2295 | | } | 2296 | | } | 2297 | 249 | } |
|
2298 | | |
2299 | | #[inline] |
2300 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
2301 | 0 | let size = self.end - self.current; |
2302 | 0 | (size, Some(size)) |
2303 | 0 | } Unexecuted instantiation: <smallvec::IntoIter<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <smallvec::IntoIter<_> as core::iter::traits::iterator::Iterator>::size_hint |
2304 | | } |
2305 | | |
2306 | | impl<A: Array> DoubleEndedIterator for IntoIter<A> { |
2307 | | #[inline] |
2308 | 0 | fn next_back(&mut self) -> Option<A::Item> { |
2309 | 0 | if self.current == self.end { |
2310 | 0 | None |
2311 | | } else { |
2312 | | unsafe { |
2313 | 0 | self.end -= 1; |
2314 | 0 | Some(ptr::read(self.data.as_ptr().add(self.end))) |
2315 | | } |
2316 | | } |
2317 | 0 | } |
2318 | | } |
2319 | | |
2320 | | impl<A: Array> ExactSizeIterator for IntoIter<A> {} |
2321 | | impl<A: Array> FusedIterator for IntoIter<A> {} |
2322 | | |
2323 | | impl<A: Array> IntoIter<A> { |
2324 | | /// Returns the remaining items of this iterator as a slice. |
2325 | 0 | pub fn as_slice(&self) -> &[A::Item] { |
2326 | 0 | let len = self.end - self.current; |
2327 | 0 | unsafe { core::slice::from_raw_parts(self.data.as_ptr().add(self.current), len) } |
2328 | 0 | } |
2329 | | |
2330 | | /// Returns the remaining items of this iterator as a mutable slice. |
2331 | 0 | pub fn as_mut_slice(&mut self) -> &mut [A::Item] { |
2332 | 0 | let len = self.end - self.current; |
2333 | 0 | unsafe { core::slice::from_raw_parts_mut(self.data.as_mut_ptr().add(self.current), len) } |
2334 | 0 | } |
2335 | | } |
2336 | | |
2337 | | impl<A: Array> IntoIterator for SmallVec<A> { |
2338 | | type IntoIter = IntoIter<A>; |
2339 | | type Item = A::Item; |
2340 | 83 | fn into_iter(mut self) -> Self::IntoIter { |
2341 | | unsafe { |
2342 | | // Set SmallVec len to zero as `IntoIter` drop handles dropping of the elements |
2343 | 83 | let len = self.len(); |
2344 | 83 | self.set_len(0); |
2345 | 83 | IntoIter { |
2346 | 83 | data: self, |
2347 | 83 | current: 0, |
2348 | 83 | end: len, |
2349 | 83 | } |
2350 | | } |
2351 | 83 | } Unexecuted instantiation: <smallvec::SmallVec<[exr::image::AnyChannel<exr::image::FlatSamples>; 4]> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <smallvec::SmallVec<_> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::IntoIterator>::into_iter <smallvec::SmallVec<[alloc::vec::Vec<u64>; 3]> as core::iter::traits::collect::IntoIterator>::into_iter Line | Count | Source | 2340 | 83 | fn into_iter(mut self) -> Self::IntoIter { | 2341 | | unsafe { | 2342 | | // Set SmallVec len to zero as `IntoIter` drop handles dropping of the elements | 2343 | 83 | let len = self.len(); | 2344 | 83 | self.set_len(0); | 2345 | 83 | IntoIter { | 2346 | 83 | data: self, | 2347 | 83 | current: 0, | 2348 | 83 | end: len, | 2349 | 83 | } | 2350 | | } | 2351 | 83 | } |
|
2352 | | } |
2353 | | |
2354 | | impl<'a, A: Array> IntoIterator for &'a SmallVec<A> { |
2355 | | type IntoIter = slice::Iter<'a, A::Item>; |
2356 | | type Item = &'a A::Item; |
2357 | 8.09k | fn into_iter(self) -> Self::IntoIter { |
2358 | 8.09k | self.iter() |
2359 | 8.09k | } Unexecuted instantiation: <&smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::IntoIterator>::into_iter <&smallvec::SmallVec<[exr::meta::attribute::ChannelDescription; 5]> as core::iter::traits::collect::IntoIterator>::into_iter Line | Count | Source | 2357 | 263 | fn into_iter(self) -> Self::IntoIter { | 2358 | 263 | self.iter() | 2359 | 263 | } |
<&smallvec::SmallVec<[u8; 24]> as core::iter::traits::collect::IntoIterator>::into_iter Line | Count | Source | 2357 | 7.82k | fn into_iter(self) -> Self::IntoIter { | 2358 | 7.82k | self.iter() | 2359 | 7.82k | } |
Unexecuted instantiation: <&smallvec::SmallVec<_> as core::iter::traits::collect::IntoIterator>::into_iter |
2360 | | } |
2361 | | |
2362 | | impl<'a, A: Array> IntoIterator for &'a mut SmallVec<A> { |
2363 | | type IntoIter = slice::IterMut<'a, A::Item>; |
2364 | | type Item = &'a mut A::Item; |
2365 | 0 | fn into_iter(self) -> Self::IntoIter { |
2366 | 0 | self.iter_mut() |
2367 | 0 | } Unexecuted instantiation: <&mut smallvec::SmallVec<[exr::compression::piz::ChannelData; 6]> as core::iter::traits::collect::IntoIterator>::into_iter Unexecuted instantiation: <&mut smallvec::SmallVec<_> as core::iter::traits::collect::IntoIterator>::into_iter |
2368 | | } |
2369 | | |
2370 | | /// Types that can be used as the backing store for a [`SmallVec`]. |
2371 | | pub unsafe trait Array { |
2372 | | /// The type of the array's elements. |
2373 | | type Item; |
2374 | | /// Returns the number of items the array can hold. |
2375 | | fn size() -> usize; |
2376 | | } |
2377 | | |
2378 | | /// Set the length of the vec when the `SetLenOnDrop` value goes out of scope. |
2379 | | /// |
2380 | | /// Copied from <https://github.com/rust-lang/rust/pull/36355> |
2381 | | struct SetLenOnDrop<'a> { |
2382 | | len: &'a mut usize, |
2383 | | local_len: usize, |
2384 | | } |
2385 | | |
2386 | | impl<'a> SetLenOnDrop<'a> { |
2387 | | #[inline] |
2388 | 8.91M | fn new(len: &'a mut usize) -> Self { |
2389 | 8.91M | SetLenOnDrop { |
2390 | 8.91M | local_len: *len, |
2391 | 8.91M | len, |
2392 | 8.91M | } |
2393 | 8.91M | } <smallvec::SetLenOnDrop>::new Line | Count | Source | 2388 | 4.36M | fn new(len: &'a mut usize) -> Self { | 2389 | 4.36M | SetLenOnDrop { | 2390 | 4.36M | local_len: *len, | 2391 | 4.36M | len, | 2392 | 4.36M | } | 2393 | 4.36M | } |
<smallvec::SetLenOnDrop>::new Line | Count | Source | 2388 | 4.54M | fn new(len: &'a mut usize) -> Self { | 2389 | 4.54M | SetLenOnDrop { | 2390 | 4.54M | local_len: *len, | 2391 | 4.54M | len, | 2392 | 4.54M | } | 2393 | 4.54M | } |
Unexecuted instantiation: <smallvec::SetLenOnDrop>::new Unexecuted instantiation: <smallvec::SetLenOnDrop>::new <smallvec::SetLenOnDrop>::new Line | Count | Source | 2388 | 166 | fn new(len: &'a mut usize) -> Self { | 2389 | 166 | SetLenOnDrop { | 2390 | 166 | local_len: *len, | 2391 | 166 | len, | 2392 | 166 | } | 2393 | 166 | } |
|
2394 | | |
2395 | | #[inline] |
2396 | 149M | fn get(&self) -> usize { |
2397 | 149M | self.local_len |
2398 | 149M | } <smallvec::SetLenOnDrop>::get Line | Count | Source | 2396 | 111M | fn get(&self) -> usize { | 2397 | 111M | self.local_len | 2398 | 111M | } |
<smallvec::SetLenOnDrop>::get Line | Count | Source | 2396 | 38.5M | fn get(&self) -> usize { | 2397 | 38.5M | self.local_len | 2398 | 38.5M | } |
Unexecuted instantiation: <smallvec::SetLenOnDrop>::get Unexecuted instantiation: <smallvec::SetLenOnDrop>::get <smallvec::SetLenOnDrop>::get Line | Count | Source | 2396 | 498 | fn get(&self) -> usize { | 2397 | 498 | self.local_len | 2398 | 498 | } |
|
2399 | | |
2400 | | #[inline] |
2401 | 70.4M | fn increment_len(&mut self, increment: usize) { |
2402 | 70.4M | self.local_len += increment; |
2403 | 70.4M | } <smallvec::SetLenOnDrop>::increment_len Line | Count | Source | 2401 | 53.4M | fn increment_len(&mut self, increment: usize) { | 2402 | 53.4M | self.local_len += increment; | 2403 | 53.4M | } |
<smallvec::SetLenOnDrop>::increment_len Line | Count | Source | 2401 | 16.9M | fn increment_len(&mut self, increment: usize) { | 2402 | 16.9M | self.local_len += increment; | 2403 | 16.9M | } |
Unexecuted instantiation: <smallvec::SetLenOnDrop>::increment_len Unexecuted instantiation: <smallvec::SetLenOnDrop>::increment_len <smallvec::SetLenOnDrop>::increment_len Line | Count | Source | 2401 | 166 | fn increment_len(&mut self, increment: usize) { | 2402 | 166 | self.local_len += increment; | 2403 | 166 | } |
|
2404 | | } |
2405 | | |
2406 | | impl<'a> Drop for SetLenOnDrop<'a> { |
2407 | | #[inline] |
2408 | 8.91M | fn drop(&mut self) { |
2409 | 8.91M | *self.len = self.local_len; |
2410 | 8.91M | } <smallvec::SetLenOnDrop as core::ops::drop::Drop>::drop Line | Count | Source | 2408 | 8.91M | fn drop(&mut self) { | 2409 | 8.91M | *self.len = self.local_len; | 2410 | 8.91M | } |
Unexecuted instantiation: <smallvec::SetLenOnDrop as core::ops::drop::Drop>::drop |
2411 | | } |
2412 | | |
2413 | | #[cfg(feature = "const_new")] |
2414 | | impl<T, const N: usize> SmallVec<[T; N]> { |
2415 | | /// Construct an empty vector. |
2416 | | /// |
2417 | | /// This is a `const` version of [`SmallVec::new`] that is enabled by the feature `const_new`, with the limitation that it only works for arrays. |
2418 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
2419 | | #[inline] |
2420 | | pub const fn new_const() -> Self { |
2421 | | SmallVec { |
2422 | | capacity: 0, |
2423 | | data: SmallVecData::from_const(MaybeUninit::uninit()), |
2424 | | } |
2425 | | } |
2426 | | |
2427 | | /// The array passed as an argument is moved to be an inline version of `SmallVec`. |
2428 | | /// |
2429 | | /// This is a `const` version of [`SmallVec::from_buf`] that is enabled by the feature `const_new`, with the limitation that it only works for arrays. |
2430 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
2431 | | #[inline] |
2432 | | pub const fn from_const(items: [T; N]) -> Self { |
2433 | | SmallVec { |
2434 | | capacity: N, |
2435 | | data: SmallVecData::from_const(MaybeUninit::new(items)), |
2436 | | } |
2437 | | } |
2438 | | |
2439 | | /// Constructs a new `SmallVec` on the stack from an array without |
2440 | | /// copying elements. Also sets the length. The user is responsible |
2441 | | /// for ensuring that `len <= N`. |
2442 | | /// |
2443 | | /// This is a `const` version of [`SmallVec::from_buf_and_len_unchecked`] that is enabled by the feature `const_new`, with the limitation that it only works for arrays. |
2444 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_new")))] |
2445 | | #[inline] |
2446 | | pub const unsafe fn from_const_with_len_unchecked(items: [T; N], len: usize) -> Self { |
2447 | | SmallVec { |
2448 | | capacity: len, |
2449 | | data: SmallVecData::from_const(MaybeUninit::new(items)), |
2450 | | } |
2451 | | } |
2452 | | } |
2453 | | |
2454 | | #[cfg(feature = "const_generics")] |
2455 | | #[cfg_attr(docsrs, doc(cfg(feature = "const_generics")))] |
2456 | | unsafe impl<T, const N: usize> Array for [T; N] { |
2457 | | type Item = T; |
2458 | | #[inline] |
2459 | | fn size() -> usize { |
2460 | | N |
2461 | | } |
2462 | | } |
2463 | | |
2464 | | #[cfg(not(feature = "const_generics"))] |
2465 | | macro_rules! impl_array( |
2466 | | ($($size:expr),+) => { |
2467 | | $( |
2468 | | unsafe impl<T> Array for [T; $size] { |
2469 | | type Item = T; |
2470 | | #[inline] |
2471 | 306M | fn size() -> usize { $size }<[u8; 64] as smallvec::Array>::size Line | Count | Source | 2471 | 38.9M | fn size() -> usize { $size } |
<[alloc::vec::Vec<u64>; 3] as smallvec::Array>::size Line | Count | Source | 2471 | 4.80M | fn size() -> usize { $size } |
<[u8; 8] as smallvec::Array>::size Line | Count | Source | 2471 | 201k | fn size() -> usize { $size } |
Unexecuted instantiation: <[usize; 8] as smallvec::Array>::size <[u8; 16] as smallvec::Array>::size Line | Count | Source | 2471 | 9.07M | fn size() -> usize { $size } |
<[u8; 24] as smallvec::Array>::size Line | Count | Source | 2471 | 227M | fn size() -> usize { $size } |
<[u32; 2] as smallvec::Array>::size Line | Count | Source | 2471 | 17.1k | fn size() -> usize { $size } |
Unexecuted instantiation: <[exr::image::AnyChannel<exr::image::FlatSamples>; 4] as smallvec::Array>::size <[exr::meta::attribute::ChannelDescription; 5] as smallvec::Array>::size Line | Count | Source | 2471 | 9.15M | fn size() -> usize { $size } |
Unexecuted instantiation: <[exr::block::samples::Sample; 8] as smallvec::Array>::size Unexecuted instantiation: <[exr::compression::piz::ChannelData; 6] as smallvec::Array>::size <[exr::meta::header::Header; 3] as smallvec::Array>::size Line | Count | Source | 2471 | 16.9M | fn size() -> usize { $size } |
Unexecuted instantiation: <[_; 5] as smallvec::Array>::size Unexecuted instantiation: <[_; 6] as smallvec::Array>::size Unexecuted instantiation: <[_; 7] as smallvec::Array>::size Unexecuted instantiation: <[_; 8] as smallvec::Array>::size Unexecuted instantiation: <[_; 9] as smallvec::Array>::size Unexecuted instantiation: <[_; 10] as smallvec::Array>::size Unexecuted instantiation: <[_; 11] as smallvec::Array>::size Unexecuted instantiation: <[_; 12] as smallvec::Array>::size Unexecuted instantiation: <[_; 13] as smallvec::Array>::size Unexecuted instantiation: <[_; 14] as smallvec::Array>::size Unexecuted instantiation: <[_; 2048] as smallvec::Array>::size Unexecuted instantiation: <[_; 4096] as smallvec::Array>::size Unexecuted instantiation: <[_; 8192] as smallvec::Array>::size Unexecuted instantiation: <[_; 16384] as smallvec::Array>::size Unexecuted instantiation: <[_; 24576] as smallvec::Array>::size Unexecuted instantiation: <[_; 32768] as smallvec::Array>::size Unexecuted instantiation: <[_; 65536] as smallvec::Array>::size Unexecuted instantiation: <[_; 131072] as smallvec::Array>::size Unexecuted instantiation: <[_; 262144] as smallvec::Array>::size Unexecuted instantiation: <[_; 393216] as smallvec::Array>::size Unexecuted instantiation: <[_; 524288] as smallvec::Array>::size Unexecuted instantiation: <[_; 1048576] as smallvec::Array>::size Unexecuted instantiation: <[_; 15] as smallvec::Array>::size Unexecuted instantiation: <[_; 16] as smallvec::Array>::size Unexecuted instantiation: <[_; 17] as smallvec::Array>::size Unexecuted instantiation: <[_; 18] as smallvec::Array>::size Unexecuted instantiation: <[_; 19] as smallvec::Array>::size Unexecuted instantiation: <[_; 20] as smallvec::Array>::size Unexecuted instantiation: <[_; 21] as smallvec::Array>::size Unexecuted instantiation: <[_; 22] as smallvec::Array>::size Unexecuted instantiation: <[_; 23] as smallvec::Array>::size Unexecuted instantiation: <[_; 24] as smallvec::Array>::size Unexecuted instantiation: <[_; 25] as smallvec::Array>::size Unexecuted instantiation: <[_; 26] as smallvec::Array>::size Unexecuted instantiation: <[_; 27] as smallvec::Array>::size Unexecuted instantiation: <[_; 28] as smallvec::Array>::size Unexecuted instantiation: <[_; 29] as smallvec::Array>::size Unexecuted instantiation: <[_; 30] as smallvec::Array>::size Unexecuted instantiation: <[_; 31] as smallvec::Array>::size Unexecuted instantiation: <[_; 32] as smallvec::Array>::size Unexecuted instantiation: <[_; 36] as smallvec::Array>::size Unexecuted instantiation: <[_; 64] as smallvec::Array>::size Unexecuted instantiation: <[_; 96] as smallvec::Array>::size Unexecuted instantiation: <[_; 128] as smallvec::Array>::size Unexecuted instantiation: <[_; 256] as smallvec::Array>::size Unexecuted instantiation: <[_; 512] as smallvec::Array>::size Unexecuted instantiation: <[_; 1024] as smallvec::Array>::size Unexecuted instantiation: <[_; 1536] as smallvec::Array>::size Unexecuted instantiation: <[_; 0] as smallvec::Array>::size Unexecuted instantiation: <[_; 1] as smallvec::Array>::size Unexecuted instantiation: <[_; 2] as smallvec::Array>::size Unexecuted instantiation: <[_; 3] as smallvec::Array>::size Unexecuted instantiation: <[_; 4] as smallvec::Array>::size |
2472 | | } |
2473 | | )+ |
2474 | | } |
2475 | | ); |
2476 | | |
2477 | | #[cfg(not(feature = "const_generics"))] |
2478 | | impl_array!( |
2479 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, |
2480 | | 26, 27, 28, 29, 30, 31, 32, 36, 0x40, 0x60, 0x80, 0x100, 0x200, 0x400, 0x600, 0x800, 0x1000, |
2481 | | 0x2000, 0x4000, 0x6000, 0x8000, 0x10000, 0x20000, 0x40000, 0x60000, 0x80000, 0x10_0000 |
2482 | | ); |
2483 | | |
2484 | | /// Convenience trait for constructing a `SmallVec` |
2485 | | pub trait ToSmallVec<A: Array> { |
2486 | | /// Construct a new `SmallVec` from a slice. |
2487 | | fn to_smallvec(&self) -> SmallVec<A>; |
2488 | | } |
2489 | | |
2490 | | impl<A: Array> ToSmallVec<A> for [A::Item] |
2491 | | where |
2492 | | A::Item: Copy, |
2493 | | { |
2494 | | #[inline] |
2495 | 0 | fn to_smallvec(&self) -> SmallVec<A> { |
2496 | 0 | SmallVec::from_slice(self) |
2497 | 0 | } |
2498 | | } |
2499 | | |
2500 | | // Immutable counterpart for `NonNull<T>`. |
2501 | | #[repr(transparent)] |
2502 | | struct ConstNonNull<T>(NonNull<T>); |
2503 | | |
2504 | | impl<T> ConstNonNull<T> { |
2505 | | #[inline] |
2506 | 60.9M | fn new(ptr: *const T) -> Option<Self> { |
2507 | 60.9M | NonNull::new(ptr as *mut T).map(Self) |
2508 | 60.9M | } <smallvec::ConstNonNull<alloc::vec::Vec<u64>>>::new Line | Count | Source | 2506 | 1.60M | fn new(ptr: *const T) -> Option<Self> { | 2507 | 1.60M | NonNull::new(ptr as *mut T).map(Self) | 2508 | 1.60M | } |
Unexecuted instantiation: <smallvec::ConstNonNull<exr::image::AnyChannel<exr::image::FlatSamples>>>::new Unexecuted instantiation: <smallvec::ConstNonNull<exr::compression::piz::ChannelData>>::new <smallvec::ConstNonNull<exr::meta::header::Header>>::new Line | Count | Source | 2506 | 5.36M | fn new(ptr: *const T) -> Option<Self> { | 2507 | 5.36M | NonNull::new(ptr as *mut T).map(Self) | 2508 | 5.36M | } |
<smallvec::ConstNonNull<exr::meta::attribute::ChannelDescription>>::new Line | Count | Source | 2506 | 1.10M | fn new(ptr: *const T) -> Option<Self> { | 2507 | 1.10M | NonNull::new(ptr as *mut T).map(Self) | 2508 | 1.10M | } |
Unexecuted instantiation: <smallvec::ConstNonNull<exr::block::samples::Sample>>::new <smallvec::ConstNonNull<u8>>::new Line | Count | Source | 2506 | 52.9M | fn new(ptr: *const T) -> Option<Self> { | 2507 | 52.9M | NonNull::new(ptr as *mut T).map(Self) | 2508 | 52.9M | } |
Unexecuted instantiation: <smallvec::ConstNonNull<usize>>::new <smallvec::ConstNonNull<u32>>::new Line | Count | Source | 2506 | 990 | fn new(ptr: *const T) -> Option<Self> { | 2507 | 990 | NonNull::new(ptr as *mut T).map(Self) | 2508 | 990 | } |
Unexecuted instantiation: <smallvec::ConstNonNull<_>>::new |
2509 | | #[inline] |
2510 | 22.8M | fn as_ptr(self) -> *const T { |
2511 | 22.8M | self.0.as_ptr() |
2512 | 22.8M | } <smallvec::ConstNonNull<alloc::vec::Vec<u64>>>::as_ptr Line | Count | Source | 2510 | 1.63M | fn as_ptr(self) -> *const T { | 2511 | 1.63M | self.0.as_ptr() | 2512 | 1.63M | } |
Unexecuted instantiation: <smallvec::ConstNonNull<exr::image::AnyChannel<exr::image::FlatSamples>>>::as_ptr Unexecuted instantiation: <smallvec::ConstNonNull<exr::compression::piz::ChannelData>>::as_ptr <smallvec::ConstNonNull<exr::meta::header::Header>>::as_ptr Line | Count | Source | 2510 | 4.60M | fn as_ptr(self) -> *const T { | 2511 | 4.60M | self.0.as_ptr() | 2512 | 4.60M | } |
<smallvec::ConstNonNull<exr::meta::attribute::ChannelDescription>>::as_ptr Line | Count | Source | 2510 | 1.10M | fn as_ptr(self) -> *const T { | 2511 | 1.10M | self.0.as_ptr() | 2512 | 1.10M | } |
<smallvec::ConstNonNull<u8>>::as_ptr Line | Count | Source | 2510 | 15.4M | fn as_ptr(self) -> *const T { | 2511 | 15.4M | self.0.as_ptr() | 2512 | 15.4M | } |
Unexecuted instantiation: <smallvec::ConstNonNull<usize>>::as_ptr <smallvec::ConstNonNull<u32>>::as_ptr Line | Count | Source | 2510 | 11.3k | fn as_ptr(self) -> *const T { | 2511 | 11.3k | self.0.as_ptr() | 2512 | 11.3k | } |
Unexecuted instantiation: <smallvec::ConstNonNull<_>>::as_ptr |
2513 | | } |
2514 | | |
2515 | | impl<T> Clone for ConstNonNull<T> { |
2516 | | #[inline] |
2517 | 0 | fn clone(&self) -> Self { |
2518 | 0 | *self |
2519 | 0 | } |
2520 | | } |
2521 | | |
2522 | | impl<T> Copy for ConstNonNull<T> {} |
2523 | | |
2524 | | #[cfg(feature = "impl_bincode")] |
2525 | | use bincode::{ |
2526 | | de::{BorrowDecoder, Decode, Decoder, read::Reader}, |
2527 | | enc::{Encode, Encoder, write::Writer}, |
2528 | | error::{DecodeError, EncodeError}, |
2529 | | BorrowDecode, |
2530 | | }; |
2531 | | |
2532 | | #[cfg(feature = "impl_bincode")] |
2533 | | impl<A, Context> Decode<Context> for SmallVec<A> |
2534 | | where |
2535 | | A: Array, |
2536 | | A::Item: Decode<Context>, |
2537 | | { |
2538 | | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
2539 | | use core::convert::TryInto; |
2540 | | let len = u64::decode(decoder)?; |
2541 | | let len = len.try_into().map_err(|_| DecodeError::OutsideUsizeRange(len))?; |
2542 | | decoder.claim_container_read::<A::Item>(len)?; |
2543 | | |
2544 | | let mut vec = SmallVec::with_capacity(len); |
2545 | | if unty::type_equal::<A::Item, u8>() { |
2546 | | // Initialize the smallvec's buffer. Note that we need to do this through |
2547 | | // the raw pointer as we cannot name the type [u8; N] even though A::Item is u8. |
2548 | | let ptr = vec.as_mut_ptr(); |
2549 | | // SAFETY: A::Item is u8 and the smallvec has been allocated with enough capacity |
2550 | | unsafe { |
2551 | | core::ptr::write_bytes(ptr, 0, len); |
2552 | | vec.set_len(len); |
2553 | | } |
2554 | | // Read the data into the smallvec's buffer. |
2555 | | let slice = vec.as_mut_slice(); |
2556 | | // SAFETY: A::Item is u8 |
2557 | | let slice = unsafe { core::mem::transmute::<&mut [A::Item], &mut [u8]>(slice) }; |
2558 | | decoder.reader().read(slice)?; |
2559 | | } else { |
2560 | | for _ in 0..len { |
2561 | | decoder.unclaim_bytes_read(core::mem::size_of::<A::Item>()); |
2562 | | vec.push(A::Item::decode(decoder)?); |
2563 | | } |
2564 | | } |
2565 | | Ok(vec) |
2566 | | } |
2567 | | } |
2568 | | |
2569 | | #[cfg(feature = "impl_bincode")] |
2570 | | impl<'de, A, Context> BorrowDecode<'de, Context> for SmallVec<A> |
2571 | | where |
2572 | | A: Array, |
2573 | | A::Item: BorrowDecode<'de, Context>, |
2574 | | { |
2575 | | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
2576 | | use core::convert::TryInto; |
2577 | | let len = u64::decode(decoder)?; |
2578 | | let len = len.try_into().map_err(|_| DecodeError::OutsideUsizeRange(len))?; |
2579 | | decoder.claim_container_read::<A::Item>(len)?; |
2580 | | |
2581 | | let mut vec = SmallVec::with_capacity(len); |
2582 | | if unty::type_equal::<A::Item, u8>() { |
2583 | | // Initialize the smallvec's buffer. Note that we need to do this through |
2584 | | // the raw pointer as we cannot name the type [u8; N] even though A::Item is u8. |
2585 | | let ptr = vec.as_mut_ptr(); |
2586 | | // SAFETY: A::Item is u8 and the smallvec has been allocated with enough capacity |
2587 | | unsafe { |
2588 | | core::ptr::write_bytes(ptr, 0, len); |
2589 | | vec.set_len(len); |
2590 | | } |
2591 | | // Read the data into the smallvec's buffer. |
2592 | | let slice = vec.as_mut_slice(); |
2593 | | // SAFETY: A::Item is u8 |
2594 | | let slice = unsafe { core::mem::transmute::<&mut [A::Item], &mut [u8]>(slice) }; |
2595 | | decoder.reader().read(slice)?; |
2596 | | } else { |
2597 | | for _ in 0..len { |
2598 | | decoder.unclaim_bytes_read(core::mem::size_of::<A::Item>()); |
2599 | | vec.push(A::Item::borrow_decode(decoder)?); |
2600 | | } |
2601 | | } |
2602 | | Ok(vec) |
2603 | | } |
2604 | | } |
2605 | | |
2606 | | #[cfg(feature = "impl_bincode")] |
2607 | | impl<A> Encode for SmallVec<A> |
2608 | | where |
2609 | | A: Array, |
2610 | | A::Item: Encode, |
2611 | | { |
2612 | | fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
2613 | | (self.len() as u64).encode(encoder)?; |
2614 | | if unty::type_equal::<A::Item, u8>() { |
2615 | | // Safety: A::Item is u8 |
2616 | | let slice: &[u8] = unsafe { core::mem::transmute(self.as_slice()) }; |
2617 | | encoder.writer().write(slice)?; |
2618 | | } else { |
2619 | | for item in self.iter() { |
2620 | | item.encode(encoder)?; |
2621 | | } |
2622 | | } |
2623 | | Ok(()) |
2624 | | } |
2625 | | } |