bumpalo/collections/
vec.rs

1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10//! A contiguous growable array type with heap-allocated contents, written
11//! [`Vec<'bump, T>`].
12//!
13//! Vectors have `O(1)` indexing, amortized `O(1)` push (to the end) and
14//! `O(1)` pop (from the end).
15//!
16//! This module is a fork of the [`std::vec`] module, that uses a bump allocator.
17//!
18//! [`std::vec`]: https://doc.rust-lang.org/std/vec/index.html
19//!
20//! # Examples
21//!
22//! You can explicitly create a [`Vec<'bump, T>`] with [`new_in`]:
23//!
24//! ```
25//! use bumpalo::{Bump, collections::Vec};
26//!
27//! let b = Bump::new();
28//! let v: Vec<i32> = Vec::new_in(&b);
29//! ```
30//!
31//! ... or by using the [`vec!`] macro:
32//!
33//! ```
34//! use bumpalo::{Bump, collections::Vec};
35//!
36//! let b = Bump::new();
37//!
38//! let v: Vec<i32> = bumpalo::vec![in &b];
39//!
40//! let v = bumpalo::vec![in &b; 1, 2, 3, 4, 5];
41//!
42//! let v = bumpalo::vec![in &b; 0; 10]; // ten zeroes
43//! ```
44//!
45//! You can [`push`] values onto the end of a vector (which will grow the vector
46//! as needed):
47//!
48//! ```
49//! use bumpalo::{Bump, collections::Vec};
50//!
51//! let b = Bump::new();
52//!
53//! let mut v = bumpalo::vec![in &b; 1, 2];
54//!
55//! v.push(3);
56//! ```
57//!
58//! Popping values works in much the same way:
59//!
60//! ```
61//! use bumpalo::{Bump, collections::Vec};
62//!
63//! let b = Bump::new();
64//!
65//! let mut v = bumpalo::vec![in &b; 1, 2];
66//!
67//! assert_eq!(v.pop(), Some(2));
68//! ```
69//!
70//! Vectors also support indexing (through the [`Index`] and [`IndexMut`] traits):
71//!
72//! ```
73//! use bumpalo::{Bump, collections::Vec};
74//!
75//! let b = Bump::new();
76//!
77//! let mut v = bumpalo::vec![in &b; 1, 2, 3];
78//! assert_eq!(v[2], 3);
79//! v[1] += 5;
80//! assert_eq!(v, [1, 7, 3]);
81//! ```
82//!
83//! [`Vec<'bump, T>`]: struct.Vec.html
84//! [`new_in`]: struct.Vec.html#method.new_in
85//! [`push`]: struct.Vec.html#method.push
86//! [`Index`]: https://doc.rust-lang.org/std/ops/trait.Index.html
87//! [`IndexMut`]: https://doc.rust-lang.org/std/ops/trait.IndexMut.html
88//! [`vec!`]: ../../macro.vec.html
89
90use super::raw_vec::RawVec;
91use crate::collections::CollectionAllocErr;
92use crate::Bump;
93use core::borrow::{Borrow, BorrowMut};
94use core::cmp::Ordering;
95use core::fmt;
96use core::hash::{self, Hash};
97use core::iter::FusedIterator;
98use core::marker::PhantomData;
99use core::mem;
100use core::ops;
101use core::ops::Bound::{Excluded, Included, Unbounded};
102use core::ops::{Index, IndexMut, RangeBounds};
103use core::ptr;
104use core::ptr::NonNull;
105use core::slice;
106#[cfg(feature = "std")]
107use std::io;
108
109unsafe fn arith_offset<T>(p: *const T, offset: isize) -> *const T {
110    p.offset(offset)
111}
112
113fn partition_dedup_by<T, F>(s: &mut [T], mut same_bucket: F) -> (&mut [T], &mut [T])
114where
115    F: FnMut(&mut T, &mut T) -> bool,
116{
117    // Although we have a mutable reference to `s`, we cannot make
118    // *arbitrary* changes. The `same_bucket` calls could panic, so we
119    // must ensure that the slice is in a valid state at all times.
120    //
121    // The way that we handle this is by using swaps; we iterate
122    // over all the elements, swapping as we go so that at the end
123    // the elements we wish to keep are in the front, and those we
124    // wish to reject are at the back. We can then split the slice.
125    // This operation is still O(n).
126    //
127    // Example: We start in this state, where `r` represents "next
128    // read" and `w` represents "next_write`.
129    //
130    //           r
131    //     +---+---+---+---+---+---+
132    //     | 0 | 1 | 1 | 2 | 3 | 3 |
133    //     +---+---+---+---+---+---+
134    //           w
135    //
136    // Comparing s[r] against s[w-1], this is not a duplicate, so
137    // we swap s[r] and s[w] (no effect as r==w) and then increment both
138    // r and w, leaving us with:
139    //
140    //               r
141    //     +---+---+---+---+---+---+
142    //     | 0 | 1 | 1 | 2 | 3 | 3 |
143    //     +---+---+---+---+---+---+
144    //               w
145    //
146    // Comparing s[r] against s[w-1], this value is a duplicate,
147    // so we increment `r` but leave everything else unchanged:
148    //
149    //                   r
150    //     +---+---+---+---+---+---+
151    //     | 0 | 1 | 1 | 2 | 3 | 3 |
152    //     +---+---+---+---+---+---+
153    //               w
154    //
155    // Comparing s[r] against s[w-1], this is not a duplicate,
156    // so swap s[r] and s[w] and advance r and w:
157    //
158    //                       r
159    //     +---+---+---+---+---+---+
160    //     | 0 | 1 | 2 | 1 | 3 | 3 |
161    //     +---+---+---+---+---+---+
162    //                   w
163    //
164    // Not a duplicate, repeat:
165    //
166    //                           r
167    //     +---+---+---+---+---+---+
168    //     | 0 | 1 | 2 | 3 | 1 | 3 |
169    //     +---+---+---+---+---+---+
170    //                       w
171    //
172    // Duplicate, advance r. End of slice. Split at w.
173
174    let len = s.len();
175    if len <= 1 {
176        return (s, &mut []);
177    }
178
179    let ptr = s.as_mut_ptr();
180    let mut next_read: usize = 1;
181    let mut next_write: usize = 1;
182
183    unsafe {
184        // Avoid bounds checks by using raw pointers.
185        while next_read < len {
186            let ptr_read = ptr.add(next_read);
187            let prev_ptr_write = ptr.add(next_write - 1);
188            if !same_bucket(&mut *ptr_read, &mut *prev_ptr_write) {
189                if next_read != next_write {
190                    let ptr_write = prev_ptr_write.offset(1);
191                    mem::swap(&mut *ptr_read, &mut *ptr_write);
192                }
193                next_write += 1;
194            }
195            next_read += 1;
196        }
197    }
198
199    s.split_at_mut(next_write)
200}
201
202unsafe fn offset_from<T>(p: *const T, origin: *const T) -> isize
203where
204    T: Sized,
205{
206    let pointee_size = mem::size_of::<T>();
207    assert!(0 < pointee_size && pointee_size <= isize::max_value() as usize);
208
209    // This is the same sequence that Clang emits for pointer subtraction.
210    // It can be neither `nsw` nor `nuw` because the input is treated as
211    // unsigned but then the output is treated as signed, so neither works.
212    let d = isize::wrapping_sub(p as _, origin as _);
213    d / (pointee_size as isize)
214}
215
216/// Creates a [`Vec`] containing the arguments.
217///
218/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
219/// There are two forms of this macro:
220///
221/// - Create a [`Vec`] containing a given list of elements:
222///
223/// ```
224/// use bumpalo::Bump;
225///
226/// let b = Bump::new();
227/// let v = bumpalo::vec![in &b; 1, 2, 3];
228/// assert_eq!(v, [1, 2, 3]);
229/// ```
230///
231/// - Create a [`Vec`] from a given element and size:
232///
233/// ```
234/// use bumpalo::Bump;
235///
236/// let b = Bump::new();
237/// let v = bumpalo::vec![in &b; 1; 3];
238/// assert_eq!(v, [1, 1, 1]);
239/// ```
240///
241/// Note that unlike array expressions, this syntax supports all elements
242/// which implement [`Clone`] and the number of elements doesn't have to be
243/// a constant.
244///
245/// This will use `clone` to duplicate an expression, so one should be careful
246/// using this with types having a non-standard `Clone` implementation. For
247/// example, `bumpalo::vec![in &bump; Rc::new(1); 5]` will create a vector of five references
248/// to the same boxed integer value, not five references pointing to independently
249/// boxed integers.
250///
251/// [`Vec`]: collections/vec/struct.Vec.html
252/// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html
253#[macro_export]
254macro_rules! vec {
255    (in $bump:expr; $elem:expr; $n:expr) => {{
256        let n = $n;
257        let mut v = $crate::collections::Vec::with_capacity_in(n, $bump);
258        if n > 0 {
259            let elem = $elem;
260            for _ in 0..n - 1 {
261                v.push(elem.clone());
262            }
263            v.push(elem);
264        }
265        v
266    }};
267    (in $bump:expr) => { $crate::collections::Vec::new_in($bump) };
268    (in $bump:expr; $($x:expr),*) => {{
269        let mut v = $crate::collections::Vec::new_in($bump);
270        $( v.push($x); )*
271        v
272    }};
273    (in $bump:expr; $($x:expr,)*) => (bumpalo::vec![in $bump; $($x),*])
274}
275
276/// A contiguous growable array type, written `Vec<'bump, T>` but pronounced 'vector'.
277///
278/// # Examples
279///
280/// ```
281/// use bumpalo::{Bump, collections::Vec};
282///
283/// let b = Bump::new();
284///
285/// let mut vec = Vec::new_in(&b);
286/// vec.push(1);
287/// vec.push(2);
288///
289/// assert_eq!(vec.len(), 2);
290/// assert_eq!(vec[0], 1);
291///
292/// assert_eq!(vec.pop(), Some(2));
293/// assert_eq!(vec.len(), 1);
294///
295/// vec[0] = 7;
296/// assert_eq!(vec[0], 7);
297///
298/// vec.extend([1, 2, 3].iter().cloned());
299///
300/// for x in &vec {
301///     println!("{}", x);
302/// }
303/// assert_eq!(vec, [7, 1, 2, 3]);
304/// ```
305///
306/// The [`vec!`] macro is provided to make initialization more convenient:
307///
308/// ```
309/// use bumpalo::{Bump, collections::Vec};
310///
311/// let b = Bump::new();
312///
313/// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
314/// vec.push(4);
315/// assert_eq!(vec, [1, 2, 3, 4]);
316/// ```
317///
318/// It can also initialize each element of a `Vec<'bump, T>` with a given value.
319/// This may be more efficient than performing allocation and initialization
320/// in separate steps, especially when initializing a vector of zeros:
321///
322/// ```
323/// use bumpalo::{Bump, collections::Vec};
324///
325/// let b = Bump::new();
326///
327/// let vec = bumpalo::vec![in &b; 0; 5];
328/// assert_eq!(vec, [0, 0, 0, 0, 0]);
329///
330/// // The following is equivalent, but potentially slower:
331/// let mut vec1 = Vec::with_capacity_in(5, &b);
332/// vec1.resize(5, 0);
333/// ```
334///
335/// Use a `Vec<'bump, T>` as an efficient stack:
336///
337/// ```
338/// use bumpalo::{Bump, collections::Vec};
339///
340/// let b = Bump::new();
341///
342/// let mut stack = Vec::new_in(&b);
343///
344/// stack.push(1);
345/// stack.push(2);
346/// stack.push(3);
347///
348/// while let Some(top) = stack.pop() {
349///     // Prints 3, 2, 1
350///     println!("{}", top);
351/// }
352/// ```
353///
354/// # Indexing
355///
356/// The `Vec` type allows to access values by index, because it implements the
357/// [`Index`] trait. An example will be more explicit:
358///
359/// ```
360/// use bumpalo::{Bump, collections::Vec};
361///
362/// let b = Bump::new();
363///
364/// let v = bumpalo::vec![in &b; 0, 2, 4, 6];
365/// println!("{}", v[1]); // it will display '2'
366/// ```
367///
368/// However be careful: if you try to access an index which isn't in the `Vec`,
369/// your software will panic! You cannot do this:
370///
371/// ```should_panic
372/// use bumpalo::{Bump, collections::Vec};
373///
374/// let b = Bump::new();
375///
376/// let v = bumpalo::vec![in &b; 0, 2, 4, 6];
377/// println!("{}", v[6]); // it will panic!
378/// ```
379///
380/// In conclusion: always check if the index you want to get really exists
381/// before doing it.
382///
383/// # Slicing
384///
385/// A `Vec` can be mutable. Slices, on the other hand, are read-only objects.
386/// To get a slice, use `&`. Example:
387///
388/// ```
389/// use bumpalo::{Bump, collections::Vec};
390///
391/// fn read_slice(slice: &[usize]) {
392///     // ...
393/// }
394///
395/// let b = Bump::new();
396///
397/// let v = bumpalo::vec![in &b; 0, 1];
398/// read_slice(&v);
399///
400/// // ... and that's all!
401/// // you can also do it like this:
402/// let x : &[usize] = &v;
403/// ```
404///
405/// In Rust, it's more common to pass slices as arguments rather than vectors
406/// when you just want to provide a read access. The same goes for [`String`] and
407/// [`&str`].
408///
409/// # Capacity and reallocation
410///
411/// The capacity of a vector is the amount of space allocated for any future
412/// elements that will be added onto the vector. This is not to be confused with
413/// the *length* of a vector, which specifies the number of actual elements
414/// within the vector. If a vector's length exceeds its capacity, its capacity
415/// will automatically be increased, but its elements will have to be
416/// reallocated.
417///
418/// For example, a vector with capacity 10 and length 0 would be an empty vector
419/// with space for 10 more elements. Pushing 10 or fewer elements onto the
420/// vector will not change its capacity or cause reallocation to occur. However,
421/// if the vector's length is increased to 11, it will have to reallocate, which
422/// can be slow. For this reason, it is recommended to use [`Vec::with_capacity_in`]
423/// whenever possible to specify how big the vector is expected to get.
424///
425/// # Guarantees
426///
427/// Due to its incredibly fundamental nature, `Vec` makes a lot of guarantees
428/// about its design. This ensures that it's as low-overhead as possible in
429/// the general case, and can be correctly manipulated in primitive ways
430/// by unsafe code. Note that these guarantees refer to an unqualified `Vec<'bump, T>`.
431/// If additional type parameters are added (e.g. to support custom allocators),
432/// overriding their defaults may change the behavior.
433///
434/// Most fundamentally, `Vec` is and always will be a (pointer, capacity, length)
435/// triplet. No more, no less. The order of these fields is completely
436/// unspecified, and you should use the appropriate methods to modify these.
437/// The pointer will never be null, so this type is null-pointer-optimized.
438///
439/// However, the pointer may not actually point to allocated memory. In particular,
440/// if you construct a `Vec` with capacity 0 via [`Vec::new_in`], [`bumpalo::vec![in bump]`][`vec!`],
441/// [`Vec::with_capacity_in(0)`][`Vec::with_capacity_in`], or by calling [`shrink_to_fit`]
442/// on an empty Vec, it will not allocate memory. Similarly, if you store zero-sized
443/// types inside a `Vec`, it will not allocate space for them. *Note that in this case
444/// the `Vec` may not report a [`capacity`] of 0*. `Vec` will allocate if and only
445/// if <code>[`mem::size_of::<T>`]\() * capacity() > 0</code>. In general, `Vec`'s allocation
446/// details are very subtle &mdash; if you intend to allocate memory using a `Vec`
447/// and use it for something else (either to pass to unsafe code, or to build your
448/// own memory-backed collection), be sure to deallocate this memory by using
449/// `from_raw_parts` to recover the `Vec` and then dropping it.
450///
451/// If a `Vec` *has* allocated memory, then the memory it points to is
452/// in the [`Bump`] arena used to construct it, and its
453/// pointer points to [`len`] initialized, contiguous elements in order (what
454/// you would see if you coerced it to a slice), followed by <code>[`capacity`] -
455/// [`len`]</code> logically uninitialized, contiguous elements.
456///
457/// `Vec` will never perform a "small optimization" where elements are actually
458/// stored on the stack for two reasons:
459///
460/// * It would make it more difficult for unsafe code to correctly manipulate
461///   a `Vec`. The contents of a `Vec` wouldn't have a stable address if it were
462///   only moved, and it would be more difficult to determine if a `Vec` had
463///   actually allocated memory.
464///
465/// * It would penalize the general case, incurring an additional branch
466///   on every access.
467///
468/// `Vec` will never automatically shrink itself, even if completely empty. This
469/// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec`
470/// and then filling it back up to the same [`len`] should incur no calls to
471/// the allocator. If you wish to free up unused memory, use
472/// [`shrink_to_fit`][`shrink_to_fit`].
473///
474/// [`push`] and [`insert`] will never (re)allocate if the reported capacity is
475/// sufficient. [`push`] and [`insert`] *will* (re)allocate if
476/// <code>[`len`] == [`capacity`]</code>. That is, the reported capacity is completely
477/// accurate, and can be relied on. It can even be used to manually free the memory
478/// allocated by a `Vec` if desired. Bulk insertion methods *may* reallocate, even
479/// when not necessary.
480///
481/// `Vec` does not guarantee any particular growth strategy when reallocating
482/// when full, nor when [`reserve`] is called. The current strategy is basic
483/// and it may prove desirable to use a non-constant growth factor. Whatever
484/// strategy is used will of course guarantee `O(1)` amortized [`push`].
485///
486/// `bumpalo::vec![in bump; x; n]`, `bumpalo::vec![in bump; a, b, c, d]`, and
487/// [`Vec::with_capacity_in(n)`][`Vec::with_capacity_in`], will all produce a
488/// `Vec` with exactly the requested capacity. If <code>[`len`] == [`capacity`]</code>, (as
489/// is the case for the [`vec!`] macro), then a `Vec<'bump, T>` can be converted
490/// to and from a [`Box<[T]>`][owned slice] without reallocating or moving the
491/// elements.
492///
493/// `Vec` will not specifically overwrite any data that is removed from it,
494/// but also won't specifically preserve it. Its uninitialized memory is
495/// scratch space that it may use however it wants. It will generally just do
496/// whatever is most efficient or otherwise easy to implement. Do not rely on
497/// removed data to be erased for security purposes. Even if you drop a `Vec`, its
498/// buffer may simply be reused by another `Vec`. Even if you zero a `Vec`'s memory
499/// first, that may not actually happen because the optimizer does not consider
500/// this a side-effect that must be preserved. There is one case which we will
501/// not break, however: using `unsafe` code to write to the excess capacity,
502/// and then increasing the length to match, is always valid.
503///
504/// `Vec` does not currently guarantee the order in which elements are dropped.
505/// The order has changed in the past and may change again.
506///
507/// [`vec!`]: ../../macro.vec.html
508/// [`Index`]: https://doc.rust-lang.org/std/ops/trait.Index.html
509/// [`String`]: ../string/struct.String.html
510/// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
511/// [`Vec::with_capacity_in`]: struct.Vec.html#method.with_capacity_in
512/// [`Vec::new_in`]: struct.Vec.html#method.new_in
513/// [`shrink_to_fit`]: struct.Vec.html#method.shrink_to_fit
514/// [`capacity`]: struct.Vec.html#method.capacity
515/// [`mem::size_of::<T>`]: https://doc.rust-lang.org/std/mem/fn.size_of.html
516/// [`len`]: struct.Vec.html#method.len
517/// [`push`]: struct.Vec.html#method.push
518/// [`insert`]: struct.Vec.html#method.insert
519/// [`reserve`]: struct.Vec.html#method.reserve
520/// [owned slice]: https://doc.rust-lang.org/std/boxed/struct.Box.html
521pub struct Vec<'bump, T: 'bump> {
522    buf: RawVec<'bump, T>,
523    len: usize,
524}
525
526////////////////////////////////////////////////////////////////////////////////
527// Inherent methods
528////////////////////////////////////////////////////////////////////////////////
529
530impl<'bump, T: 'bump> Vec<'bump, T> {
531    /// Constructs a new, empty `Vec<'bump, T>`.
532    ///
533    /// The vector will not allocate until elements are pushed onto it.
534    ///
535    /// # Examples
536    ///
537    /// ```
538    /// # #![allow(unused_mut)]
539    /// use bumpalo::{Bump, collections::Vec};
540    ///
541    /// let b = Bump::new();
542    /// let mut vec: Vec<i32> = Vec::new_in(&b);
543    /// ```
544    #[inline]
545    pub fn new_in(bump: &'bump Bump) -> Vec<'bump, T> {
546        Vec {
547            buf: RawVec::new_in(bump),
548            len: 0,
549        }
550    }
551
552    /// Constructs a new, empty `Vec<'bump, T>` with the specified capacity.
553    ///
554    /// The vector will be able to hold exactly `capacity` elements without
555    /// reallocating. If `capacity` is 0, the vector will not allocate.
556    ///
557    /// It is important to note that although the returned vector has the
558    /// *capacity* specified, the vector will have a zero *length*. For an
559    /// explanation of the difference between length and capacity, see
560    /// *[Capacity and reallocation]*.
561    ///
562    /// [Capacity and reallocation]: #capacity-and-reallocation
563    ///
564    /// # Examples
565    ///
566    /// ```
567    /// use bumpalo::{Bump, collections::Vec};
568    ///
569    /// let b = Bump::new();
570    ///
571    /// let mut vec = Vec::with_capacity_in(10, &b);
572    ///
573    /// // The vector contains no items, even though it has capacity for more
574    /// assert_eq!(vec.len(), 0);
575    ///
576    /// // These are all done without reallocating...
577    /// for i in 0..10 {
578    ///     vec.push(i);
579    /// }
580    ///
581    /// // ...but this may make the vector reallocate
582    /// vec.push(11);
583    /// ```
584    #[inline]
585    pub fn with_capacity_in(capacity: usize, bump: &'bump Bump) -> Vec<'bump, T> {
586        Vec {
587            buf: RawVec::with_capacity_in(capacity, bump),
588            len: 0,
589        }
590    }
591
592    /// Construct a new `Vec` from the given iterator's items.
593    ///
594    /// # Examples
595    ///
596    /// ```
597    /// use bumpalo::{Bump, collections::Vec};
598    /// use std::iter;
599    ///
600    /// let b = Bump::new();
601    /// let v = Vec::from_iter_in(iter::repeat(7).take(3), &b);
602    /// assert_eq!(v, [7, 7, 7]);
603    /// ```
604    pub fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, bump: &'bump Bump) -> Vec<'bump, T> {
605        let mut v = Vec::new_in(bump);
606        v.extend(iter);
607        v
608    }
609
610    /// Creates a `Vec<'bump, T>` directly from the raw components of another vector.
611    ///
612    /// # Safety
613    ///
614    /// This is highly unsafe, due to the number of invariants that aren't
615    /// checked:
616    ///
617    /// * `ptr` needs to have been previously allocated via [`String`]/`Vec<'bump, T>`
618    ///   (at least, it's highly likely to be incorrect if it wasn't).
619    /// * `ptr`'s `T` needs to have the same size and alignment as it was allocated with.
620    /// * `length` needs to be less than or equal to `capacity`.
621    /// * `capacity` needs to be the capacity that the pointer was allocated with.
622    ///
623    /// Violating these may cause problems like corrupting the allocator's
624    /// internal data structures. For example it is **not** safe
625    /// to build a `Vec<u8>` from a pointer to a C `char` array and a `size_t`.
626    ///
627    /// The ownership of `ptr` is effectively transferred to the
628    /// `Vec<'bump, T>` which may then deallocate, reallocate or change the
629    /// contents of memory pointed to by the pointer at will. Ensure
630    /// that nothing else uses the pointer after calling this
631    /// function.
632    ///
633    /// [`String`]: ../string/struct.String.html
634    ///
635    /// # Examples
636    ///
637    /// ```
638    /// use bumpalo::{Bump, collections::Vec};
639    ///
640    /// use std::ptr;
641    /// use std::mem;
642    ///
643    /// let b = Bump::new();
644    ///
645    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
646    ///
647    /// // Pull out the various important pieces of information about `v`
648    /// let p = v.as_mut_ptr();
649    /// let len = v.len();
650    /// let cap = v.capacity();
651    ///
652    /// unsafe {
653    ///     // Cast `v` into the void: no destructor run, so we are in
654    ///     // complete control of the allocation to which `p` points.
655    ///     mem::forget(v);
656    ///
657    ///     // Overwrite memory with 4, 5, 6
658    ///     for i in 0..len as isize {
659    ///         ptr::write(p.offset(i), 4 + i);
660    ///     }
661    ///
662    ///     // Put everything back together into a Vec
663    ///     let rebuilt = Vec::from_raw_parts_in(p, len, cap, &b);
664    ///     assert_eq!(rebuilt, [4, 5, 6]);
665    /// }
666    /// ```
667    pub unsafe fn from_raw_parts_in(
668        ptr: *mut T,
669        length: usize,
670        capacity: usize,
671        bump: &'bump Bump,
672    ) -> Vec<'bump, T> {
673        Vec {
674            buf: RawVec::from_raw_parts_in(ptr, capacity, bump),
675            len: length,
676        }
677    }
678
679    /// Returns a shared reference to the allocator backing this `Vec`.
680    ///
681    /// # Examples
682    ///
683    /// ```
684    /// use bumpalo::{Bump, collections::Vec};
685    ///
686    /// // uses the same allocator as the provided `Vec`
687    /// fn add_strings<'bump>(vec: &mut Vec<'bump, &'bump str>) {
688    ///     for string in ["foo", "bar", "baz"] {
689    ///         vec.push(vec.bump().alloc_str(string));
690    ///     }
691    /// }
692    /// ```
693    #[inline]
694    #[must_use]
695    pub fn bump(&self) -> &'bump Bump {
696        self.buf.bump()
697    }
698
699    /// Returns the number of elements the vector can hold without
700    /// reallocating.
701    ///
702    /// # Examples
703    ///
704    /// ```
705    /// use bumpalo::{Bump, collections::Vec};
706    ///
707    /// let b = Bump::new();
708    /// let vec: Vec<i32> = Vec::with_capacity_in(10, &b);
709    /// assert_eq!(vec.capacity(), 10);
710    /// ```
711    #[inline]
712    pub fn capacity(&self) -> usize {
713        self.buf.cap()
714    }
715
716    /// Reserves capacity for at least `additional` more elements to be inserted
717    /// in the given `Vec<'bump, T>`. The collection may reserve more space to avoid
718    /// frequent reallocations. After calling `reserve`, capacity will be
719    /// greater than or equal to `self.len() + additional`. Does nothing if
720    /// capacity is already sufficient.
721    ///
722    /// # Panics
723    ///
724    /// Panics if the new capacity overflows `usize`.
725    ///
726    /// # Examples
727    ///
728    /// ```
729    /// use bumpalo::{Bump, collections::Vec};
730    ///
731    /// let b = Bump::new();
732    /// let mut vec = bumpalo::vec![in &b; 1];
733    /// vec.reserve(10);
734    /// assert!(vec.capacity() >= 11);
735    /// ```
736    pub fn reserve(&mut self, additional: usize) {
737        self.buf.reserve(self.len, additional);
738    }
739
740    /// Reserves the minimum capacity for exactly `additional` more elements to
741    /// be inserted in the given `Vec<'bump, T>`. After calling `reserve_exact`,
742    /// capacity will be greater than or equal to `self.len() + additional`.
743    /// Does nothing if the capacity is already sufficient.
744    ///
745    /// Note that the allocator may give the collection more space than it
746    /// requests. Therefore capacity can not be relied upon to be precisely
747    /// minimal. Prefer `reserve` if future insertions are expected.
748    ///
749    /// # Panics
750    ///
751    /// Panics if the new capacity overflows `usize`.
752    ///
753    /// # Examples
754    ///
755    /// ```
756    /// use bumpalo::{Bump, collections::Vec};
757    ///
758    /// let b = Bump::new();
759    /// let mut vec = bumpalo::vec![in &b; 1];
760    /// vec.reserve_exact(10);
761    /// assert!(vec.capacity() >= 11);
762    /// ```
763    pub fn reserve_exact(&mut self, additional: usize) {
764        self.buf.reserve_exact(self.len, additional);
765    }
766
767    /// Attempts to reserve capacity for at least `additional` more elements to be inserted
768    /// in the given `Vec<'bump, T>`. The collection may reserve more space to avoid
769    /// frequent reallocations. After calling `try_reserve`, capacity will be
770    /// greater than or equal to `self.len() + additional`. Does nothing if
771    /// capacity is already sufficient.
772    ///
773    /// # Panics
774    ///
775    /// Panics if the new capacity overflows `usize`.
776    ///
777    /// # Examples
778    ///
779    /// ```
780    /// use bumpalo::{Bump, collections::Vec};
781    ///
782    /// let b = Bump::new();
783    /// let mut vec = bumpalo::vec![in &b; 1];
784    /// vec.try_reserve(10).unwrap();
785    /// assert!(vec.capacity() >= 11);
786    /// ```
787    pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
788        self.buf.try_reserve(self.len, additional)
789    }
790
791    /// Attempts to reserve the minimum capacity for exactly `additional` more elements to
792    /// be inserted in the given `Vec<'bump, T>`. After calling `try_reserve_exact`,
793    /// capacity will be greater than or equal to `self.len() + additional`.
794    /// Does nothing if the capacity is already sufficient.
795    ///
796    /// Note that the allocator may give the collection more space than it
797    /// requests. Therefore capacity can not be relied upon to be precisely
798    /// minimal. Prefer `try_reserve` if future insertions are expected.
799    ///
800    /// # Panics
801    ///
802    /// Panics if the new capacity overflows `usize`.
803    ///
804    /// # Examples
805    ///
806    /// ```
807    /// use bumpalo::{Bump, collections::Vec};
808    ///
809    /// let b = Bump::new();
810    /// let mut vec = bumpalo::vec![in &b; 1];
811    /// vec.try_reserve_exact(10).unwrap();
812    /// assert!(vec.capacity() >= 11);
813    /// ```
814    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> {
815        self.buf.try_reserve_exact(self.len, additional)
816    }
817
818    /// Shrinks the capacity of the vector as much as possible.
819    ///
820    /// It will drop down as close as possible to the length but the allocator
821    /// may still inform the vector that there is space for a few more elements.
822    ///
823    /// # Examples
824    ///
825    /// ```
826    /// use bumpalo::{Bump, collections::Vec};
827    ///
828    /// let b = Bump::new();
829    ///
830    /// let mut vec = Vec::with_capacity_in(10, &b);
831    /// vec.extend([1, 2, 3].iter().cloned());
832    /// assert_eq!(vec.capacity(), 10);
833    /// vec.shrink_to_fit();
834    /// assert!(vec.capacity() >= 3);
835    /// ```
836    pub fn shrink_to_fit(&mut self) {
837        if self.capacity() != self.len {
838            self.buf.shrink_to_fit(self.len);
839        }
840    }
841
842    /// Converts the vector into `&'bump [T]`.
843    ///
844    /// # Examples
845    ///
846    /// ```
847    /// use bumpalo::{Bump, collections::Vec};
848    ///
849    /// let b = Bump::new();
850    /// let v = bumpalo::vec![in &b; 1, 2, 3];
851    ///
852    /// let slice = v.into_bump_slice();
853    /// assert_eq!(slice, [1, 2, 3]);
854    /// ```
855    pub fn into_bump_slice(self) -> &'bump [T] {
856        unsafe {
857            let ptr = self.as_ptr();
858            let len = self.len();
859            mem::forget(self);
860            slice::from_raw_parts(ptr, len)
861        }
862    }
863
864    /// Converts the vector into `&'bump mut [T]`.
865    ///
866    /// # Examples
867    ///
868    /// ```
869    /// use bumpalo::{Bump, collections::Vec};
870    ///
871    /// let b = Bump::new();
872    /// let v = bumpalo::vec![in &b; 1, 2, 3];
873    ///
874    /// let mut slice = v.into_bump_slice_mut();
875    ///
876    /// slice[0] = 3;
877    /// slice[2] = 1;
878    ///
879    /// assert_eq!(slice, [3, 2, 1]);
880    /// ```
881    pub fn into_bump_slice_mut(mut self) -> &'bump mut [T] {
882        let ptr = self.as_mut_ptr();
883        let len = self.len();
884        mem::forget(self);
885
886        unsafe { slice::from_raw_parts_mut(ptr, len) }
887    }
888
889    /// Shortens the vector, keeping the first `len` elements and dropping
890    /// the rest.
891    ///
892    /// If `len` is greater than the vector's current length, this has no
893    /// effect.
894    ///
895    /// The [`drain`] method can emulate `truncate`, but causes the excess
896    /// elements to be returned instead of dropped.
897    ///
898    /// Note that this method has no effect on the allocated capacity
899    /// of the vector.
900    ///
901    /// # Examples
902    ///
903    /// Truncating a five element vector to two elements:
904    ///
905    /// ```
906    /// use bumpalo::{Bump, collections::Vec};
907    ///
908    /// let b = Bump::new();
909    ///
910    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4, 5];
911    /// vec.truncate(2);
912    /// assert_eq!(vec, [1, 2]);
913    /// ```
914    ///
915    /// No truncation occurs when `len` is greater than the vector's current
916    /// length:
917    ///
918    /// ```
919    /// use bumpalo::{Bump, collections::Vec};
920    ///
921    /// let b = Bump::new();
922    ///
923    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
924    /// vec.truncate(8);
925    /// assert_eq!(vec, [1, 2, 3]);
926    /// ```
927    ///
928    /// Truncating when `len == 0` is equivalent to calling the [`clear`]
929    /// method.
930    ///
931    /// ```
932    /// use bumpalo::{Bump, collections::Vec};
933    ///
934    /// let b = Bump::new();
935    ///
936    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
937    /// vec.truncate(0);
938    /// assert_eq!(vec, []);
939    /// ```
940    ///
941    /// [`clear`]: #method.clear
942    /// [`drain`]: #method.drain
943    pub fn truncate(&mut self, len: usize) {
944        let current_len = self.len;
945        unsafe {
946            let mut ptr = self.as_mut_ptr().add(self.len);
947            // Set the final length at the end, keeping in mind that
948            // dropping an element might panic. Works around a missed
949            // optimization, as seen in the following issue:
950            // https://github.com/rust-lang/rust/issues/51802
951            let mut local_len = SetLenOnDrop::new(&mut self.len);
952
953            // drop any extra elements
954            for _ in len..current_len {
955                local_len.decrement_len(1);
956                ptr = ptr.offset(-1);
957                ptr::drop_in_place(ptr);
958            }
959        }
960    }
961
962    /// Extracts a slice containing the entire vector.
963    ///
964    /// Equivalent to `&s[..]`.
965    ///
966    /// # Examples
967    ///
968    /// ```
969    /// use bumpalo::{Bump, collections::Vec};
970    /// use std::io::{self, Write};
971    ///
972    /// let b = Bump::new();
973    ///
974    /// let buffer = bumpalo::vec![in &b; 1, 2, 3, 5, 8];
975    /// io::sink().write(buffer.as_slice()).unwrap();
976    /// ```
977    #[inline]
978    pub fn as_slice(&self) -> &[T] {
979        self
980    }
981
982    /// Extracts a mutable slice of the entire vector.
983    ///
984    /// Equivalent to `&mut s[..]`.
985    ///
986    /// # Examples
987    ///
988    /// ```
989    /// use bumpalo::{Bump, collections::Vec};
990    /// use std::io::{self, Read};
991    ///
992    /// let b = Bump::new();
993    /// let mut buffer = bumpalo::vec![in &b; 0; 3];
994    /// io::repeat(0b101).read_exact(buffer.as_mut_slice()).unwrap();
995    /// ```
996    #[inline]
997    pub fn as_mut_slice(&mut self) -> &mut [T] {
998        self
999    }
1000
1001    /// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
1002    /// valid for zero sized reads if the vector didn't allocate.
1003    ///
1004    /// The caller must ensure that the vector outlives the pointer this
1005    /// function returns, or else it will end up pointing to garbage.
1006    /// Modifying the vector may cause its buffer to be reallocated,
1007    /// which would also make any pointers to it invalid.
1008    ///
1009    /// The caller must also ensure that the memory the pointer (non-transitively) points to
1010    /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
1011    /// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
1012    ///
1013    /// # Examples
1014    ///
1015    /// ```
1016    /// use bumpalo::{Bump, collections::Vec};
1017    ///
1018    /// let bump = Bump::new();
1019    ///
1020    /// let x = bumpalo::vec![in &bump; 1, 2, 4];
1021    /// let x_ptr = x.as_ptr();
1022    ///
1023    /// unsafe {
1024    ///     for i in 0..x.len() {
1025    ///         assert_eq!(*x_ptr.add(i), 1 << i);
1026    ///     }
1027    /// }
1028    /// ```
1029    ///
1030    /// [`as_mut_ptr`]: Vec::as_mut_ptr
1031    #[inline]
1032    pub fn as_ptr(&self) -> *const T {
1033        // We shadow the slice method of the same name to avoid going through
1034        // `deref`, which creates an intermediate reference.
1035        let ptr = self.buf.ptr();
1036        unsafe {
1037            if ptr.is_null() {
1038                core::hint::unreachable_unchecked();
1039            }
1040        }
1041        ptr
1042    }
1043
1044    /// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
1045    /// raw pointer valid for zero sized reads if the vector didn't allocate.
1046    ///
1047    /// The caller must ensure that the vector outlives the pointer this
1048    /// function returns, or else it will end up pointing to garbage.
1049    /// Modifying the vector may cause its buffer to be reallocated,
1050    /// which would also make any pointers to it invalid.
1051    ///
1052    /// # Examples
1053    ///
1054    /// ```
1055    /// use bumpalo::{Bump, collections::Vec};
1056    ///
1057    /// let bump = Bump::new();
1058    ///
1059    /// // Allocate vector big enough for 4 elements.
1060    /// let size = 4;
1061    /// let mut x: Vec<i32> = Vec::with_capacity_in(size, &bump);
1062    /// let x_ptr = x.as_mut_ptr();
1063    ///
1064    /// // Initialize elements via raw pointer writes, then set length.
1065    /// unsafe {
1066    ///     for i in 0..size {
1067    ///         x_ptr.add(i).write(i as i32);
1068    ///     }
1069    ///     x.set_len(size);
1070    /// }
1071    /// assert_eq!(&*x, &[0, 1, 2, 3]);
1072    /// ```
1073    #[inline]
1074    pub fn as_mut_ptr(&mut self) -> *mut T {
1075        // We shadow the slice method of the same name to avoid going through
1076        // `deref_mut`, which creates an intermediate reference.
1077        let ptr = self.buf.ptr();
1078        unsafe {
1079            if ptr.is_null() {
1080                core::hint::unreachable_unchecked();
1081            }
1082        }
1083        ptr
1084    }
1085
1086    /// Sets the length of a vector.
1087    ///
1088    /// This will explicitly set the size of the vector, without actually
1089    /// modifying its buffers, so it is up to the caller to ensure that the
1090    /// vector is actually the specified size.
1091    ///
1092    /// # Safety
1093    ///
1094    /// - `new_len` must be less than or equal to [`capacity()`].
1095    /// - The elements at `old_len..new_len` must be initialized.
1096    ///
1097    /// [`capacity()`]: struct.Vec.html#method.capacity
1098    ///
1099    /// # Examples
1100    ///
1101    /// ```
1102    /// use bumpalo::{Bump, collections::Vec};
1103    ///
1104    /// use std::ptr;
1105    ///
1106    /// let b = Bump::new();
1107    ///
1108    /// let mut vec = bumpalo::vec![in &b; 'r', 'u', 's', 't'];
1109    ///
1110    /// unsafe {
1111    ///     ptr::drop_in_place(&mut vec[3]);
1112    ///     vec.set_len(3);
1113    /// }
1114    /// assert_eq!(vec, ['r', 'u', 's']);
1115    /// ```
1116    ///
1117    /// In this example, there is a memory leak since the memory locations
1118    /// owned by the inner vectors were not freed prior to the `set_len` call:
1119    ///
1120    /// ```
1121    /// use bumpalo::{Bump, collections::Vec};
1122    ///
1123    /// let b = Bump::new();
1124    ///
1125    /// let mut vec = bumpalo::vec![in &b;
1126    ///                             bumpalo::vec![in &b; 1, 0, 0],
1127    ///                             bumpalo::vec![in &b; 0, 1, 0],
1128    ///                             bumpalo::vec![in &b; 0, 0, 1]];
1129    /// unsafe {
1130    ///     vec.set_len(0);
1131    /// }
1132    /// ```
1133    ///
1134    /// In this example, the vector gets expanded from zero to four items
1135    /// but we directly initialize uninitialized memory:
1136    ///
1137    // TODO: rely upon `spare_capacity_mut`
1138    /// ```
1139    /// use bumpalo::{Bump, collections::Vec};
1140    ///
1141    /// let len = 4;
1142    /// let b = Bump::new();
1143    ///
1144    /// let mut vec: Vec<u8> = Vec::with_capacity_in(len, &b);
1145    ///
1146    /// for i in 0..len {
1147    ///     // SAFETY: we initialize memory via `pointer::write`
1148    ///     unsafe { vec.as_mut_ptr().add(i).write(b'a') }
1149    /// }
1150    ///
1151    /// unsafe {
1152    ///     vec.set_len(len);
1153    /// }
1154    ///
1155    /// assert_eq!(b"aaaa", &*vec);
1156    /// ```
1157    #[inline]
1158    pub unsafe fn set_len(&mut self, new_len: usize) {
1159        self.len = new_len;
1160    }
1161
1162    /// Removes an element from the vector and returns it.
1163    ///
1164    /// The removed element is replaced by the last element of the vector.
1165    ///
1166    /// This does not preserve ordering, but is O(1).
1167    ///
1168    /// # Panics
1169    ///
1170    /// Panics if `index` is out of bounds.
1171    ///
1172    /// # Examples
1173    ///
1174    /// ```
1175    /// use bumpalo::{Bump, collections::Vec};
1176    ///
1177    /// let b = Bump::new();
1178    ///
1179    /// let mut v = bumpalo::vec![in &b; "foo", "bar", "baz", "qux"];
1180    ///
1181    /// assert_eq!(v.swap_remove(1), "bar");
1182    /// assert_eq!(v, ["foo", "qux", "baz"]);
1183    ///
1184    /// assert_eq!(v.swap_remove(0), "foo");
1185    /// assert_eq!(v, ["baz", "qux"]);
1186    /// ```
1187    #[inline]
1188    pub fn swap_remove(&mut self, index: usize) -> T {
1189        unsafe {
1190            // We replace self[index] with the last element. Note that if the
1191            // bounds check on hole succeeds there must be a last element (which
1192            // can be self[index] itself).
1193            let hole: *mut T = &mut self[index];
1194            let last = ptr::read(self.get_unchecked(self.len - 1));
1195            self.len -= 1;
1196            ptr::replace(hole, last)
1197        }
1198    }
1199
1200    /// Inserts an element at position `index` within the vector, shifting all
1201    /// elements after it to the right.
1202    ///
1203    /// # Panics
1204    ///
1205    /// Panics if `index > len`.
1206    ///
1207    /// # Examples
1208    ///
1209    /// ```
1210    /// use bumpalo::{Bump, collections::Vec};
1211    ///
1212    /// let b = Bump::new();
1213    ///
1214    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1215    /// vec.insert(1, 4);
1216    /// assert_eq!(vec, [1, 4, 2, 3]);
1217    /// vec.insert(4, 5);
1218    /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1219    /// ```
1220    pub fn insert(&mut self, index: usize, element: T) {
1221        let len = self.len();
1222        assert!(index <= len);
1223
1224        // space for the new element
1225        if len == self.buf.cap() {
1226            self.reserve(1);
1227        }
1228
1229        unsafe {
1230            // infallible
1231            // The spot to put the new value
1232            {
1233                let p = self.as_mut_ptr().add(index);
1234                // Shift everything over to make space. (Duplicating the
1235                // `index`th element into two consecutive places.)
1236                ptr::copy(p, p.offset(1), len - index);
1237                // Write it in, overwriting the first copy of the `index`th
1238                // element.
1239                ptr::write(p, element);
1240            }
1241            self.set_len(len + 1);
1242        }
1243    }
1244
1245    /// Removes and returns the element at position `index` within the vector,
1246    /// shifting all elements after it to the left.
1247    ///
1248    /// # Panics
1249    ///
1250    /// Panics if `index` is out of bounds.
1251    ///
1252    /// # Examples
1253    ///
1254    /// ```
1255    /// use bumpalo::{Bump, collections::Vec};
1256    ///
1257    /// let b = Bump::new();
1258    ///
1259    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
1260    /// assert_eq!(v.remove(1), 2);
1261    /// assert_eq!(v, [1, 3]);
1262    /// ```
1263    pub fn remove(&mut self, index: usize) -> T {
1264        let len = self.len();
1265        assert!(index < len);
1266        unsafe {
1267            // infallible
1268            let ret;
1269            {
1270                // the place we are taking from.
1271                let ptr = self.as_mut_ptr().add(index);
1272                // copy it out, unsafely having a copy of the value on
1273                // the stack and in the vector at the same time.
1274                ret = ptr::read(ptr);
1275
1276                // Shift everything down to fill in that spot.
1277                ptr::copy(ptr.offset(1), ptr, len - index - 1);
1278            }
1279            self.set_len(len - 1);
1280            ret
1281        }
1282    }
1283
1284    /// Retains only the elements specified by the predicate.
1285    ///
1286    /// In other words, remove all elements `e` such that `f(&e)` returns `false`.
1287    /// This method operates in place and preserves the order of the retained
1288    /// elements.
1289    ///
1290    /// # Examples
1291    ///
1292    /// ```
1293    /// use bumpalo::{Bump, collections::Vec};
1294    ///
1295    /// let b = Bump::new();
1296    ///
1297    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4];
1298    /// vec.retain(|x: &i32| *x % 2 == 0);
1299    /// assert_eq!(vec, [2, 4]);
1300    /// ```
1301    pub fn retain<F>(&mut self, mut f: F)
1302    where
1303        F: FnMut(&T) -> bool,
1304    {
1305        self.drain_filter(|x| !f(x));
1306    }
1307
1308    /// Retains only the elements specified by the predicate.
1309    ///
1310    /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
1311    /// This method operates in place and preserves the order of the retained
1312    /// elements.
1313    ///
1314    /// # Examples
1315    ///
1316    /// ```
1317    /// use bumpalo::{Bump, collections::Vec};
1318    ///
1319    /// let b = Bump::new();
1320    ///
1321    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4];
1322    /// vec.retain_mut(|x: &mut i32| *x % 2 == 0);
1323    /// assert_eq!(vec, [2, 4]);
1324    /// ```
1325    pub fn retain_mut<F>(&mut self, mut f: F)
1326    where
1327        F: FnMut(&mut T) -> bool,
1328    {
1329        self.drain_filter(|x| !f(x));
1330    }
1331
1332    /// Creates an iterator that removes the elements in the vector
1333    /// for which the predicate returns `true` and yields the removed items.
1334    ///
1335    /// # Examples
1336    ///
1337    /// ```
1338    /// use bumpalo::Bump;
1339    /// use bumpalo::collections::{CollectIn, Vec};
1340    ///
1341    /// let b = Bump::new();
1342    ///
1343    /// let mut numbers = bumpalo::vec![in &b; 1, 2, 3, 4, 5];
1344    ///
1345    /// let evens: Vec<_> = numbers.drain_filter(|x| *x % 2 == 0).collect_in(&b);
1346    ///
1347    /// assert_eq!(numbers, &[1, 3, 5]);
1348    /// assert_eq!(evens, &[2, 4]);
1349    /// ```
1350    pub fn drain_filter<'a, F>(&'a mut self, filter: F) -> DrainFilter<'a, 'bump, T, F>
1351    where
1352        F: FnMut(&mut T) -> bool,
1353    {
1354        let old_len = self.len();
1355
1356        // Guard against us getting leaked (leak amplification)
1357        unsafe {
1358            self.set_len(0);
1359        }
1360
1361        DrainFilter {
1362            vec: self,
1363            idx: 0,
1364            del: 0,
1365            old_len,
1366            pred: filter,
1367        }
1368    }
1369
1370    /// Removes all but the first of consecutive elements in the vector that resolve to the same
1371    /// key.
1372    ///
1373    /// If the vector is sorted, this removes all duplicates.
1374    ///
1375    /// # Examples
1376    ///
1377    /// ```
1378    /// use bumpalo::{Bump, collections::Vec};
1379    ///
1380    /// let b = Bump::new();
1381    ///
1382    /// let mut vec = bumpalo::vec![in &b; 10, 20, 21, 30, 20];
1383    ///
1384    /// vec.dedup_by_key(|i| *i / 10);
1385    ///
1386    /// assert_eq!(vec, [10, 20, 30, 20]);
1387    /// ```
1388    #[inline]
1389    pub fn dedup_by_key<F, K>(&mut self, mut key: F)
1390    where
1391        F: FnMut(&mut T) -> K,
1392        K: PartialEq,
1393    {
1394        self.dedup_by(|a, b| key(a) == key(b))
1395    }
1396
1397    /// Removes all but the first of consecutive elements in the vector satisfying a given equality
1398    /// relation.
1399    ///
1400    /// The `same_bucket` function is passed references to two elements from the vector and
1401    /// must determine if the elements compare equal. The elements are passed in opposite order
1402    /// from their order in the slice, so if `same_bucket(a, b)` returns `true`, `a` is removed.
1403    ///
1404    /// If the vector is sorted, this removes all duplicates.
1405    ///
1406    /// # Examples
1407    ///
1408    /// ```
1409    /// use bumpalo::{Bump, collections::Vec};
1410    ///
1411    /// let b = Bump::new();
1412    ///
1413    /// let mut vec = bumpalo::vec![in &b; "foo", "bar", "Bar", "baz", "bar"];
1414    ///
1415    /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1416    ///
1417    /// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
1418    /// ```
1419    pub fn dedup_by<F>(&mut self, same_bucket: F)
1420    where
1421        F: FnMut(&mut T, &mut T) -> bool,
1422    {
1423        let len = {
1424            let (dedup, _) = partition_dedup_by(self.as_mut_slice(), same_bucket);
1425            dedup.len()
1426        };
1427        self.truncate(len);
1428    }
1429
1430    // Proven specification with verus, converted to comments.
1431    /// # Preconditions
1432    ///
1433    /// - old(self).len() < old(self).capacity(),
1434    ///
1435    /// # Postconditions
1436    ///
1437    /// - self.get_unchecked(old(self).len()) == value,
1438    /// - self.len()      == old(self).len() + 1,
1439    /// - self.capacity() == old(self).capacity(),
1440    /// - forall|i: usize| implies(
1441    ///       i < old(self).len(),
1442    ///       self.get_unchecked(i) == old(self).get_unchecked(i)
1443    ///   )
1444    #[allow(clippy::inline_always)]
1445    #[inline(always)]
1446    unsafe fn push_unchecked(&mut self, value: T) {
1447        debug_assert!(self.len() < self.capacity());
1448
1449        // Divergence from verified impl:
1450        //   Verified implementation has special handling for ZSTs
1451        //   as ZSTs do not play nicely with separation logic.
1452        ptr::write(self.buf.ptr().add(self.len), value);
1453
1454        self.len = self.len + 1;
1455    }
1456
1457    /// Appends an element to the back of a vector.
1458    ///
1459    /// # Panics
1460    ///
1461    /// Panics if the number of elements in the vector overflows a `usize`.
1462    ///
1463    /// # Examples
1464    ///
1465    /// ```
1466    /// use bumpalo::{Bump, collections::Vec};
1467    ///
1468    /// let b = Bump::new();
1469    ///
1470    /// let mut vec = bumpalo::vec![in &b; 1, 2];
1471    /// vec.push(3);
1472    /// assert_eq!(vec, [1, 2, 3]);
1473    /// ```
1474    #[inline]
1475    pub fn push(&mut self, value: T) {
1476        // This will panic or abort if we would allocate > isize::MAX bytes
1477        // or if the length increment would overflow for zero-sized types.
1478        if self.len == self.buf.cap() {
1479            self.reserve(1);
1480        }
1481        unsafe {
1482            self.push_unchecked(value);
1483        }
1484    }
1485
1486    /// Removes the last element from a vector and returns it, or [`None`] if it
1487    /// is empty.
1488    ///
1489    /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None
1490    ///
1491    /// # Examples
1492    ///
1493    /// ```
1494    /// use bumpalo::{Bump, collections::Vec};
1495    ///
1496    /// let b = Bump::new();
1497    ///
1498    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1499    /// assert_eq!(vec.pop(), Some(3));
1500    /// assert_eq!(vec, [1, 2]);
1501    /// ```
1502    #[inline]
1503    pub fn pop(&mut self) -> Option<T> {
1504        if self.len == 0 {
1505            None
1506        } else {
1507            unsafe {
1508                self.len -= 1;
1509                Some(ptr::read(self.as_ptr().add(self.len())))
1510            }
1511        }
1512    }
1513
1514    /// Moves all the elements of `other` into `Self`, leaving `other` empty.
1515    ///
1516    /// # Panics
1517    ///
1518    /// Panics if the number of elements in the vector overflows a `usize`.
1519    ///
1520    /// # Examples
1521    ///
1522    /// ```
1523    /// use bumpalo::{Bump, collections::Vec};
1524    ///
1525    /// let b = Bump::new();
1526    ///
1527    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1528    /// let mut vec2 = bumpalo::vec![in &b; 4, 5, 6];
1529    /// vec.append(&mut vec2);
1530    /// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
1531    /// assert_eq!(vec2, []);
1532    /// ```
1533    #[inline]
1534    pub fn append(&mut self, other: &mut Self) {
1535        unsafe {
1536            self.append_elements(other.as_slice() as _);
1537            other.set_len(0);
1538        }
1539    }
1540
1541    /// Appends elements to `Self` from other buffer.
1542    #[inline]
1543    unsafe fn append_elements(&mut self, other: *const [T]) {
1544        let count = (&(*other)).len();
1545        self.reserve(count);
1546        let len = self.len();
1547        ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count);
1548        self.len += count;
1549    }
1550
1551    /// Creates a draining iterator that removes the specified range in the vector
1552    /// and yields the removed items.
1553    ///
1554    /// Note 1: The element range is removed even if the iterator is only
1555    /// partially consumed or not consumed at all.
1556    ///
1557    /// Note 2: It is unspecified how many elements are removed from the vector
1558    /// if the `Drain` value is leaked.
1559    ///
1560    /// # Panics
1561    ///
1562    /// Panics if the starting point is greater than the end point or if
1563    /// the end point is greater than the length of the vector.
1564    ///
1565    /// # Examples
1566    ///
1567    /// ```
1568    /// use bumpalo::Bump;
1569    /// use bumpalo::collections::{CollectIn, Vec};
1570    ///
1571    /// let b = Bump::new();
1572    ///
1573    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
1574    ///
1575    /// let u: Vec<_> = v.drain(1..).collect_in(&b);
1576    ///
1577    /// assert_eq!(v, &[1]);
1578    /// assert_eq!(u, &[2, 3]);
1579    ///
1580    /// // A full range clears the vector
1581    /// v.drain(..);
1582    /// assert_eq!(v, &[]);
1583    /// ```
1584    pub fn drain<R>(&mut self, range: R) -> Drain<T>
1585    where
1586        R: RangeBounds<usize>,
1587    {
1588        // Memory safety
1589        //
1590        // When the Drain is first created, it shortens the length of
1591        // the source vector to make sure no uninitialized or moved-from elements
1592        // are accessible at all if the Drain's destructor never gets to run.
1593        //
1594        // Drain will ptr::read out the values to remove.
1595        // When finished, remaining tail of the vec is copied back to cover
1596        // the hole, and the vector length is restored to the new length.
1597        //
1598        let len = self.len();
1599        let start = match range.start_bound() {
1600            Included(&n) => n,
1601            Excluded(&n) => n + 1,
1602            Unbounded => 0,
1603        };
1604        let end = match range.end_bound() {
1605            Included(&n) => n + 1,
1606            Excluded(&n) => n,
1607            Unbounded => len,
1608        };
1609        assert!(start <= end);
1610        assert!(end <= len);
1611
1612        unsafe {
1613            // set self.vec length's to start, to be safe in case Drain is leaked
1614            self.set_len(start);
1615            // Use the borrow in the IterMut to indicate borrowing behavior of the
1616            // whole Drain iterator (like &mut T).
1617            let range_slice = slice::from_raw_parts_mut(self.as_mut_ptr().add(start), end - start);
1618            Drain {
1619                tail_start: end,
1620                tail_len: len - end,
1621                iter: range_slice.iter(),
1622                vec: NonNull::from(self),
1623            }
1624        }
1625    }
1626
1627    /// Clears the vector, removing all values.
1628    ///
1629    /// Note that this method has no effect on the allocated capacity
1630    /// of the vector.
1631    ///
1632    /// # Examples
1633    ///
1634    /// ```
1635    /// use bumpalo::{Bump, collections::Vec};
1636    ///
1637    /// let b = Bump::new();
1638    ///
1639    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
1640    ///
1641    /// v.clear();
1642    ///
1643    /// assert!(v.is_empty());
1644    /// ```
1645    #[inline]
1646    pub fn clear(&mut self) {
1647        self.truncate(0)
1648    }
1649
1650    /// Returns the number of elements in the vector, also referred to
1651    /// as its 'length'.
1652    ///
1653    /// # Examples
1654    ///
1655    /// ```
1656    /// use bumpalo::{Bump, collections::Vec};
1657    ///
1658    /// let b = Bump::new();
1659    ///
1660    /// let a = bumpalo::vec![in &b; 1, 2, 3];
1661    /// assert_eq!(a.len(), 3);
1662    /// ```
1663    #[inline]
1664    pub fn len(&self) -> usize {
1665        self.len
1666    }
1667
1668    /// Returns `true` if the vector contains no elements.
1669    ///
1670    /// # Examples
1671    ///
1672    /// ```
1673    /// use bumpalo::{Bump, collections::Vec};
1674    ///
1675    /// let b = Bump::new();
1676    ///
1677    /// let mut v = Vec::new_in(&b);
1678    /// assert!(v.is_empty());
1679    ///
1680    /// v.push(1);
1681    /// assert!(!v.is_empty());
1682    /// ```
1683    pub fn is_empty(&self) -> bool {
1684        self.len() == 0
1685    }
1686
1687    /// Splits the collection into two at the given index.
1688    ///
1689    /// Returns a newly allocated vector. `self` contains elements `[0, at)`,
1690    /// and the returned vector contains elements `[at, len)`.
1691    ///
1692    /// Note that the capacity of `self` does not change.
1693    ///
1694    /// # Panics
1695    ///
1696    /// Panics if `at > len`.
1697    ///
1698    /// # Examples
1699    ///
1700    /// ```
1701    /// use bumpalo::{Bump, collections::Vec};
1702    ///
1703    /// let b = Bump::new();
1704    ///
1705    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1706    /// let vec2 = vec.split_off(1);
1707    /// assert_eq!(vec, [1]);
1708    /// assert_eq!(vec2, [2, 3]);
1709    /// ```
1710    #[inline]
1711    pub fn split_off(&mut self, at: usize) -> Self {
1712        assert!(at <= self.len(), "`at` out of bounds");
1713
1714        let other_len = self.len - at;
1715        let mut other = Vec::with_capacity_in(other_len, self.buf.bump());
1716
1717        // Unsafely `set_len` and copy items to `other`.
1718        unsafe {
1719            self.set_len(at);
1720            other.set_len(other_len);
1721
1722            ptr::copy_nonoverlapping(self.as_ptr().add(at), other.as_mut_ptr(), other.len());
1723        }
1724        other
1725    }
1726}
1727
1728#[cfg(feature = "boxed")]
1729impl<'bump, T> Vec<'bump, T> {
1730    /// Converts the vector into [`Box<[T]>`][owned slice].
1731    ///
1732    /// Note that this will drop any excess capacity.
1733    ///
1734    /// [owned slice]: ../../boxed/struct.Box.html
1735    ///
1736    /// # Examples
1737    ///
1738    /// ```
1739    /// use bumpalo::{Bump, collections::Vec, vec};
1740    ///
1741    /// let b = Bump::new();
1742    ///
1743    /// let v = vec![in &b; 1, 2, 3];
1744    ///
1745    /// let slice = v.into_boxed_slice();
1746    /// ```
1747    pub fn into_boxed_slice(mut self) -> crate::boxed::Box<'bump, [T]> {
1748        use crate::boxed::Box;
1749
1750        // Unlike `alloc::vec::Vec` shrinking here isn't necessary as `bumpalo::boxed::Box` doesn't own memory.
1751        unsafe {
1752            let slice = slice::from_raw_parts_mut(self.as_mut_ptr(), self.len);
1753            let output: Box<'bump, [T]> = Box::from_raw(slice);
1754            mem::forget(self);
1755            output
1756        }
1757    }
1758}
1759
1760impl<'bump, T: 'bump + Clone> Vec<'bump, T> {
1761    /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
1762    ///
1763    /// If `new_len` is greater than `len`, the `Vec` is extended by the
1764    /// difference, with each additional slot filled with `value`.
1765    /// If `new_len` is less than `len`, the `Vec` is simply truncated.
1766    ///
1767    /// This method requires [`Clone`] to be able clone the passed value. If
1768    /// you need more flexibility (or want to rely on [`Default`] instead of
1769    /// [`Clone`]), use [`resize_with`].
1770    ///
1771    /// # Examples
1772    ///
1773    /// ```
1774    /// use bumpalo::{Bump, collections::Vec};
1775    ///
1776    /// let b = Bump::new();
1777    ///
1778    /// let mut vec = bumpalo::vec![in &b; "hello"];
1779    /// vec.resize(3, "world");
1780    /// assert_eq!(vec, ["hello", "world", "world"]);
1781    ///
1782    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4];
1783    /// vec.resize(2, 0);
1784    /// assert_eq!(vec, [1, 2]);
1785    /// ```
1786    ///
1787    /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html
1788    /// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html
1789    /// [`resize_with`]: #method.resize_with
1790    pub fn resize(&mut self, new_len: usize, value: T) {
1791        let len = self.len();
1792
1793        if new_len > len {
1794            self.extend_with(new_len - len, ExtendElement(value))
1795        } else {
1796            self.truncate(new_len);
1797        }
1798    }
1799
1800    // Proven specification with verus, converted to comments.
1801    /// # Preconditions
1802    ///
1803    /// - old(self).len() + slice.len() <= old(self).capacity(),
1804    ///
1805    /// # Postconditions
1806    ///
1807    /// - forall|i: usize| implies(
1808    ///       i < old(self).len(),
1809    ///       self.get_unchecked(i) == old(self).get_unchecked(i)
1810    ///   ),
1811    /// - forall|i: usize| implies(
1812    ///       i < slice.len(),
1813    ///       self.get_unchecked((old(self).len() + i) as usize)
1814    ///           == clone(slice.get_unchecked(i))
1815    ///   ),
1816    /// - self.len()      == old(self).len() + slice.len(),
1817    /// - self.capacity() == old(self).capacity(),
1818    #[inline]
1819    unsafe fn extend_from_slice_unchecked(&mut self, slice: &[T]) {
1820        // Guaranteed never to overflow for non ZSTs
1821        //   size_of::<T>() <= isize::MAX - (isize::MAX % align_of::<T>()))
1822        //   isize::MAX + isize::MAX < usize::MAX
1823        debug_assert!(
1824            core::mem::size_of::<T>() == 0 || self.capacity() >= self.len() + slice.len()
1825        );
1826        debug_assert!(
1827            // is_zst::<T>() ==> capacity >= slen + slice.len()
1828            core::mem::size_of::<T>() != 0
1829            // Capacity is usize::MAX for ZSTs
1830            || self.len() <= usize::MAX - slice.len()
1831        );
1832
1833        let mut pos = 0usize;
1834
1835        loop
1836        /*
1837        invariants
1838            pos <= slice.len(),
1839            self.len() + (slice.len() - pos) <= old(self).capacity(),
1840            old(self).capacity() == self.capacity(),
1841
1842            self.len() == old(self).len() + pos,
1843
1844            forall|i: usize| implies(
1845                i < old(self).len(),
1846                self.get_unchecked(i) == old(self).get_unchecked(i)
1847            ),
1848            forall|i: usize| implies(
1849                i < pos,
1850                self.get_unchecked((old(self).len() + i) as usize)
1851                    == clone(slice.get_unchecked(i))
1852            )
1853        */
1854        {
1855            if pos == slice.len() {
1856                /*
1857                pos = slice.len(),
1858                self.len() = old(self).len() + slice.len(),
1859
1860                forall|i: usize| i < slice.len() implies {
1861                    self.get_unchecked((old(self).len() + i) as usize)
1862                        == clone(slice.get_unchecked(i))
1863                }
1864                by {
1865                    i < pos
1866                }
1867                */
1868                return;
1869            }
1870
1871            /*
1872            pos < slice.len(),
1873            self.len() < self.capacity()
1874            */
1875
1876            let elem = slice.get_unchecked(pos);
1877            self.push_unchecked(elem.clone());
1878
1879            /*
1880            ghost prev_pos = pos
1881            */
1882
1883            pos = pos + 1;
1884
1885            /*
1886            forall|i: usize| i < pos implies {
1887                self.get_unchecked((old(self).len() + i) as usize)
1888                    == clone(slice.get_unchecked(i))
1889            }
1890            by {
1891                if i < pos - 1 {
1892                    // By invariant
1893                }
1894                else {
1895                    i == prev_pos
1896                }
1897            }
1898            */
1899        }
1900    }
1901
1902    /// Clones and appends all elements in a slice to the `Vec`.
1903    ///
1904    /// Iterates over the slice `other`, clones each element, and then appends
1905    /// it to this `Vec`. The `other` vector is traversed in-order.
1906    ///
1907    /// Note that this function is same as [`extend`] except that it is
1908    /// specialized to work with slices instead. If and when Rust gets
1909    /// specialization this function will likely be deprecated (but still
1910    /// available).
1911    ///
1912    /// # Examples
1913    ///
1914    /// ```
1915    /// use bumpalo::{Bump, collections::Vec};
1916    ///
1917    /// let b = Bump::new();
1918    ///
1919    /// let mut vec = bumpalo::vec![in &b; 1];
1920    /// vec.extend_from_slice(&[2, 3, 4]);
1921    /// assert_eq!(vec, [1, 2, 3, 4]);
1922    /// ```
1923    ///
1924    /// [`extend`]: #method.extend
1925    pub fn extend_from_slice(&mut self, other: &[T]) {
1926        let capacity = self.capacity();
1927
1928        /*
1929        Cannot underflow via invariant of the Vec (capacity >= length).
1930
1931        This also holds for ZSTs, as capacity is usize::MAX
1932        */
1933        let remaining_cap = capacity - self.len;
1934
1935        /*
1936            self.len() + other.len() <= self.capacity(),
1937                <==>
1938            other.len() <= self.capacity() - self.len()
1939        */
1940
1941        if other.len() > remaining_cap {
1942            /*
1943            Divergence from verified impl:
1944              Verified implementation's reserve is not the same
1945              as bumpalo's. Verified implementation reserves with
1946              respect to capacity, not length. Thus this is equivalent
1947              to the verified implementation's:
1948
1949                self.buf.reserve(other.len() - remaining_cap)
1950
1951            */
1952            self.reserve(other.len());
1953        }
1954
1955        /*
1956        self.capacity() >= self.len() + other.len()
1957        */
1958
1959        unsafe {
1960            self.extend_from_slice_unchecked(other);
1961        }
1962    }
1963}
1964
1965impl<'bump, T: 'bump + Copy> Vec<'bump, T> {
1966    /// Helper method to copy all of the items in `other` and append them to the end of `self`.
1967    ///
1968    /// SAFETY:
1969    ///   * The caller is responsible for:
1970    ///       * calling [`reserve`](Self::reserve) beforehand to guarantee that there is enough
1971    ///         capacity to store `other.len()` more items.
1972    ///       * guaranteeing that `self` and `other` do not overlap.
1973    unsafe fn extend_from_slice_copy_unchecked(&mut self, other: &[T]) {
1974        let old_len = self.len();
1975        debug_assert!(old_len + other.len() <= self.capacity());
1976
1977        // SAFETY:
1978        // * `src` is valid for reads of `other.len()` values by virtue of being a `&[T]`.
1979        // * `dst` is valid for writes of `other.len()` bytes because the caller of this
1980        //   method is required to `reserve` capacity to store at least `other.len()` items
1981        //   beforehand.
1982        // * Because `src` is a `&[T]` and dst is a `&[T]` within the `Vec<T>`,
1983        //   `copy_nonoverlapping`'s alignment requirements are met.
1984        // * Caller is required to guarantee that the source and destination ranges cannot overlap
1985        unsafe {
1986            let src = other.as_ptr();
1987            let dst = self.as_mut_ptr().add(old_len);
1988            ptr::copy_nonoverlapping(src, dst, other.len());
1989            self.set_len(old_len + other.len());
1990        }
1991    }
1992
1993    /// Copies all elements in the slice `other` and appends them to the `Vec`.
1994    ///
1995    /// Note that this function is same as [`extend_from_slice`] except that it is optimized for
1996    /// slices of types that implement the `Copy` trait. If and when Rust gets specialization
1997    /// this function will likely be deprecated (but still available).
1998    ///
1999    /// To copy and append the data from multiple source slices at once, see
2000    /// [`extend_from_slices_copy`].
2001    ///
2002    /// # Examples
2003    ///
2004    /// ```
2005    /// use bumpalo::{Bump, collections::Vec};
2006    ///
2007    /// let b = Bump::new();
2008    ///
2009    /// let mut vec = bumpalo::vec![in &b; 1];
2010    /// vec.extend_from_slice_copy(&[2, 3, 4]);
2011    /// assert_eq!(vec, [1, 2, 3, 4]);
2012    /// ```
2013    ///
2014    /// ```
2015    /// use bumpalo::{Bump, collections::Vec};
2016    ///
2017    /// let b = Bump::new();
2018    ///
2019    /// let mut vec = bumpalo::vec![in &b; 'H' as u8];
2020    /// vec.extend_from_slice_copy("ello, world!".as_bytes());
2021    /// assert_eq!(vec, "Hello, world!".as_bytes());
2022    /// ```
2023    ///
2024    /// [`extend_from_slice`]: #method.extend_from_slice
2025    /// [`extend_from_slices`]: #method.extend_from_slices
2026    pub fn extend_from_slice_copy(&mut self, other: &[T]) {
2027        // Reserve space in the Vec for the values to be added
2028        self.reserve(other.len());
2029
2030        // Copy values into the space that was just reserved
2031        // SAFETY:
2032        // * `self` has enough capacity to store `other.len()` more items as `self.reserve(other.len())`
2033        //   above guarantees that.
2034        // * Source and destination data ranges cannot overlap as we just reserved the destination
2035        //   range from the bump.
2036        unsafe {
2037            self.extend_from_slice_copy_unchecked(other);
2038        }
2039    }
2040
2041    /// For each slice in `slices`, copies all elements in the slice and appends them to the `Vec`.
2042    ///
2043    /// This method is equivalent to calling [`extend_from_slice_copy`] in a loop, but is able
2044    /// to precompute the total amount of space to reserve in advance. This reduces the potential
2045    /// maximum number of reallocations needed from one-per-slice to just one.
2046    ///
2047    /// # Examples
2048    ///
2049    /// ```
2050    /// use bumpalo::{Bump, collections::Vec};
2051    ///
2052    /// let b = Bump::new();
2053    ///
2054    /// let mut vec = bumpalo::vec![in &b; 1];
2055    /// vec.extend_from_slices_copy(&[&[2, 3], &[], &[4]]);
2056    /// assert_eq!(vec, [1, 2, 3, 4]);
2057    /// ```
2058    ///
2059    /// ```
2060    /// use bumpalo::{Bump, collections::Vec};
2061    ///
2062    /// let b = Bump::new();
2063    ///
2064    /// let mut vec = bumpalo::vec![in &b; 'H' as u8];
2065    /// vec.extend_from_slices_copy(&["ello,".as_bytes(), &[], " world!".as_bytes()]);
2066    /// assert_eq!(vec, "Hello, world!".as_bytes());
2067    /// ```
2068    ///
2069    /// [`extend_from_slice_copy`]: #method.extend_from_slice_copy
2070    pub fn extend_from_slices_copy(&mut self, slices: &[&[T]]) {
2071        // Reserve the total amount of capacity we'll need to safely append the aggregated contents
2072        // of each slice in `slices`.
2073        let capacity_to_reserve: usize = slices.iter().map(|slice| slice.len()).sum();
2074        self.reserve(capacity_to_reserve);
2075
2076        // SAFETY:
2077        // * `dst` is valid for writes of `capacity_to_reserve` items as
2078        //   `self.reserve(capacity_to_reserve)` above guarantees that.
2079        // * Source and destination ranges cannot overlap as we just reserved the destination
2080        //   range from the bump.
2081        unsafe {
2082            // Copy the contents of each slice onto the end of `self`
2083            slices.iter().for_each(|slice| {
2084                self.extend_from_slice_copy_unchecked(slice);
2085            });
2086        }
2087    }
2088}
2089
2090// This code generalises `extend_with_{element,default}`.
2091trait ExtendWith<T> {
2092    fn next(&mut self) -> T;
2093    fn last(self) -> T;
2094}
2095
2096struct ExtendElement<T>(T);
2097impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
2098    fn next(&mut self) -> T {
2099        self.0.clone()
2100    }
2101    fn last(self) -> T {
2102        self.0
2103    }
2104}
2105
2106impl<'bump, T: 'bump> Vec<'bump, T> {
2107    /// Extend the vector by `n` values, using the given generator.
2108    fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) {
2109        self.reserve(n);
2110
2111        unsafe {
2112            let mut ptr = self.as_mut_ptr().add(self.len());
2113            // Use SetLenOnDrop to work around bug where compiler
2114            // may not realize the store through `ptr` through self.set_len()
2115            // don't alias.
2116            let mut local_len = SetLenOnDrop::new(&mut self.len);
2117
2118            // Write all elements except the last one
2119            for _ in 1..n {
2120                ptr::write(ptr, value.next());
2121                ptr = ptr.offset(1);
2122                // Increment the length in every step in case next() panics
2123                local_len.increment_len(1);
2124            }
2125
2126            if n > 0 {
2127                // We can write the last element directly without cloning needlessly
2128                ptr::write(ptr, value.last());
2129                local_len.increment_len(1);
2130            }
2131
2132            // len set by scope guard
2133        }
2134    }
2135}
2136
2137// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
2138//
2139// The idea is: The length field in SetLenOnDrop is a local variable
2140// that the optimizer will see does not alias with any stores through the Vec's data
2141// pointer. This is a workaround for alias analysis issue #32155
2142struct SetLenOnDrop<'a> {
2143    len: &'a mut usize,
2144    local_len: usize,
2145}
2146
2147impl<'a> SetLenOnDrop<'a> {
2148    #[inline]
2149    fn new(len: &'a mut usize) -> Self {
2150        SetLenOnDrop {
2151            local_len: *len,
2152            len,
2153        }
2154    }
2155
2156    #[inline]
2157    fn increment_len(&mut self, increment: usize) {
2158        self.local_len += increment;
2159    }
2160
2161    #[inline]
2162    fn decrement_len(&mut self, decrement: usize) {
2163        self.local_len -= decrement;
2164    }
2165}
2166
2167impl<'a> Drop for SetLenOnDrop<'a> {
2168    #[inline]
2169    fn drop(&mut self) {
2170        *self.len = self.local_len;
2171    }
2172}
2173
2174impl<'bump, T: 'bump + PartialEq> Vec<'bump, T> {
2175    /// Removes consecutive repeated elements in the vector according to the
2176    /// [`PartialEq`] trait implementation.
2177    ///
2178    /// If the vector is sorted, this removes all duplicates.
2179    ///
2180    /// # Examples
2181    ///
2182    /// ```
2183    /// use bumpalo::{Bump, collections::Vec};
2184    ///
2185    /// let b = Bump::new();
2186    ///
2187    /// let mut vec = bumpalo::vec![in &b; 1, 2, 2, 3, 2];
2188    ///
2189    /// vec.dedup();
2190    ///
2191    /// assert_eq!(vec, [1, 2, 3, 2]);
2192    /// ```
2193    #[inline]
2194    pub fn dedup(&mut self) {
2195        self.dedup_by(|a, b| a == b)
2196    }
2197}
2198
2199////////////////////////////////////////////////////////////////////////////////
2200// Common trait implementations for Vec
2201////////////////////////////////////////////////////////////////////////////////
2202
2203impl<'bump, T: 'bump + Clone> Clone for Vec<'bump, T> {
2204    #[cfg(not(test))]
2205    fn clone(&self) -> Vec<'bump, T> {
2206        let mut v = Vec::with_capacity_in(self.len(), self.buf.bump());
2207        v.extend(self.iter().cloned());
2208        v
2209    }
2210
2211    // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
2212    // required for this method definition, is not available. Instead use the
2213    // `slice::to_vec`  function which is only available with cfg(test)
2214    // NB see the slice::hack module in slice.rs for more information
2215    #[cfg(test)]
2216    fn clone(&self) -> Vec<'bump, T> {
2217        let mut v = Vec::new_in(self.buf.bump());
2218        v.extend(self.iter().cloned());
2219        v
2220    }
2221}
2222
2223impl<'bump, T: 'bump + Hash> Hash for Vec<'bump, T> {
2224    #[inline]
2225    fn hash<H: hash::Hasher>(&self, state: &mut H) {
2226        Hash::hash(&**self, state)
2227    }
2228}
2229
2230impl<'bump, T, I> Index<I> for Vec<'bump, T>
2231where
2232    I: ::core::slice::SliceIndex<[T]>,
2233{
2234    type Output = I::Output;
2235
2236    #[inline]
2237    fn index(&self, index: I) -> &Self::Output {
2238        Index::index(&**self, index)
2239    }
2240}
2241
2242impl<'bump, T, I> IndexMut<I> for Vec<'bump, T>
2243where
2244    I: ::core::slice::SliceIndex<[T]>,
2245{
2246    #[inline]
2247    fn index_mut(&mut self, index: I) -> &mut Self::Output {
2248        IndexMut::index_mut(&mut **self, index)
2249    }
2250}
2251
2252impl<'bump, T: 'bump> ops::Deref for Vec<'bump, T> {
2253    type Target = [T];
2254
2255    fn deref(&self) -> &[T] {
2256        unsafe {
2257            let p = self.buf.ptr();
2258            // assume(!p.is_null());
2259            slice::from_raw_parts(p, self.len)
2260        }
2261    }
2262}
2263
2264impl<'bump, T: 'bump> ops::DerefMut for Vec<'bump, T> {
2265    fn deref_mut(&mut self) -> &mut [T] {
2266        unsafe {
2267            let ptr = self.buf.ptr();
2268            // assume(!ptr.is_null());
2269            slice::from_raw_parts_mut(ptr, self.len)
2270        }
2271    }
2272}
2273
2274impl<'bump, T: 'bump> IntoIterator for Vec<'bump, T> {
2275    type Item = T;
2276    type IntoIter = IntoIter<'bump, T>;
2277
2278    /// Creates a consuming iterator, that is, one that moves each value out of
2279    /// the vector (from start to end). The vector cannot be used after calling
2280    /// this.
2281    ///
2282    /// # Examples
2283    ///
2284    /// ```
2285    /// use bumpalo::{Bump, collections::Vec};
2286    ///
2287    /// let b = Bump::new();
2288    ///
2289    /// let v = bumpalo::vec![in &b; "a".to_string(), "b".to_string()];
2290    /// for s in v.into_iter() {
2291    ///     // s has type String, not &String
2292    ///     println!("{}", s);
2293    /// }
2294    /// ```
2295    #[inline]
2296    fn into_iter(mut self) -> IntoIter<'bump, T> {
2297        unsafe {
2298            let begin = self.as_mut_ptr();
2299            // assume(!begin.is_null());
2300            let end = if mem::size_of::<T>() == 0 {
2301                arith_offset(begin as *const i8, self.len() as isize) as *const T
2302            } else {
2303                begin.add(self.len()) as *const T
2304            };
2305            mem::forget(self);
2306            IntoIter {
2307                phantom: PhantomData,
2308                ptr: begin,
2309                end,
2310            }
2311        }
2312    }
2313}
2314
2315impl<'a, 'bump, T> IntoIterator for &'a Vec<'bump, T> {
2316    type Item = &'a T;
2317    type IntoIter = slice::Iter<'a, T>;
2318
2319    fn into_iter(self) -> slice::Iter<'a, T> {
2320        self.iter()
2321    }
2322}
2323
2324impl<'a, 'bump, T> IntoIterator for &'a mut Vec<'bump, T> {
2325    type Item = &'a mut T;
2326    type IntoIter = slice::IterMut<'a, T>;
2327
2328    fn into_iter(self) -> slice::IterMut<'a, T> {
2329        self.iter_mut()
2330    }
2331}
2332
2333impl<'bump, T: 'bump> Extend<T> for Vec<'bump, T> {
2334    #[inline]
2335    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
2336        let iter = iter.into_iter();
2337        self.reserve(iter.size_hint().0);
2338
2339        for t in iter {
2340            self.push(t);
2341        }
2342    }
2343}
2344
2345impl<'bump, T: 'bump> Vec<'bump, T> {
2346    /// Creates a splicing iterator that replaces the specified range in the vector
2347    /// with the given `replace_with` iterator and yields the removed items.
2348    /// `replace_with` does not need to be the same length as `range`.
2349    ///
2350    /// Note 1: The element range is removed even if the iterator is not
2351    /// consumed until the end.
2352    ///
2353    /// Note 2: It is unspecified how many elements are removed from the vector,
2354    /// if the `Splice` value is leaked.
2355    ///
2356    /// Note 3: The input iterator `replace_with` is only consumed
2357    /// when the `Splice` value is dropped.
2358    ///
2359    /// Note 4: This is optimal if:
2360    ///
2361    /// * The tail (elements in the vector after `range`) is empty,
2362    /// * or `replace_with` yields fewer elements than `range`’s length
2363    /// * or the lower bound of its `size_hint()` is exact.
2364    ///
2365    /// Otherwise, a temporary vector is allocated and the tail is moved twice.
2366    ///
2367    /// # Panics
2368    ///
2369    /// Panics if the starting point is greater than the end point or if
2370    /// the end point is greater than the length of the vector.
2371    ///
2372    /// # Examples
2373    ///
2374    /// ```
2375    /// use bumpalo::{Bump, collections::Vec};
2376    ///
2377    /// let b = Bump::new();
2378    ///
2379    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
2380    /// let new = [7, 8];
2381    /// let u: Vec<_> = Vec::from_iter_in(v.splice(..2, new.iter().cloned()), &b);
2382    /// assert_eq!(v, &[7, 8, 3]);
2383    /// assert_eq!(u, &[1, 2]);
2384    /// ```
2385    #[inline]
2386    pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<I::IntoIter>
2387    where
2388        R: RangeBounds<usize>,
2389        I: IntoIterator<Item = T>,
2390    {
2391        Splice {
2392            drain: self.drain(range),
2393            replace_with: replace_with.into_iter(),
2394        }
2395    }
2396}
2397
2398/// Extend implementation that copies elements out of references before pushing them onto the Vec.
2399///
2400/// This implementation is specialized for slice iterators, where it uses [`copy_from_slice`] to
2401/// append the entire slice at once.
2402///
2403/// [`copy_from_slice`]: https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice
2404impl<'a, 'bump, T: 'a + Copy> Extend<&'a T> for Vec<'bump, T> {
2405    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
2406        self.extend(iter.into_iter().cloned())
2407    }
2408}
2409
2410macro_rules! __impl_slice_eq1 {
2411    ($Lhs: ty, $Rhs: ty) => {
2412        __impl_slice_eq1! { $Lhs, $Rhs, Sized }
2413    };
2414    ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
2415        impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs
2416        where
2417            A: PartialEq<B>,
2418        {
2419            #[inline]
2420            fn eq(&self, other: &$Rhs) -> bool {
2421                self[..] == other[..]
2422            }
2423        }
2424    };
2425}
2426
2427__impl_slice_eq1! { Vec<'a, A>, Vec<'b, B> }
2428__impl_slice_eq1! { Vec<'a, A>, &'b [B] }
2429__impl_slice_eq1! { Vec<'a, A>, &'b mut [B] }
2430// __impl_slice_eq1! { Cow<'a, [A]>, Vec<'b, B>, Clone }
2431
2432macro_rules! __impl_slice_eq1_array {
2433    ($Lhs: ty, $Rhs: ty) => {
2434        impl<'a, 'b, A, B, const N: usize> PartialEq<$Rhs> for $Lhs
2435        where
2436            A: PartialEq<B>,
2437        {
2438            #[inline]
2439            fn eq(&self, other: &$Rhs) -> bool {
2440                self[..] == other[..]
2441            }
2442        }
2443    };
2444}
2445
2446__impl_slice_eq1_array! { Vec<'a, A>, [B; N] }
2447__impl_slice_eq1_array! { Vec<'a, A>, &'b [B; N] }
2448__impl_slice_eq1_array! { Vec<'a, A>, &'b mut [B; N] }
2449
2450/// Implements comparison of vectors, lexicographically.
2451impl<'bump, T: 'bump + PartialOrd> PartialOrd for Vec<'bump, T> {
2452    #[inline]
2453    fn partial_cmp(&self, other: &Vec<'bump, T>) -> Option<Ordering> {
2454        PartialOrd::partial_cmp(&**self, &**other)
2455    }
2456}
2457
2458impl<'bump, T: 'bump + Eq> Eq for Vec<'bump, T> {}
2459
2460/// Implements ordering of vectors, lexicographically.
2461impl<'bump, T: 'bump + Ord> Ord for Vec<'bump, T> {
2462    #[inline]
2463    fn cmp(&self, other: &Vec<'bump, T>) -> Ordering {
2464        Ord::cmp(&**self, &**other)
2465    }
2466}
2467
2468impl<'bump, T: 'bump + fmt::Debug> fmt::Debug for Vec<'bump, T> {
2469    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2470        fmt::Debug::fmt(&**self, f)
2471    }
2472}
2473
2474impl<'bump, T: 'bump> AsRef<Vec<'bump, T>> for Vec<'bump, T> {
2475    fn as_ref(&self) -> &Vec<'bump, T> {
2476        self
2477    }
2478}
2479
2480impl<'bump, T: 'bump> AsMut<Vec<'bump, T>> for Vec<'bump, T> {
2481    fn as_mut(&mut self) -> &mut Vec<'bump, T> {
2482        self
2483    }
2484}
2485
2486impl<'bump, T: 'bump> AsRef<[T]> for Vec<'bump, T> {
2487    fn as_ref(&self) -> &[T] {
2488        self
2489    }
2490}
2491
2492impl<'bump, T: 'bump> AsMut<[T]> for Vec<'bump, T> {
2493    fn as_mut(&mut self) -> &mut [T] {
2494        self
2495    }
2496}
2497
2498#[cfg(feature = "boxed")]
2499impl<'bump, T: 'bump> From<Vec<'bump, T>> for crate::boxed::Box<'bump, [T]> {
2500    fn from(v: Vec<'bump, T>) -> crate::boxed::Box<'bump, [T]> {
2501        v.into_boxed_slice()
2502    }
2503}
2504
2505impl<'bump, T: 'bump> Borrow<[T]> for Vec<'bump, T> {
2506    #[inline]
2507    fn borrow(&self) -> &[T] {
2508        &self[..]
2509    }
2510}
2511
2512impl<'bump, T: 'bump> BorrowMut<[T]> for Vec<'bump, T> {
2513    #[inline]
2514    fn borrow_mut(&mut self) -> &mut [T] {
2515        &mut self[..]
2516    }
2517}
2518
2519impl<'bump, T> Drop for Vec<'bump, T> {
2520    fn drop(&mut self) {
2521        unsafe {
2522            // use drop for [T]
2523            // use a raw slice to refer to the elements of the vector as weakest necessary type;
2524            // could avoid questions of validity in certain cases
2525            ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
2526        }
2527        // RawVec handles deallocation
2528    }
2529}
2530
2531////////////////////////////////////////////////////////////////////////////////
2532// Clone-on-write
2533////////////////////////////////////////////////////////////////////////////////
2534
2535// impl<'a, 'bump, T: Clone> From<Vec<'bump, T>> for Cow<'a, [T]> {
2536//     fn from(v: Vec<'bump, T>) -> Cow<'a, [T]> {
2537//         Cow::Owned(v)
2538//     }
2539// }
2540
2541// impl<'a, 'bump, T: Clone> From<&'a Vec<'bump, T>> for Cow<'a, [T]> {
2542//     fn from(v: &'a Vec<'bump, T>) -> Cow<'a, [T]> {
2543//         Cow::Borrowed(v.as_slice())
2544//     }
2545// }
2546
2547////////////////////////////////////////////////////////////////////////////////
2548// Iterators
2549////////////////////////////////////////////////////////////////////////////////
2550
2551/// An iterator that moves out of a vector.
2552///
2553/// This `struct` is created by the [`Vec::into_iter`] method
2554/// (provided by the [`IntoIterator`] trait).
2555///
2556/// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
2557pub struct IntoIter<'bump, T> {
2558    phantom: PhantomData<&'bump [T]>,
2559    ptr: *const T,
2560    end: *const T,
2561}
2562
2563impl<'bump, T: fmt::Debug> fmt::Debug for IntoIter<'bump, T> {
2564    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2565        f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
2566    }
2567}
2568
2569impl<'bump, T: 'bump> IntoIter<'bump, T> {
2570    /// Returns the remaining items of this iterator as a slice.
2571    ///
2572    /// # Examples
2573    ///
2574    /// ```
2575    /// use bumpalo::{Bump, collections::Vec};
2576    ///
2577    /// let b = Bump::new();
2578    ///
2579    /// let vec = bumpalo::vec![in &b; 'a', 'b', 'c'];
2580    /// let mut into_iter = vec.into_iter();
2581    /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
2582    /// let _ = into_iter.next().unwrap();
2583    /// assert_eq!(into_iter.as_slice(), &['b', 'c']);
2584    /// ```
2585    pub fn as_slice(&self) -> &[T] {
2586        unsafe { slice::from_raw_parts(self.ptr, self.len()) }
2587    }
2588
2589    /// Returns the remaining items of this iterator as a mutable slice.
2590    ///
2591    /// # Examples
2592    ///
2593    /// ```
2594    /// use bumpalo::{Bump, collections::Vec};
2595    ///
2596    /// let b = Bump::new();
2597    ///
2598    /// let vec = bumpalo::vec![in &b; 'a', 'b', 'c'];
2599    /// let mut into_iter = vec.into_iter();
2600    /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
2601    /// into_iter.as_mut_slice()[2] = 'z';
2602    /// assert_eq!(into_iter.next().unwrap(), 'a');
2603    /// assert_eq!(into_iter.next().unwrap(), 'b');
2604    /// assert_eq!(into_iter.next().unwrap(), 'z');
2605    /// ```
2606    pub fn as_mut_slice(&mut self) -> &mut [T] {
2607        unsafe { slice::from_raw_parts_mut(self.ptr as *mut T, self.len()) }
2608    }
2609}
2610
2611unsafe impl<'bump, T: Send> Send for IntoIter<'bump, T> {}
2612unsafe impl<'bump, T: Sync> Sync for IntoIter<'bump, T> {}
2613
2614impl<'bump, T: 'bump> Iterator for IntoIter<'bump, T> {
2615    type Item = T;
2616
2617    #[inline]
2618    fn next(&mut self) -> Option<T> {
2619        unsafe {
2620            if self.ptr as *const _ == self.end {
2621                None
2622            } else if mem::size_of::<T>() == 0 {
2623                // purposefully don't use 'ptr.offset' because for
2624                // vectors with 0-size elements this would return the
2625                // same pointer.
2626                self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;
2627
2628                // Make up a value of this ZST.
2629                Some(mem::zeroed())
2630            } else {
2631                let old = self.ptr;
2632                self.ptr = self.ptr.offset(1);
2633
2634                Some(ptr::read(old))
2635            }
2636        }
2637    }
2638
2639    #[inline]
2640    fn size_hint(&self) -> (usize, Option<usize>) {
2641        let exact = if mem::size_of::<T>() == 0 {
2642            (self.end as usize).wrapping_sub(self.ptr as usize)
2643        } else {
2644            unsafe { offset_from(self.end, self.ptr) as usize }
2645        };
2646        (exact, Some(exact))
2647    }
2648
2649    #[inline]
2650    fn count(self) -> usize {
2651        self.len()
2652    }
2653}
2654
2655impl<'bump, T: 'bump> DoubleEndedIterator for IntoIter<'bump, T> {
2656    #[inline]
2657    fn next_back(&mut self) -> Option<T> {
2658        unsafe {
2659            if self.end == self.ptr {
2660                None
2661            } else if mem::size_of::<T>() == 0 {
2662                // See above for why 'ptr.offset' isn't used
2663                self.end = arith_offset(self.end as *const i8, -1) as *mut T;
2664
2665                // Make up a value of this ZST.
2666                Some(mem::zeroed())
2667            } else {
2668                self.end = self.end.offset(-1);
2669
2670                Some(ptr::read(self.end))
2671            }
2672        }
2673    }
2674}
2675
2676impl<'bump, T: 'bump> ExactSizeIterator for IntoIter<'bump, T> {}
2677
2678impl<'bump, T: 'bump> FusedIterator for IntoIter<'bump, T> {}
2679
2680impl<'bump, T> Drop for IntoIter<'bump, T> {
2681    fn drop(&mut self) {
2682        // drop all remaining elements
2683        self.for_each(drop);
2684    }
2685}
2686
2687/// A draining iterator for `Vec<'bump, T>`.
2688///
2689/// This `struct` is created by the [`Vec::drain`] method.
2690pub struct Drain<'a, 'bump, T: 'a + 'bump> {
2691    /// Index of tail to preserve
2692    tail_start: usize,
2693    /// Length of tail
2694    tail_len: usize,
2695    /// Current remaining range to remove
2696    iter: slice::Iter<'a, T>,
2697    vec: NonNull<Vec<'bump, T>>,
2698}
2699
2700impl<'a, 'bump, T: 'a + 'bump + fmt::Debug> fmt::Debug for Drain<'a, 'bump, T> {
2701    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2702        f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
2703    }
2704}
2705
2706unsafe impl<'a, 'bump, T: Sync> Sync for Drain<'a, 'bump, T> {}
2707unsafe impl<'a, 'bump, T: Send> Send for Drain<'a, 'bump, T> {}
2708
2709impl<'a, 'bump, T> Iterator for Drain<'a, 'bump, T> {
2710    type Item = T;
2711
2712    #[inline]
2713    fn next(&mut self) -> Option<T> {
2714        self.iter
2715            .next()
2716            .map(|elt| unsafe { ptr::read(elt as *const _) })
2717    }
2718
2719    fn size_hint(&self) -> (usize, Option<usize>) {
2720        self.iter.size_hint()
2721    }
2722}
2723
2724impl<'a, 'bump, T> DoubleEndedIterator for Drain<'a, 'bump, T> {
2725    #[inline]
2726    fn next_back(&mut self) -> Option<T> {
2727        self.iter
2728            .next_back()
2729            .map(|elt| unsafe { ptr::read(elt as *const _) })
2730    }
2731}
2732
2733impl<'a, 'bump, T> Drop for Drain<'a, 'bump, T> {
2734    fn drop(&mut self) {
2735        // exhaust self first
2736        self.for_each(drop);
2737
2738        if self.tail_len > 0 {
2739            unsafe {
2740                let source_vec = self.vec.as_mut();
2741                // memmove back untouched tail, update to new length
2742                let start = source_vec.len();
2743                let tail = self.tail_start;
2744                if tail != start {
2745                    let src = source_vec.as_ptr().add(tail);
2746                    let dst = source_vec.as_mut_ptr().add(start);
2747                    ptr::copy(src, dst, self.tail_len);
2748                }
2749                source_vec.set_len(start + self.tail_len);
2750            }
2751        }
2752    }
2753}
2754
2755impl<'a, 'bump, T> ExactSizeIterator for Drain<'a, 'bump, T> {}
2756
2757impl<'a, 'bump, T> FusedIterator for Drain<'a, 'bump, T> {}
2758
2759/// A splicing iterator for `Vec`.
2760///
2761/// This struct is created by the [`Vec::splice`] method. See its
2762/// documentation for more information.
2763#[derive(Debug)]
2764pub struct Splice<'a, 'bump, I: Iterator + 'a + 'bump> {
2765    drain: Drain<'a, 'bump, I::Item>,
2766    replace_with: I,
2767}
2768
2769impl<'a, 'bump, I: Iterator> Iterator for Splice<'a, 'bump, I> {
2770    type Item = I::Item;
2771
2772    fn next(&mut self) -> Option<Self::Item> {
2773        self.drain.next()
2774    }
2775
2776    fn size_hint(&self) -> (usize, Option<usize>) {
2777        self.drain.size_hint()
2778    }
2779}
2780
2781impl<'a, 'bump, I: Iterator> DoubleEndedIterator for Splice<'a, 'bump, I> {
2782    fn next_back(&mut self) -> Option<Self::Item> {
2783        self.drain.next_back()
2784    }
2785}
2786
2787impl<'a, 'bump, I: Iterator> ExactSizeIterator for Splice<'a, 'bump, I> {}
2788
2789impl<'a, 'bump, I: Iterator> Drop for Splice<'a, 'bump, I> {
2790    fn drop(&mut self) {
2791        self.drain.by_ref().for_each(drop);
2792
2793        unsafe {
2794            if self.drain.tail_len == 0 {
2795                self.drain.vec.as_mut().extend(self.replace_with.by_ref());
2796                return;
2797            }
2798
2799            // First fill the range left by drain().
2800            if !self.drain.fill(&mut self.replace_with) {
2801                return;
2802            }
2803
2804            // There may be more elements. Use the lower bound as an estimate.
2805            // FIXME: Is the upper bound a better guess? Or something else?
2806            let (lower_bound, _upper_bound) = self.replace_with.size_hint();
2807            if lower_bound > 0 {
2808                self.drain.move_tail(lower_bound);
2809                if !self.drain.fill(&mut self.replace_with) {
2810                    return;
2811                }
2812            }
2813
2814            // Collect any remaining elements.
2815            // This is a zero-length vector which does not allocate if `lower_bound` was exact.
2816            let mut collected = Vec::new_in(self.drain.vec.as_ref().buf.bump());
2817            collected.extend(self.replace_with.by_ref());
2818            let mut collected = collected.into_iter();
2819            // Now we have an exact count.
2820            if collected.len() > 0 {
2821                self.drain.move_tail(collected.len());
2822                let filled = self.drain.fill(&mut collected);
2823                debug_assert!(filled);
2824                debug_assert_eq!(collected.len(), 0);
2825            }
2826        }
2827        // Let `Drain::drop` move the tail back if necessary and restore `vec.len`.
2828    }
2829}
2830
2831/// Private helper methods for `Splice::drop`
2832impl<'a, 'bump, T> Drain<'a, 'bump, T> {
2833    /// The range from `self.vec.len` to `self.tail_start` contains elements
2834    /// that have been moved out.
2835    /// Fill that range as much as possible with new elements from the `replace_with` iterator.
2836    /// Return whether we filled the entire range. (`replace_with.next()` didn’t return `None`.)
2837    unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool {
2838        let vec = self.vec.as_mut();
2839        let range_start = vec.len;
2840        let range_end = self.tail_start;
2841        let range_slice =
2842            slice::from_raw_parts_mut(vec.as_mut_ptr().add(range_start), range_end - range_start);
2843
2844        for place in range_slice {
2845            if let Some(new_item) = replace_with.next() {
2846                ptr::write(place, new_item);
2847                vec.len += 1;
2848            } else {
2849                return false;
2850            }
2851        }
2852        true
2853    }
2854
2855    /// Make room for inserting more elements before the tail.
2856    unsafe fn move_tail(&mut self, extra_capacity: usize) {
2857        let vec = self.vec.as_mut();
2858        let used_capacity = self.tail_start + self.tail_len;
2859        vec.buf.reserve(used_capacity, extra_capacity);
2860
2861        let new_tail_start = self.tail_start + extra_capacity;
2862        let src = vec.as_ptr().add(self.tail_start);
2863        let dst = vec.as_mut_ptr().add(new_tail_start);
2864        ptr::copy(src, dst, self.tail_len);
2865        self.tail_start = new_tail_start;
2866    }
2867}
2868
2869/// An iterator produced by calling [`Vec::drain_filter`].
2870#[derive(Debug)]
2871pub struct DrainFilter<'a, 'bump: 'a, T: 'a + 'bump, F>
2872where
2873    F: FnMut(&mut T) -> bool,
2874{
2875    vec: &'a mut Vec<'bump, T>,
2876    idx: usize,
2877    del: usize,
2878    old_len: usize,
2879    pred: F,
2880}
2881
2882impl<'a, 'bump, T, F> Iterator for DrainFilter<'a, 'bump, T, F>
2883where
2884    F: FnMut(&mut T) -> bool,
2885{
2886    type Item = T;
2887
2888    fn next(&mut self) -> Option<T> {
2889        unsafe {
2890            while self.idx != self.old_len {
2891                let i = self.idx;
2892                self.idx += 1;
2893                let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len);
2894                if (self.pred)(&mut v[i]) {
2895                    self.del += 1;
2896                    return Some(ptr::read(&v[i]));
2897                } else if self.del > 0 {
2898                    let del = self.del;
2899                    let src: *const T = &v[i];
2900                    let dst: *mut T = &mut v[i - del];
2901                    // This is safe because self.vec has length 0
2902                    // thus its elements will not have Drop::drop
2903                    // called on them in the event of a panic.
2904                    ptr::copy_nonoverlapping(src, dst, 1);
2905                }
2906            }
2907            None
2908        }
2909    }
2910
2911    fn size_hint(&self) -> (usize, Option<usize>) {
2912        (0, Some(self.old_len - self.idx))
2913    }
2914}
2915
2916impl<'a, 'bump, T, F> Drop for DrainFilter<'a, 'bump, T, F>
2917where
2918    F: FnMut(&mut T) -> bool,
2919{
2920    fn drop(&mut self) {
2921        self.for_each(drop);
2922        unsafe {
2923            self.vec.set_len(self.old_len - self.del);
2924        }
2925    }
2926}
2927
2928#[cfg(feature = "std")]
2929impl<'bump> io::Write for Vec<'bump, u8> {
2930    #[inline]
2931    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2932        self.extend_from_slice_copy(buf);
2933        Ok(buf.len())
2934    }
2935
2936    #[inline]
2937    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
2938        self.extend_from_slice_copy(buf);
2939        Ok(())
2940    }
2941
2942    #[inline]
2943    fn flush(&mut self) -> io::Result<()> {
2944        Ok(())
2945    }
2946}
2947
2948#[cfg(feature = "serde")]
2949mod serialize {
2950    use super::*;
2951
2952    use serde::{ser::SerializeSeq, Serialize, Serializer};
2953
2954    impl<'a, T> Serialize for Vec<'a, T>
2955    where
2956        T: Serialize,
2957    {
2958        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2959        where
2960            S: Serializer,
2961        {
2962            let mut seq = serializer.serialize_seq(Some(self.len))?;
2963            for e in self.iter() {
2964                seq.serialize_element(e)?;
2965            }
2966            seq.end()
2967        }
2968    }
2969}