Skip to main content

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    /// Removes and returns the last element from a vector if the predicate
1515    /// returns `true`, or [`None`] if the predicate returns false or the vector
1516    /// is empty (the predicate will not be called in that case).
1517    ///
1518    /// # Examples
1519    ///
1520    /// ```
1521    /// use bumpalo::{Bump, collections::Vec};
1522    ///
1523    /// let b = Bump::new();
1524    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4];
1525    /// let pred = |x: &mut i32| *x % 2 == 0;
1526    ///
1527    /// assert_eq!(vec.pop_if(pred), Some(4));
1528    /// assert_eq!(vec, [1, 2, 3]);
1529    /// assert_eq!(vec.pop_if(pred), None);
1530    /// ```
1531    #[inline]
1532    pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T> {
1533        let last = self.last_mut()?;
1534        if predicate(last) {
1535            self.pop()
1536        } else {
1537            None
1538        }
1539    }
1540
1541    /// Moves all the elements of `other` into `Self`, leaving `other` empty.
1542    ///
1543    /// # Panics
1544    ///
1545    /// Panics if the number of elements in the vector overflows a `usize`.
1546    ///
1547    /// # Examples
1548    ///
1549    /// ```
1550    /// use bumpalo::{Bump, collections::Vec};
1551    ///
1552    /// let b = Bump::new();
1553    ///
1554    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1555    /// let mut vec2 = bumpalo::vec![in &b; 4, 5, 6];
1556    /// vec.append(&mut vec2);
1557    /// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
1558    /// assert_eq!(vec2, []);
1559    /// ```
1560    #[inline]
1561    pub fn append(&mut self, other: &mut Self) {
1562        unsafe {
1563            self.append_elements(other.as_slice() as _);
1564            other.set_len(0);
1565        }
1566    }
1567
1568    /// Appends elements to `Self` from other buffer.
1569    #[inline]
1570    unsafe fn append_elements(&mut self, other: *const [T]) {
1571        let count = (&(*other)).len();
1572        self.reserve(count);
1573        let len = self.len();
1574        ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr().add(len), count);
1575        self.len += count;
1576    }
1577
1578    /// Creates a draining iterator that removes the specified range in the vector
1579    /// and yields the removed items.
1580    ///
1581    /// Note 1: The element range is removed even if the iterator is only
1582    /// partially consumed or not consumed at all.
1583    ///
1584    /// Note 2: It is unspecified how many elements are removed from the vector
1585    /// if the `Drain` value is leaked.
1586    ///
1587    /// # Panics
1588    ///
1589    /// Panics if the starting point is greater than the end point or if
1590    /// the end point is greater than the length of the vector.
1591    ///
1592    /// # Examples
1593    ///
1594    /// ```
1595    /// use bumpalo::Bump;
1596    /// use bumpalo::collections::{CollectIn, Vec};
1597    ///
1598    /// let b = Bump::new();
1599    ///
1600    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
1601    ///
1602    /// let u: Vec<_> = v.drain(1..).collect_in(&b);
1603    ///
1604    /// assert_eq!(v, &[1]);
1605    /// assert_eq!(u, &[2, 3]);
1606    ///
1607    /// // A full range clears the vector
1608    /// v.drain(..);
1609    /// assert_eq!(v, &[]);
1610    /// ```
1611    pub fn drain<'a, R>(&'a mut self, range: R) -> Drain<'a, 'bump, T>
1612    where
1613        R: RangeBounds<usize>,
1614    {
1615        // Memory safety
1616        //
1617        // When the Drain is first created, it shortens the length of
1618        // the source vector to make sure no uninitialized or moved-from elements
1619        // are accessible at all if the Drain's destructor never gets to run.
1620        //
1621        // Drain will ptr::read out the values to remove.
1622        // When finished, remaining tail of the vec is copied back to cover
1623        // the hole, and the vector length is restored to the new length.
1624        //
1625        let len = self.len();
1626        let start = match range.start_bound() {
1627            Included(&n) => n,
1628            Excluded(&n) => n + 1,
1629            Unbounded => 0,
1630        };
1631        let end = match range.end_bound() {
1632            Included(&n) => n + 1,
1633            Excluded(&n) => n,
1634            Unbounded => len,
1635        };
1636        assert!(start <= end);
1637        assert!(end <= len);
1638
1639        unsafe {
1640            // set self.vec length's to start, to be safe in case Drain is leaked
1641            self.set_len(start);
1642            // Use the borrow in the IterMut to indicate borrowing behavior of the
1643            // whole Drain iterator (like &mut T).
1644            let range_slice = slice::from_raw_parts_mut(self.as_mut_ptr().add(start), end - start);
1645            Drain {
1646                tail_start: end,
1647                tail_len: len - end,
1648                iter: range_slice.iter(),
1649                vec: NonNull::from(self),
1650            }
1651        }
1652    }
1653
1654    /// Clears the vector, removing all values.
1655    ///
1656    /// Note that this method has no effect on the allocated capacity
1657    /// of the vector.
1658    ///
1659    /// # Examples
1660    ///
1661    /// ```
1662    /// use bumpalo::{Bump, collections::Vec};
1663    ///
1664    /// let b = Bump::new();
1665    ///
1666    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
1667    ///
1668    /// v.clear();
1669    ///
1670    /// assert!(v.is_empty());
1671    /// ```
1672    #[inline]
1673    pub fn clear(&mut self) {
1674        self.truncate(0)
1675    }
1676
1677    /// Returns the number of elements in the vector, also referred to
1678    /// as its 'length'.
1679    ///
1680    /// # Examples
1681    ///
1682    /// ```
1683    /// use bumpalo::{Bump, collections::Vec};
1684    ///
1685    /// let b = Bump::new();
1686    ///
1687    /// let a = bumpalo::vec![in &b; 1, 2, 3];
1688    /// assert_eq!(a.len(), 3);
1689    /// ```
1690    #[inline]
1691    pub fn len(&self) -> usize {
1692        self.len
1693    }
1694
1695    /// Returns `true` if the vector contains no elements.
1696    ///
1697    /// # Examples
1698    ///
1699    /// ```
1700    /// use bumpalo::{Bump, collections::Vec};
1701    ///
1702    /// let b = Bump::new();
1703    ///
1704    /// let mut v = Vec::new_in(&b);
1705    /// assert!(v.is_empty());
1706    ///
1707    /// v.push(1);
1708    /// assert!(!v.is_empty());
1709    /// ```
1710    pub fn is_empty(&self) -> bool {
1711        self.len() == 0
1712    }
1713
1714    /// Splits the collection into two at the given index.
1715    ///
1716    /// Returns a newly allocated vector. `self` contains elements `[0, at)`,
1717    /// and the returned vector contains elements `[at, len)`.
1718    ///
1719    /// Note that the capacity of `self` does not change.
1720    ///
1721    /// # Panics
1722    ///
1723    /// Panics if `at > len`.
1724    ///
1725    /// # Examples
1726    ///
1727    /// ```
1728    /// use bumpalo::{Bump, collections::Vec};
1729    ///
1730    /// let b = Bump::new();
1731    ///
1732    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3];
1733    /// let vec2 = vec.split_off(1);
1734    /// assert_eq!(vec, [1]);
1735    /// assert_eq!(vec2, [2, 3]);
1736    /// ```
1737    #[inline]
1738    pub fn split_off(&mut self, at: usize) -> Self {
1739        assert!(at <= self.len(), "`at` out of bounds");
1740
1741        let other_len = self.len - at;
1742        let mut other = Vec::with_capacity_in(other_len, self.buf.bump());
1743
1744        // Unsafely `set_len` and copy items to `other`.
1745        unsafe {
1746            self.set_len(at);
1747            other.set_len(other_len);
1748
1749            ptr::copy_nonoverlapping(self.as_ptr().add(at), other.as_mut_ptr(), other.len());
1750        }
1751        other
1752    }
1753}
1754
1755#[cfg(feature = "boxed")]
1756impl<'bump, T> Vec<'bump, T> {
1757    /// Converts the vector into [`Box<[T]>`][owned slice].
1758    ///
1759    /// Note that this will drop any excess capacity.
1760    ///
1761    /// [owned slice]: ../../boxed/struct.Box.html
1762    ///
1763    /// # Examples
1764    ///
1765    /// ```
1766    /// use bumpalo::{Bump, collections::Vec, vec};
1767    ///
1768    /// let b = Bump::new();
1769    ///
1770    /// let v = vec![in &b; 1, 2, 3];
1771    ///
1772    /// let slice = v.into_boxed_slice();
1773    /// ```
1774    pub fn into_boxed_slice(mut self) -> crate::boxed::Box<'bump, [T]> {
1775        use crate::boxed::Box;
1776
1777        // Unlike `alloc::vec::Vec` shrinking here isn't necessary as `bumpalo::boxed::Box` doesn't own memory.
1778        unsafe {
1779            let slice = slice::from_raw_parts_mut(self.as_mut_ptr(), self.len);
1780            let output: Box<'bump, [T]> = Box::from_raw(slice);
1781            mem::forget(self);
1782            output
1783        }
1784    }
1785}
1786
1787impl<'bump, T: 'bump + Clone> Vec<'bump, T> {
1788    /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
1789    ///
1790    /// If `new_len` is greater than `len`, the `Vec` is extended by the
1791    /// difference, with each additional slot filled with `value`.
1792    /// If `new_len` is less than `len`, the `Vec` is simply truncated.
1793    ///
1794    /// This method requires [`Clone`] to be able clone the passed value. If
1795    /// you need more flexibility (or want to rely on [`Default`] instead of
1796    /// [`Clone`]), use [`resize_with`].
1797    ///
1798    /// # Examples
1799    ///
1800    /// ```
1801    /// use bumpalo::{Bump, collections::Vec};
1802    ///
1803    /// let b = Bump::new();
1804    ///
1805    /// let mut vec = bumpalo::vec![in &b; "hello"];
1806    /// vec.resize(3, "world");
1807    /// assert_eq!(vec, ["hello", "world", "world"]);
1808    ///
1809    /// let mut vec = bumpalo::vec![in &b; 1, 2, 3, 4];
1810    /// vec.resize(2, 0);
1811    /// assert_eq!(vec, [1, 2]);
1812    /// ```
1813    ///
1814    /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html
1815    /// [`Default`]: https://doc.rust-lang.org/std/default/trait.Default.html
1816    /// [`resize_with`]: #method.resize_with
1817    pub fn resize(&mut self, new_len: usize, value: T) {
1818        let len = self.len();
1819
1820        if new_len > len {
1821            self.extend_with(new_len - len, ExtendElement(value))
1822        } else {
1823            self.truncate(new_len);
1824        }
1825    }
1826
1827    // Proven specification with verus, converted to comments.
1828    /// # Preconditions
1829    ///
1830    /// - old(self).len() + slice.len() <= old(self).capacity(),
1831    ///
1832    /// # Postconditions
1833    ///
1834    /// - forall|i: usize| implies(
1835    ///       i < old(self).len(),
1836    ///       self.get_unchecked(i) == old(self).get_unchecked(i)
1837    ///   ),
1838    /// - forall|i: usize| implies(
1839    ///       i < slice.len(),
1840    ///       self.get_unchecked((old(self).len() + i) as usize)
1841    ///           == clone(slice.get_unchecked(i))
1842    ///   ),
1843    /// - self.len()      == old(self).len() + slice.len(),
1844    /// - self.capacity() == old(self).capacity(),
1845    #[inline]
1846    unsafe fn extend_from_slice_unchecked(&mut self, slice: &[T]) {
1847        // Guaranteed never to overflow for non ZSTs
1848        //   size_of::<T>() <= isize::MAX - (isize::MAX % align_of::<T>()))
1849        //   isize::MAX + isize::MAX < usize::MAX
1850        debug_assert!(
1851            core::mem::size_of::<T>() == 0 || self.capacity() >= self.len() + slice.len()
1852        );
1853        debug_assert!(
1854            // is_zst::<T>() ==> capacity >= slen + slice.len()
1855            core::mem::size_of::<T>() != 0
1856            // Capacity is usize::MAX for ZSTs
1857            || self.len() <= usize::MAX - slice.len()
1858        );
1859
1860        let mut pos = 0usize;
1861
1862        loop
1863        /*
1864        invariants
1865            pos <= slice.len(),
1866            self.len() + (slice.len() - pos) <= old(self).capacity(),
1867            old(self).capacity() == self.capacity(),
1868
1869            self.len() == old(self).len() + pos,
1870
1871            forall|i: usize| implies(
1872                i < old(self).len(),
1873                self.get_unchecked(i) == old(self).get_unchecked(i)
1874            ),
1875            forall|i: usize| implies(
1876                i < pos,
1877                self.get_unchecked((old(self).len() + i) as usize)
1878                    == clone(slice.get_unchecked(i))
1879            )
1880        */
1881        {
1882            if pos == slice.len() {
1883                /*
1884                pos = slice.len(),
1885                self.len() = old(self).len() + slice.len(),
1886
1887                forall|i: usize| i < slice.len() implies {
1888                    self.get_unchecked((old(self).len() + i) as usize)
1889                        == clone(slice.get_unchecked(i))
1890                }
1891                by {
1892                    i < pos
1893                }
1894                */
1895                return;
1896            }
1897
1898            /*
1899            pos < slice.len(),
1900            self.len() < self.capacity()
1901            */
1902
1903            let elem = slice.get_unchecked(pos);
1904            self.push_unchecked(elem.clone());
1905
1906            /*
1907            ghost prev_pos = pos
1908            */
1909
1910            pos = pos + 1;
1911
1912            /*
1913            forall|i: usize| i < pos implies {
1914                self.get_unchecked((old(self).len() + i) as usize)
1915                    == clone(slice.get_unchecked(i))
1916            }
1917            by {
1918                if i < pos - 1 {
1919                    // By invariant
1920                }
1921                else {
1922                    i == prev_pos
1923                }
1924            }
1925            */
1926        }
1927    }
1928
1929    /// Clones and appends all elements in a slice to the `Vec`.
1930    ///
1931    /// Iterates over the slice `other`, clones each element, and then appends
1932    /// it to this `Vec`. The `other` vector is traversed in-order.
1933    ///
1934    /// Note that this function is same as [`extend`] except that it is
1935    /// specialized to work with slices instead. If and when Rust gets
1936    /// specialization this function will likely be deprecated (but still
1937    /// available).
1938    ///
1939    /// # Examples
1940    ///
1941    /// ```
1942    /// use bumpalo::{Bump, collections::Vec};
1943    ///
1944    /// let b = Bump::new();
1945    ///
1946    /// let mut vec = bumpalo::vec![in &b; 1];
1947    /// vec.extend_from_slice(&[2, 3, 4]);
1948    /// assert_eq!(vec, [1, 2, 3, 4]);
1949    /// ```
1950    ///
1951    /// [`extend`]: #method.extend
1952    pub fn extend_from_slice(&mut self, other: &[T]) {
1953        let capacity = self.capacity();
1954
1955        /*
1956        Cannot underflow via invariant of the Vec (capacity >= length).
1957
1958        This also holds for ZSTs, as capacity is usize::MAX
1959        */
1960        let remaining_cap = capacity - self.len;
1961
1962        /*
1963            self.len() + other.len() <= self.capacity(),
1964                <==>
1965            other.len() <= self.capacity() - self.len()
1966        */
1967
1968        if other.len() > remaining_cap {
1969            /*
1970            Divergence from verified impl:
1971              Verified implementation's reserve is not the same
1972              as bumpalo's. Verified implementation reserves with
1973              respect to capacity, not length. Thus this is equivalent
1974              to the verified implementation's:
1975
1976                self.buf.reserve(other.len() - remaining_cap)
1977
1978            */
1979            self.reserve(other.len());
1980        }
1981
1982        /*
1983        self.capacity() >= self.len() + other.len()
1984        */
1985
1986        unsafe {
1987            self.extend_from_slice_unchecked(other);
1988        }
1989    }
1990}
1991
1992impl<'bump, T: 'bump + Copy> Vec<'bump, T> {
1993    /// Helper method to copy all of the items in `other` and append them to the end of `self`.
1994    ///
1995    /// SAFETY:
1996    ///   * The caller is responsible for:
1997    ///       * calling [`reserve`](Self::reserve) beforehand to guarantee that there is enough
1998    ///         capacity to store `other.len()` more items.
1999    ///       * guaranteeing that `self` and `other` do not overlap.
2000    unsafe fn extend_from_slice_copy_unchecked(&mut self, other: &[T]) {
2001        let old_len = self.len();
2002        debug_assert!(old_len + other.len() <= self.capacity());
2003
2004        // SAFETY:
2005        // * `src` is valid for reads of `other.len()` values by virtue of being a `&[T]`.
2006        // * `dst` is valid for writes of `other.len()` bytes because the caller of this
2007        //   method is required to `reserve` capacity to store at least `other.len()` items
2008        //   beforehand.
2009        // * Because `src` is a `&[T]` and dst is a `&[T]` within the `Vec<T>`,
2010        //   `copy_nonoverlapping`'s alignment requirements are met.
2011        // * Caller is required to guarantee that the source and destination ranges cannot overlap
2012        unsafe {
2013            let src = other.as_ptr();
2014            let dst = self.as_mut_ptr().add(old_len);
2015            ptr::copy_nonoverlapping(src, dst, other.len());
2016            self.set_len(old_len + other.len());
2017        }
2018    }
2019
2020    /// Copies all elements in the slice `other` and appends them to the `Vec`.
2021    ///
2022    /// Note that this function is same as [`extend_from_slice`] except that it is optimized for
2023    /// slices of types that implement the `Copy` trait. If and when Rust gets specialization
2024    /// this function will likely be deprecated (but still available).
2025    ///
2026    /// To copy and append the data from multiple source slices at once, see
2027    /// [`extend_from_slices_copy`].
2028    ///
2029    /// # Examples
2030    ///
2031    /// ```
2032    /// use bumpalo::{Bump, collections::Vec};
2033    ///
2034    /// let b = Bump::new();
2035    ///
2036    /// let mut vec = bumpalo::vec![in &b; 1];
2037    /// vec.extend_from_slice_copy(&[2, 3, 4]);
2038    /// assert_eq!(vec, [1, 2, 3, 4]);
2039    /// ```
2040    ///
2041    /// ```
2042    /// use bumpalo::{Bump, collections::Vec};
2043    ///
2044    /// let b = Bump::new();
2045    ///
2046    /// let mut vec = bumpalo::vec![in &b; 'H' as u8];
2047    /// vec.extend_from_slice_copy("ello, world!".as_bytes());
2048    /// assert_eq!(vec, "Hello, world!".as_bytes());
2049    /// ```
2050    ///
2051    /// [`extend_from_slice`]: #method.extend_from_slice
2052    /// [`extend_from_slices`]: #method.extend_from_slices
2053    pub fn extend_from_slice_copy(&mut self, other: &[T]) {
2054        // Reserve space in the Vec for the values to be added
2055        self.reserve(other.len());
2056
2057        // Copy values into the space that was just reserved
2058        // SAFETY:
2059        // * `self` has enough capacity to store `other.len()` more items as `self.reserve(other.len())`
2060        //   above guarantees that.
2061        // * Source and destination data ranges cannot overlap as we just reserved the destination
2062        //   range from the bump.
2063        unsafe {
2064            self.extend_from_slice_copy_unchecked(other);
2065        }
2066    }
2067
2068    /// For each slice in `slices`, copies all elements in the slice and appends them to the `Vec`.
2069    ///
2070    /// This method is equivalent to calling [`extend_from_slice_copy`] in a loop, but is able
2071    /// to precompute the total amount of space to reserve in advance. This reduces the potential
2072    /// maximum number of reallocations needed from one-per-slice to just one.
2073    ///
2074    /// # Examples
2075    ///
2076    /// ```
2077    /// use bumpalo::{Bump, collections::Vec};
2078    ///
2079    /// let b = Bump::new();
2080    ///
2081    /// let mut vec = bumpalo::vec![in &b; 1];
2082    /// vec.extend_from_slices_copy(&[&[2, 3], &[], &[4]]);
2083    /// assert_eq!(vec, [1, 2, 3, 4]);
2084    /// ```
2085    ///
2086    /// ```
2087    /// use bumpalo::{Bump, collections::Vec};
2088    ///
2089    /// let b = Bump::new();
2090    ///
2091    /// let mut vec = bumpalo::vec![in &b; 'H' as u8];
2092    /// vec.extend_from_slices_copy(&["ello,".as_bytes(), &[], " world!".as_bytes()]);
2093    /// assert_eq!(vec, "Hello, world!".as_bytes());
2094    /// ```
2095    ///
2096    /// [`extend_from_slice_copy`]: #method.extend_from_slice_copy
2097    pub fn extend_from_slices_copy(&mut self, slices: &[&[T]]) {
2098        // Reserve the total amount of capacity we'll need to safely append the aggregated contents
2099        // of each slice in `slices`.
2100        let capacity_to_reserve: usize = slices.iter().map(|slice| slice.len()).sum();
2101        self.reserve(capacity_to_reserve);
2102
2103        // SAFETY:
2104        // * `dst` is valid for writes of `capacity_to_reserve` items as
2105        //   `self.reserve(capacity_to_reserve)` above guarantees that.
2106        // * Source and destination ranges cannot overlap as we just reserved the destination
2107        //   range from the bump.
2108        unsafe {
2109            // Copy the contents of each slice onto the end of `self`
2110            slices.iter().for_each(|slice| {
2111                self.extend_from_slice_copy_unchecked(slice);
2112            });
2113        }
2114    }
2115}
2116
2117// This code generalises `extend_with_{element,default}`.
2118trait ExtendWith<T> {
2119    fn next(&mut self) -> T;
2120    fn last(self) -> T;
2121}
2122
2123struct ExtendElement<T>(T);
2124impl<T: Clone> ExtendWith<T> for ExtendElement<T> {
2125    fn next(&mut self) -> T {
2126        self.0.clone()
2127    }
2128    fn last(self) -> T {
2129        self.0
2130    }
2131}
2132
2133impl<'bump, T: 'bump> Vec<'bump, T> {
2134    /// Extend the vector by `n` values, using the given generator.
2135    fn extend_with<E: ExtendWith<T>>(&mut self, n: usize, mut value: E) {
2136        self.reserve(n);
2137
2138        unsafe {
2139            let mut ptr = self.as_mut_ptr().add(self.len());
2140            // Use SetLenOnDrop to work around bug where compiler
2141            // may not realize the store through `ptr` through self.set_len()
2142            // don't alias.
2143            let mut local_len = SetLenOnDrop::new(&mut self.len);
2144
2145            // Write all elements except the last one
2146            for _ in 1..n {
2147                ptr::write(ptr, value.next());
2148                ptr = ptr.offset(1);
2149                // Increment the length in every step in case next() panics
2150                local_len.increment_len(1);
2151            }
2152
2153            if n > 0 {
2154                // We can write the last element directly without cloning needlessly
2155                ptr::write(ptr, value.last());
2156                local_len.increment_len(1);
2157            }
2158
2159            // len set by scope guard
2160        }
2161    }
2162}
2163
2164// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
2165//
2166// The idea is: The length field in SetLenOnDrop is a local variable
2167// that the optimizer will see does not alias with any stores through the Vec's data
2168// pointer. This is a workaround for alias analysis issue #32155
2169struct SetLenOnDrop<'a> {
2170    len: &'a mut usize,
2171    local_len: usize,
2172}
2173
2174impl<'a> SetLenOnDrop<'a> {
2175    #[inline]
2176    fn new(len: &'a mut usize) -> Self {
2177        SetLenOnDrop {
2178            local_len: *len,
2179            len,
2180        }
2181    }
2182
2183    #[inline]
2184    fn increment_len(&mut self, increment: usize) {
2185        self.local_len += increment;
2186    }
2187
2188    #[inline]
2189    fn decrement_len(&mut self, decrement: usize) {
2190        self.local_len -= decrement;
2191    }
2192}
2193
2194impl<'a> Drop for SetLenOnDrop<'a> {
2195    #[inline]
2196    fn drop(&mut self) {
2197        *self.len = self.local_len;
2198    }
2199}
2200
2201impl<'bump, T: 'bump + PartialEq> Vec<'bump, T> {
2202    /// Removes consecutive repeated elements in the vector according to the
2203    /// [`PartialEq`] trait implementation.
2204    ///
2205    /// If the vector is sorted, this removes all duplicates.
2206    ///
2207    /// # Examples
2208    ///
2209    /// ```
2210    /// use bumpalo::{Bump, collections::Vec};
2211    ///
2212    /// let b = Bump::new();
2213    ///
2214    /// let mut vec = bumpalo::vec![in &b; 1, 2, 2, 3, 2];
2215    ///
2216    /// vec.dedup();
2217    ///
2218    /// assert_eq!(vec, [1, 2, 3, 2]);
2219    /// ```
2220    #[inline]
2221    pub fn dedup(&mut self) {
2222        self.dedup_by(|a, b| a == b)
2223    }
2224}
2225
2226////////////////////////////////////////////////////////////////////////////////
2227// Common trait implementations for Vec
2228////////////////////////////////////////////////////////////////////////////////
2229
2230impl<'bump, T: 'bump + Clone> Clone for Vec<'bump, T> {
2231    #[cfg(not(test))]
2232    fn clone(&self) -> Vec<'bump, T> {
2233        let mut v = Vec::with_capacity_in(self.len(), self.buf.bump());
2234        v.extend(self.iter().cloned());
2235        v
2236    }
2237
2238    // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
2239    // required for this method definition, is not available. Instead use the
2240    // `slice::to_vec`  function which is only available with cfg(test)
2241    // NB see the slice::hack module in slice.rs for more information
2242    #[cfg(test)]
2243    fn clone(&self) -> Vec<'bump, T> {
2244        let mut v = Vec::new_in(self.buf.bump());
2245        v.extend(self.iter().cloned());
2246        v
2247    }
2248}
2249
2250impl<'bump, T: 'bump + Hash> Hash for Vec<'bump, T> {
2251    #[inline]
2252    fn hash<H: hash::Hasher>(&self, state: &mut H) {
2253        Hash::hash(&**self, state)
2254    }
2255}
2256
2257impl<'bump, T, I> Index<I> for Vec<'bump, T>
2258where
2259    I: ::core::slice::SliceIndex<[T]>,
2260{
2261    type Output = I::Output;
2262
2263    #[inline]
2264    fn index(&self, index: I) -> &Self::Output {
2265        Index::index(&**self, index)
2266    }
2267}
2268
2269impl<'bump, T, I> IndexMut<I> for Vec<'bump, T>
2270where
2271    I: ::core::slice::SliceIndex<[T]>,
2272{
2273    #[inline]
2274    fn index_mut(&mut self, index: I) -> &mut Self::Output {
2275        IndexMut::index_mut(&mut **self, index)
2276    }
2277}
2278
2279impl<'bump, T: 'bump> ops::Deref for Vec<'bump, T> {
2280    type Target = [T];
2281
2282    fn deref(&self) -> &[T] {
2283        unsafe {
2284            let p = self.buf.ptr();
2285            // assume(!p.is_null());
2286            slice::from_raw_parts(p, self.len)
2287        }
2288    }
2289}
2290
2291impl<'bump, T: 'bump> ops::DerefMut for Vec<'bump, T> {
2292    fn deref_mut(&mut self) -> &mut [T] {
2293        unsafe {
2294            let ptr = self.buf.ptr();
2295            // assume(!ptr.is_null());
2296            slice::from_raw_parts_mut(ptr, self.len)
2297        }
2298    }
2299}
2300
2301impl<'bump, T: 'bump> IntoIterator for Vec<'bump, T> {
2302    type Item = T;
2303    type IntoIter = IntoIter<'bump, T>;
2304
2305    /// Creates a consuming iterator, that is, one that moves each value out of
2306    /// the vector (from start to end). The vector cannot be used after calling
2307    /// this.
2308    ///
2309    /// # Examples
2310    ///
2311    /// ```
2312    /// use bumpalo::{Bump, collections::Vec};
2313    ///
2314    /// let b = Bump::new();
2315    ///
2316    /// let v = bumpalo::vec![in &b; "a".to_string(), "b".to_string()];
2317    /// for s in v.into_iter() {
2318    ///     // s has type String, not &String
2319    ///     println!("{}", s);
2320    /// }
2321    /// ```
2322    #[inline]
2323    fn into_iter(mut self) -> IntoIter<'bump, T> {
2324        unsafe {
2325            let begin = self.as_mut_ptr();
2326            // assume(!begin.is_null());
2327            let end = if mem::size_of::<T>() == 0 {
2328                arith_offset(begin as *const i8, self.len() as isize) as *const T
2329            } else {
2330                begin.add(self.len()) as *const T
2331            };
2332            mem::forget(self);
2333            IntoIter {
2334                phantom: PhantomData,
2335                ptr: begin,
2336                end,
2337            }
2338        }
2339    }
2340}
2341
2342impl<'a, 'bump, T> IntoIterator for &'a Vec<'bump, T> {
2343    type Item = &'a T;
2344    type IntoIter = slice::Iter<'a, T>;
2345
2346    fn into_iter(self) -> slice::Iter<'a, T> {
2347        self.iter()
2348    }
2349}
2350
2351impl<'a, 'bump, T> IntoIterator for &'a mut Vec<'bump, T> {
2352    type Item = &'a mut T;
2353    type IntoIter = slice::IterMut<'a, T>;
2354
2355    fn into_iter(self) -> slice::IterMut<'a, T> {
2356        self.iter_mut()
2357    }
2358}
2359
2360impl<'bump, T: 'bump> Extend<T> for Vec<'bump, T> {
2361    #[inline]
2362    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
2363        let iter = iter.into_iter();
2364        self.reserve(iter.size_hint().0);
2365
2366        for t in iter {
2367            self.push(t);
2368        }
2369    }
2370}
2371
2372impl<'bump, T: 'bump> Vec<'bump, T> {
2373    /// Creates a splicing iterator that replaces the specified range in the vector
2374    /// with the given `replace_with` iterator and yields the removed items.
2375    /// `replace_with` does not need to be the same length as `range`.
2376    ///
2377    /// Note 1: The element range is removed even if the iterator is not
2378    /// consumed until the end.
2379    ///
2380    /// Note 2: It is unspecified how many elements are removed from the vector,
2381    /// if the `Splice` value is leaked.
2382    ///
2383    /// Note 3: The input iterator `replace_with` is only consumed
2384    /// when the `Splice` value is dropped.
2385    ///
2386    /// Note 4: This is optimal if:
2387    ///
2388    /// * The tail (elements in the vector after `range`) is empty,
2389    /// * or `replace_with` yields fewer elements than `range`’s length
2390    /// * or the lower bound of its `size_hint()` is exact.
2391    ///
2392    /// Otherwise, a temporary vector is allocated and the tail is moved twice.
2393    ///
2394    /// # Panics
2395    ///
2396    /// Panics if the starting point is greater than the end point or if
2397    /// the end point is greater than the length of the vector.
2398    ///
2399    /// # Examples
2400    ///
2401    /// ```
2402    /// use bumpalo::{Bump, collections::Vec};
2403    ///
2404    /// let b = Bump::new();
2405    ///
2406    /// let mut v = bumpalo::vec![in &b; 1, 2, 3];
2407    /// let new = [7, 8];
2408    /// let u: Vec<_> = Vec::from_iter_in(v.splice(..2, new.iter().cloned()), &b);
2409    /// assert_eq!(v, &[7, 8, 3]);
2410    /// assert_eq!(u, &[1, 2]);
2411    /// ```
2412    #[inline]
2413    pub fn splice<'a, R, I>(
2414        &'a mut self,
2415        range: R,
2416        replace_with: I,
2417    ) -> Splice<'a, 'bump, I::IntoIter>
2418    where
2419        R: RangeBounds<usize>,
2420        I: IntoIterator<Item = T>,
2421    {
2422        Splice {
2423            drain: self.drain(range),
2424            replace_with: replace_with.into_iter(),
2425        }
2426    }
2427}
2428
2429/// Extend implementation that copies elements out of references before pushing them onto the Vec.
2430///
2431/// This implementation is specialized for slice iterators, where it uses [`copy_from_slice`] to
2432/// append the entire slice at once.
2433///
2434/// [`copy_from_slice`]: https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice
2435impl<'a, 'bump, T: 'a + Copy> Extend<&'a T> for Vec<'bump, T> {
2436    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
2437        self.extend(iter.into_iter().cloned())
2438    }
2439}
2440
2441macro_rules! __impl_slice_eq1 {
2442    ($Lhs: ty, $Rhs: ty) => {
2443        __impl_slice_eq1! { $Lhs, $Rhs, Sized }
2444    };
2445    ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
2446        impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs
2447        where
2448            A: PartialEq<B>,
2449        {
2450            #[inline]
2451            fn eq(&self, other: &$Rhs) -> bool {
2452                self[..] == other[..]
2453            }
2454        }
2455    };
2456}
2457
2458__impl_slice_eq1! { Vec<'a, A>, Vec<'b, B> }
2459__impl_slice_eq1! { Vec<'a, A>, &'b [B] }
2460__impl_slice_eq1! { Vec<'a, A>, &'b mut [B] }
2461// __impl_slice_eq1! { Cow<'a, [A]>, Vec<'b, B>, Clone }
2462
2463macro_rules! __impl_slice_eq1_array {
2464    ($Lhs: ty, $Rhs: ty) => {
2465        impl<'a, 'b, A, B, const N: usize> PartialEq<$Rhs> for $Lhs
2466        where
2467            A: PartialEq<B>,
2468        {
2469            #[inline]
2470            fn eq(&self, other: &$Rhs) -> bool {
2471                self[..] == other[..]
2472            }
2473        }
2474    };
2475}
2476
2477__impl_slice_eq1_array! { Vec<'a, A>, [B; N] }
2478__impl_slice_eq1_array! { Vec<'a, A>, &'b [B; N] }
2479__impl_slice_eq1_array! { Vec<'a, A>, &'b mut [B; N] }
2480
2481/// Implements comparison of vectors, lexicographically.
2482impl<'bump, T: 'bump + PartialOrd> PartialOrd for Vec<'bump, T> {
2483    #[inline]
2484    fn partial_cmp(&self, other: &Vec<'bump, T>) -> Option<Ordering> {
2485        PartialOrd::partial_cmp(&**self, &**other)
2486    }
2487}
2488
2489impl<'bump, T: 'bump + Eq> Eq for Vec<'bump, T> {}
2490
2491/// Implements ordering of vectors, lexicographically.
2492impl<'bump, T: 'bump + Ord> Ord for Vec<'bump, T> {
2493    #[inline]
2494    fn cmp(&self, other: &Vec<'bump, T>) -> Ordering {
2495        Ord::cmp(&**self, &**other)
2496    }
2497}
2498
2499impl<'bump, T: 'bump + fmt::Debug> fmt::Debug for Vec<'bump, T> {
2500    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2501        fmt::Debug::fmt(&**self, f)
2502    }
2503}
2504
2505impl<'bump, T: 'bump> AsRef<Vec<'bump, T>> for Vec<'bump, T> {
2506    fn as_ref(&self) -> &Vec<'bump, T> {
2507        self
2508    }
2509}
2510
2511impl<'bump, T: 'bump> AsMut<Vec<'bump, T>> for Vec<'bump, T> {
2512    fn as_mut(&mut self) -> &mut Vec<'bump, T> {
2513        self
2514    }
2515}
2516
2517impl<'bump, T: 'bump> AsRef<[T]> for Vec<'bump, T> {
2518    fn as_ref(&self) -> &[T] {
2519        self
2520    }
2521}
2522
2523impl<'bump, T: 'bump> AsMut<[T]> for Vec<'bump, T> {
2524    fn as_mut(&mut self) -> &mut [T] {
2525        self
2526    }
2527}
2528
2529#[cfg(feature = "boxed")]
2530impl<'bump, T: 'bump> From<Vec<'bump, T>> for crate::boxed::Box<'bump, [T]> {
2531    fn from(v: Vec<'bump, T>) -> crate::boxed::Box<'bump, [T]> {
2532        v.into_boxed_slice()
2533    }
2534}
2535
2536impl<'bump, T: 'bump> Borrow<[T]> for Vec<'bump, T> {
2537    #[inline]
2538    fn borrow(&self) -> &[T] {
2539        &self[..]
2540    }
2541}
2542
2543impl<'bump, T: 'bump> BorrowMut<[T]> for Vec<'bump, T> {
2544    #[inline]
2545    fn borrow_mut(&mut self) -> &mut [T] {
2546        &mut self[..]
2547    }
2548}
2549
2550impl<'bump, T> Drop for Vec<'bump, T> {
2551    fn drop(&mut self) {
2552        unsafe {
2553            // use drop for [T]
2554            // use a raw slice to refer to the elements of the vector as weakest necessary type;
2555            // could avoid questions of validity in certain cases
2556            ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
2557        }
2558        // RawVec handles deallocation
2559    }
2560}
2561
2562////////////////////////////////////////////////////////////////////////////////
2563// Clone-on-write
2564////////////////////////////////////////////////////////////////////////////////
2565
2566// impl<'a, 'bump, T: Clone> From<Vec<'bump, T>> for Cow<'a, [T]> {
2567//     fn from(v: Vec<'bump, T>) -> Cow<'a, [T]> {
2568//         Cow::Owned(v)
2569//     }
2570// }
2571
2572// impl<'a, 'bump, T: Clone> From<&'a Vec<'bump, T>> for Cow<'a, [T]> {
2573//     fn from(v: &'a Vec<'bump, T>) -> Cow<'a, [T]> {
2574//         Cow::Borrowed(v.as_slice())
2575//     }
2576// }
2577
2578////////////////////////////////////////////////////////////////////////////////
2579// Iterators
2580////////////////////////////////////////////////////////////////////////////////
2581
2582/// An iterator that moves out of a vector.
2583///
2584/// This `struct` is created by the [`Vec::into_iter`] method
2585/// (provided by the [`IntoIterator`] trait).
2586///
2587/// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
2588pub struct IntoIter<'bump, T> {
2589    phantom: PhantomData<&'bump [T]>,
2590    ptr: *const T,
2591    end: *const T,
2592}
2593
2594impl<'bump, T: fmt::Debug> fmt::Debug for IntoIter<'bump, T> {
2595    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2596        f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
2597    }
2598}
2599
2600impl<'bump, T: 'bump> IntoIter<'bump, T> {
2601    /// Returns the remaining items of this iterator as a slice.
2602    ///
2603    /// # Examples
2604    ///
2605    /// ```
2606    /// use bumpalo::{Bump, collections::Vec};
2607    ///
2608    /// let b = Bump::new();
2609    ///
2610    /// let vec = bumpalo::vec![in &b; 'a', 'b', 'c'];
2611    /// let mut into_iter = vec.into_iter();
2612    /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
2613    /// let _ = into_iter.next().unwrap();
2614    /// assert_eq!(into_iter.as_slice(), &['b', 'c']);
2615    /// ```
2616    pub fn as_slice(&self) -> &[T] {
2617        unsafe { slice::from_raw_parts(self.ptr, self.len()) }
2618    }
2619
2620    /// Returns the remaining items of this iterator as a mutable slice.
2621    ///
2622    /// # Examples
2623    ///
2624    /// ```
2625    /// use bumpalo::{Bump, collections::Vec};
2626    ///
2627    /// let b = Bump::new();
2628    ///
2629    /// let vec = bumpalo::vec![in &b; 'a', 'b', 'c'];
2630    /// let mut into_iter = vec.into_iter();
2631    /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
2632    /// into_iter.as_mut_slice()[2] = 'z';
2633    /// assert_eq!(into_iter.next().unwrap(), 'a');
2634    /// assert_eq!(into_iter.next().unwrap(), 'b');
2635    /// assert_eq!(into_iter.next().unwrap(), 'z');
2636    /// ```
2637    pub fn as_mut_slice(&mut self) -> &mut [T] {
2638        unsafe { slice::from_raw_parts_mut(self.ptr as *mut T, self.len()) }
2639    }
2640}
2641
2642unsafe impl<'bump, T: Send> Send for IntoIter<'bump, T> {}
2643unsafe impl<'bump, T: Sync> Sync for IntoIter<'bump, T> {}
2644
2645impl<'bump, T: 'bump> Iterator for IntoIter<'bump, T> {
2646    type Item = T;
2647
2648    #[inline]
2649    fn next(&mut self) -> Option<T> {
2650        unsafe {
2651            if self.ptr as *const _ == self.end {
2652                None
2653            } else if mem::size_of::<T>() == 0 {
2654                // purposefully don't use 'ptr.offset' because for
2655                // vectors with 0-size elements this would return the
2656                // same pointer.
2657                self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;
2658
2659                // Make up a value of this ZST.
2660                Some(mem::zeroed())
2661            } else {
2662                let old = self.ptr;
2663                self.ptr = self.ptr.offset(1);
2664
2665                Some(ptr::read(old))
2666            }
2667        }
2668    }
2669
2670    #[inline]
2671    fn size_hint(&self) -> (usize, Option<usize>) {
2672        let exact = if mem::size_of::<T>() == 0 {
2673            (self.end as usize).wrapping_sub(self.ptr as usize)
2674        } else {
2675            unsafe { offset_from(self.end, self.ptr) as usize }
2676        };
2677        (exact, Some(exact))
2678    }
2679
2680    #[inline]
2681    fn count(self) -> usize {
2682        self.len()
2683    }
2684}
2685
2686impl<'bump, T: 'bump> DoubleEndedIterator for IntoIter<'bump, T> {
2687    #[inline]
2688    fn next_back(&mut self) -> Option<T> {
2689        unsafe {
2690            if self.end == self.ptr {
2691                None
2692            } else if mem::size_of::<T>() == 0 {
2693                // See above for why 'ptr.offset' isn't used
2694                self.end = arith_offset(self.end as *const i8, -1) as *mut T;
2695
2696                // Make up a value of this ZST.
2697                Some(mem::zeroed())
2698            } else {
2699                self.end = self.end.offset(-1);
2700
2701                Some(ptr::read(self.end))
2702            }
2703        }
2704    }
2705}
2706
2707impl<'bump, T: 'bump> ExactSizeIterator for IntoIter<'bump, T> {}
2708
2709impl<'bump, T: 'bump> FusedIterator for IntoIter<'bump, T> {}
2710
2711impl<'bump, T> Drop for IntoIter<'bump, T> {
2712    fn drop(&mut self) {
2713        // drop all remaining elements
2714        self.for_each(drop);
2715    }
2716}
2717
2718/// A draining iterator for `Vec<'bump, T>`.
2719///
2720/// This `struct` is created by the [`Vec::drain`] method.
2721pub struct Drain<'a, 'bump, T: 'a + 'bump> {
2722    /// Index of tail to preserve
2723    tail_start: usize,
2724    /// Length of tail
2725    tail_len: usize,
2726    /// Current remaining range to remove
2727    iter: slice::Iter<'a, T>,
2728    vec: NonNull<Vec<'bump, T>>,
2729}
2730
2731impl<'a, 'bump, T: 'a + 'bump + fmt::Debug> fmt::Debug for Drain<'a, 'bump, T> {
2732    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2733        f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
2734    }
2735}
2736
2737unsafe impl<'a, 'bump, T: Sync> Sync for Drain<'a, 'bump, T> {}
2738unsafe impl<'a, 'bump, T: Send> Send for Drain<'a, 'bump, T> {}
2739
2740impl<'a, 'bump, T> Iterator for Drain<'a, 'bump, T> {
2741    type Item = T;
2742
2743    #[inline]
2744    fn next(&mut self) -> Option<T> {
2745        self.iter
2746            .next()
2747            .map(|elt| unsafe { ptr::read(elt as *const _) })
2748    }
2749
2750    fn size_hint(&self) -> (usize, Option<usize>) {
2751        self.iter.size_hint()
2752    }
2753}
2754
2755impl<'a, 'bump, T> DoubleEndedIterator for Drain<'a, 'bump, T> {
2756    #[inline]
2757    fn next_back(&mut self) -> Option<T> {
2758        self.iter
2759            .next_back()
2760            .map(|elt| unsafe { ptr::read(elt as *const _) })
2761    }
2762}
2763
2764impl<'a, 'bump, T> Drop for Drain<'a, 'bump, T> {
2765    fn drop(&mut self) {
2766        // exhaust self first
2767        self.for_each(drop);
2768
2769        if self.tail_len > 0 {
2770            unsafe {
2771                let source_vec = self.vec.as_mut();
2772                // memmove back untouched tail, update to new length
2773                let start = source_vec.len();
2774                let tail = self.tail_start;
2775                if tail != start {
2776                    let src = source_vec.as_ptr().add(tail);
2777                    let dst = source_vec.as_mut_ptr().add(start);
2778                    ptr::copy(src, dst, self.tail_len);
2779                }
2780                source_vec.set_len(start + self.tail_len);
2781            }
2782        }
2783    }
2784}
2785
2786impl<'a, 'bump, T> ExactSizeIterator for Drain<'a, 'bump, T> {}
2787
2788impl<'a, 'bump, T> FusedIterator for Drain<'a, 'bump, T> {}
2789
2790/// A splicing iterator for `Vec`.
2791///
2792/// This struct is created by the [`Vec::splice`] method. See its
2793/// documentation for more information.
2794#[derive(Debug)]
2795pub struct Splice<'a, 'bump, I>
2796where
2797    I: Iterator,
2798    I::Item: 'a + 'bump,
2799{
2800    drain: Drain<'a, 'bump, I::Item>,
2801    replace_with: I,
2802}
2803
2804impl<'a, 'bump, I: Iterator> Iterator for Splice<'a, 'bump, I> {
2805    type Item = I::Item;
2806
2807    fn next(&mut self) -> Option<Self::Item> {
2808        self.drain.next()
2809    }
2810
2811    fn size_hint(&self) -> (usize, Option<usize>) {
2812        self.drain.size_hint()
2813    }
2814}
2815
2816impl<'a, 'bump, I: Iterator> DoubleEndedIterator for Splice<'a, 'bump, I> {
2817    fn next_back(&mut self) -> Option<Self::Item> {
2818        self.drain.next_back()
2819    }
2820}
2821
2822impl<'a, 'bump, I: Iterator> ExactSizeIterator for Splice<'a, 'bump, I> {}
2823
2824impl<'a, 'bump, I: Iterator> Drop for Splice<'a, 'bump, I> {
2825    fn drop(&mut self) {
2826        self.drain.by_ref().for_each(drop);
2827
2828        unsafe {
2829            if self.drain.tail_len == 0 {
2830                self.drain.vec.as_mut().extend(self.replace_with.by_ref());
2831                return;
2832            }
2833
2834            // First fill the range left by drain().
2835            if !self.drain.fill(&mut self.replace_with) {
2836                return;
2837            }
2838
2839            // There may be more elements. Use the lower bound as an estimate.
2840            // FIXME: Is the upper bound a better guess? Or something else?
2841            let (lower_bound, _upper_bound) = self.replace_with.size_hint();
2842            if lower_bound > 0 {
2843                self.drain.move_tail(lower_bound);
2844                if !self.drain.fill(&mut self.replace_with) {
2845                    return;
2846                }
2847            }
2848
2849            // Collect any remaining elements.
2850            // This is a zero-length vector which does not allocate if `lower_bound` was exact.
2851            let mut collected = Vec::new_in(self.drain.vec.as_ref().buf.bump());
2852            collected.extend(self.replace_with.by_ref());
2853            let mut collected = collected.into_iter();
2854            // Now we have an exact count.
2855            if collected.len() > 0 {
2856                self.drain.move_tail(collected.len());
2857                let filled = self.drain.fill(&mut collected);
2858                debug_assert!(filled);
2859                debug_assert_eq!(collected.len(), 0);
2860            }
2861        }
2862        // Let `Drain::drop` move the tail back if necessary and restore `vec.len`.
2863    }
2864}
2865
2866/// Private helper methods for `Splice::drop`
2867impl<'a, 'bump, T> Drain<'a, 'bump, T> {
2868    /// The range from `self.vec.len` to `self.tail_start` contains elements
2869    /// that have been moved out.
2870    /// Fill that range as much as possible with new elements from the `replace_with` iterator.
2871    /// Return whether we filled the entire range. (`replace_with.next()` didn’t return `None`.)
2872    unsafe fn fill<I: Iterator<Item = T>>(&mut self, replace_with: &mut I) -> bool {
2873        let vec = self.vec.as_mut();
2874        let range_start = vec.len;
2875        let range_end = self.tail_start;
2876        let range_slice =
2877            slice::from_raw_parts_mut(vec.as_mut_ptr().add(range_start), range_end - range_start);
2878
2879        for place in range_slice {
2880            if let Some(new_item) = replace_with.next() {
2881                ptr::write(place, new_item);
2882                vec.len += 1;
2883            } else {
2884                return false;
2885            }
2886        }
2887        true
2888    }
2889
2890    /// Make room for inserting more elements before the tail.
2891    unsafe fn move_tail(&mut self, extra_capacity: usize) {
2892        let vec = self.vec.as_mut();
2893        let used_capacity = self.tail_start + self.tail_len;
2894        vec.buf.reserve(used_capacity, extra_capacity);
2895
2896        let new_tail_start = self.tail_start + extra_capacity;
2897        let src = vec.as_ptr().add(self.tail_start);
2898        let dst = vec.as_mut_ptr().add(new_tail_start);
2899        ptr::copy(src, dst, self.tail_len);
2900        self.tail_start = new_tail_start;
2901    }
2902}
2903
2904/// An iterator produced by calling [`Vec::drain_filter`].
2905#[derive(Debug)]
2906pub struct DrainFilter<'a, 'bump: 'a, T: 'a + 'bump, F>
2907where
2908    F: FnMut(&mut T) -> bool,
2909{
2910    vec: &'a mut Vec<'bump, T>,
2911    idx: usize,
2912    del: usize,
2913    old_len: usize,
2914    pred: F,
2915}
2916
2917impl<'a, 'bump, T, F> Iterator for DrainFilter<'a, 'bump, T, F>
2918where
2919    F: FnMut(&mut T) -> bool,
2920{
2921    type Item = T;
2922
2923    fn next(&mut self) -> Option<T> {
2924        unsafe {
2925            while self.idx != self.old_len {
2926                let i = self.idx;
2927                self.idx += 1;
2928                let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len);
2929                if (self.pred)(&mut v[i]) {
2930                    self.del += 1;
2931                    return Some(ptr::read(&v[i]));
2932                } else if self.del > 0 {
2933                    let del = self.del;
2934                    let src: *const T = &v[i];
2935                    let dst: *mut T = &mut v[i - del];
2936                    // This is safe because self.vec has length 0
2937                    // thus its elements will not have Drop::drop
2938                    // called on them in the event of a panic.
2939                    ptr::copy_nonoverlapping(src, dst, 1);
2940                }
2941            }
2942            None
2943        }
2944    }
2945
2946    fn size_hint(&self) -> (usize, Option<usize>) {
2947        (0, Some(self.old_len - self.idx))
2948    }
2949}
2950
2951impl<'a, 'bump, T, F> Drop for DrainFilter<'a, 'bump, T, F>
2952where
2953    F: FnMut(&mut T) -> bool,
2954{
2955    fn drop(&mut self) {
2956        self.for_each(drop);
2957        unsafe {
2958            self.vec.set_len(self.old_len - self.del);
2959        }
2960    }
2961}
2962
2963#[cfg(feature = "std")]
2964impl<'bump> io::Write for Vec<'bump, u8> {
2965    #[inline]
2966    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
2967        self.extend_from_slice_copy(buf);
2968        Ok(buf.len())
2969    }
2970
2971    #[inline]
2972    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
2973        self.extend_from_slice_copy(buf);
2974        Ok(())
2975    }
2976
2977    #[inline]
2978    fn flush(&mut self) -> io::Result<()> {
2979        Ok(())
2980    }
2981}
2982
2983#[cfg(feature = "serde")]
2984mod serialize {
2985    use super::*;
2986
2987    use serde::{ser::SerializeSeq, Serialize, Serializer};
2988
2989    impl<'a, T> Serialize for Vec<'a, T>
2990    where
2991        T: Serialize,
2992    {
2993        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2994        where
2995            S: Serializer,
2996        {
2997            let mut seq = serializer.serialize_seq(Some(self.len))?;
2998            for e in self.iter() {
2999                seq.serialize_element(e)?;
3000            }
3001            seq.end()
3002        }
3003    }
3004}