Coverage Report

Created: 2024-07-06 06:44

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