Skip to main content

bump_scope/
bump_vec.rs

1use core::{
2    alloc::Layout,
3    borrow::{Borrow, BorrowMut},
4    fmt::Debug,
5    hash::Hash,
6    iter,
7    marker::PhantomData,
8    mem::{ManuallyDrop, MaybeUninit},
9    ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
10    ptr::{self, NonNull},
11    slice::SliceIndex,
12};
13
14use crate::{
15    BumpBox, ErrorBehavior, FixedBumpVec, NoDrop, SizedTypeProperties,
16    alloc::AllocError,
17    destructure::destructure,
18    fixed_bump_vec::RawFixedBumpVec,
19    min_non_zero_cap,
20    owned_slice::{self, OwnedSlice, TakeOwnedSlice},
21    polyfill::{hint::likely, non_null, pointer, slice},
22    traits::{BumpAllocatorTyped, BumpAllocatorTypedScope},
23};
24
25#[cfg(feature = "panic-on-alloc")]
26use crate::panic_on_error;
27
28mod drain;
29mod into_iter;
30mod splice;
31
32pub use into_iter::IntoIter;
33
34#[cfg(feature = "panic-on-alloc")]
35pub(crate) use drain::Drain;
36
37#[cfg(feature = "panic-on-alloc")]
38pub use splice::Splice;
39
40/// Like [`vec!`](alloc_crate::vec!) but allocates inside a bump allocator, returning a [`BumpVec`].
41///
42/// `$bump` can be any type that implements [`BumpAllocatorTyped`].
43///
44/// # Panics
45/// If used without `try`, panics on allocation failure.
46///
47/// # Errors
48/// If used with `try`, errors on allocation failure.
49///
50/// # Examples
51///
52/// There are three forms of this macro:
53///
54/// - Create an empty [`BumpVec`]:
55/// ```
56/// # use bump_scope::{Bump, bump_vec, BumpVec};
57/// # let bump: Bump = Bump::new();
58/// let vec: BumpVec<i32, _> = bump_vec![in &bump];
59/// assert!(vec.is_empty());
60/// ```
61///
62/// - Create a [`BumpVec`] containing a given list of elements:
63///
64/// ```
65/// # use bump_scope::{Bump, bump_vec};
66/// # let bump: Bump = Bump::new();
67/// let vec = bump_vec![in &bump; 1, 2, 3];
68/// assert_eq!(vec[0], 1);
69/// assert_eq!(vec[1], 2);
70/// assert_eq!(vec[2], 3);
71/// ```
72///
73/// - Create a [`BumpVec`] from a given element and size:
74///
75/// ```
76/// # use bump_scope::{Bump, bump_vec};
77/// # let bump: Bump = Bump::new();
78/// let vec = bump_vec![in &bump; 1; 3];
79/// assert_eq!(vec, [1, 1, 1]);
80/// ```
81///
82/// Note that unlike array expressions this syntax supports all elements
83/// which implement [`Clone`] and the number of elements doesn't have to be
84/// a constant.
85///
86/// This will use `clone` to duplicate an expression, so one should be careful
87/// using this with types having a nonstandard `Clone` implementation. For
88/// example, `bump_vec![in &bump; Rc::new(1); 5]` will create a vector of five references
89/// to the same boxed integer value, not five references pointing to independently
90/// boxed integers.
91///
92/// Also, note that `bump_vec![in &bump; expr; 0]` is allowed, and produces an empty vector.
93/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
94/// be mindful of side effects.
95#[macro_export]
96macro_rules! bump_vec {
97    [in $bump:expr] => {
98        $crate::BumpVec::new_in($bump)
99    };
100    [in $bump:expr; $($values:expr),* $(,)?] => {
101        $crate::__bump_vec_panic_on_alloc![in $bump; $($values),*]
102    };
103    [in $bump:expr; $value:expr; $count:expr] => {
104        $crate::__bump_vec_panic_on_alloc![in $bump; $value; $count]
105    };
106    [try in $bump:expr] => {
107        Ok::<_, $crate::alloc::AllocError>($crate::BumpVec::new_in($bump))
108    };
109    [try in $bump:expr; $($values:expr),* $(,)?] => {
110        $crate::BumpVec::try_from_owned_slice_in([$($values),*], $bump)
111    };
112    [try in $bump:expr; $value:expr; $count:expr] => {
113        $crate::BumpVec::try_from_elem_in($value, $count, $bump)
114    };
115}
116
117#[doc(hidden)]
118#[macro_export]
119#[cfg(feature = "panic-on-alloc")]
120macro_rules! __bump_vec_panic_on_alloc {
121    [in $bump:expr; $($values:expr),* $(,)?] => {
122        $crate::BumpVec::from_owned_slice_in([$($values),*], $bump)
123    };
124    [in $bump:expr; $value:expr; $count:expr] => {
125        $crate::BumpVec::from_elem_in($value, $count, $bump)
126    };
127}
128
129#[doc(hidden)]
130#[macro_export]
131#[cfg(not(feature = "panic-on-alloc"))]
132macro_rules! __bump_vec_panic_on_alloc {
133    [in $bump:expr; $($values:expr),* $(,)?] => {
134        compile_error!(concat!(
135            "the potentially panicking api of `bump_vec!` is not available\n",
136            "help: enable `bump-scope`'s \"panic-on-alloc\" feature or use `try`:\n",
137            "      `bump_vec![try in ",
138            stringify!($bump),
139            "; ",
140            stringify!($($values),*),
141            "]`"
142        ))
143    };
144    [in $bump:expr; $value:expr; $count:expr] => {
145        compile_error!(concat!(
146            "the potentially panicking api of `bump_vec!` is not available\n",
147            "help: enable `bump-scope`'s \"panic-on-alloc\" feature or use `try`:\n",
148            "      `bump_vec![try in ",
149            stringify!($bump),
150            "; ",
151            stringify!($value),
152            "; ",
153            stringify!($count),
154            "]`"
155        ))
156    };
157}
158
159/// A bump allocated [`Vec`](alloc_crate::vec::Vec).
160///
161/// The main difference to `Vec` is that it can be turned into a slice that is live for this bump scope (`'a`).
162/// Such a slice can be live while entering new scopes.
163///
164/// This would not be possible with `Vec`:
165///
166/// ```
167/// # use bump_scope::{Bump, BumpVec};
168/// # let mut bump: Bump = Bump::new();
169/// let bump = bump.as_mut_scope();
170///
171/// let slice = {
172///     let mut vec = BumpVec::new_in(&*bump);
173///
174///     vec.push(1);
175///     vec.push(2);
176///     vec.push(3);
177///
178///     vec.into_slice()
179/// };
180///
181/// bump.scoped(|bump| {
182///     // allocate more things
183///     # _ = bump;
184/// });
185///
186/// assert_eq!(slice, [1, 2, 3]);
187/// ```
188///
189/// # Examples
190///
191/// This type can be used to allocate a slice, when `alloc_*` methods are too limiting:
192/// ```
193/// # use bump_scope::{Bump, BumpVec};
194/// # let bump: Bump = Bump::new();
195/// let mut vec = BumpVec::new_in(&bump);
196///
197/// vec.push(1);
198/// vec.push(2);
199/// vec.push(3);
200///
201/// let slice: &[i32] = vec.into_slice();
202///
203/// assert_eq!(slice, [1, 2, 3]);
204/// ```
205// `BumpString` and `BumpVec<u8>` have the same repr.
206#[repr(C)]
207pub struct BumpVec<T, A: BumpAllocatorTyped> {
208    fixed: RawFixedBumpVec<T>,
209    allocator: A,
210}
211
212impl<T, A: BumpAllocatorTyped> Deref for BumpVec<T, A> {
213    type Target = [T];
214
215    fn deref(&self) -> &Self::Target {
216        unsafe { self.fixed.cook_ref() }
217    }
218}
219
220impl<T, A: BumpAllocatorTyped> DerefMut for BumpVec<T, A> {
221    fn deref_mut(&mut self) -> &mut Self::Target {
222        unsafe { self.fixed.cook_mut() }
223    }
224}
225
226impl<T, A: BumpAllocatorTyped> BumpVec<T, A> {
227    /// # Safety
228    ///
229    /// Must only be called from the drop implementation and a call to this function
230    /// must be the only thing in that drop implementation.
231    #[inline]
232    unsafe fn drop_inner(&mut self) {
233        struct DropGuard<'a, T, A: BumpAllocatorTyped>(&'a mut BumpVec<T, A>);
234
235        impl<T, A: BumpAllocatorTyped> Drop for DropGuard<'_, T, A> {
236            fn drop(&mut self) {
237                if T::IS_ZST || self.0.capacity() == 0 {
238                    return;
239                }
240
241                unsafe {
242                    let ptr = self.0.as_non_null().cast();
243                    let layout = Layout::from_size_align_unchecked(self.0.capacity() * T::SIZE, T::ALIGN);
244                    self.0.allocator.deallocate(ptr, layout);
245                }
246            }
247        }
248
249        let guard = DropGuard(self);
250
251        // destroy the remaining elements
252        guard.0.clear();
253
254        // now `guard` will be dropped and deallocate the memory
255    }
256}
257
258#[cfg(feature = "nightly-dropck-eyepatch")]
259unsafe impl<#[may_dangle] T, A: BumpAllocatorTyped> Drop for BumpVec<T, A> {
260    #[inline]
261    fn drop(&mut self) {
262        unsafe { self.drop_inner() }
263    }
264}
265
266#[cfg(not(feature = "nightly-dropck-eyepatch"))]
267impl<T, A: BumpAllocatorTyped> Drop for BumpVec<T, A> {
268    #[inline]
269    fn drop(&mut self) {
270        unsafe { self.drop_inner() }
271    }
272}
273
274impl<T, A: BumpAllocatorTyped + Default> Default for BumpVec<T, A> {
275    fn default() -> Self {
276        Self::new_in(A::default())
277    }
278}
279
280#[cfg(feature = "panic-on-alloc")]
281impl<T: Clone, A: BumpAllocatorTyped + Clone> Clone for BumpVec<T, A> {
282    fn clone(&self) -> Self {
283        let allocator = self.allocator.clone();
284        let ptr = allocator.allocate_slice::<MaybeUninit<T>>(self.len());
285        let slice = NonNull::slice_from_raw_parts(ptr, self.len());
286        let boxed = unsafe { BumpBox::from_raw(slice) };
287        let boxed = boxed.init_clone(self);
288        let fixed = FixedBumpVec::from_init(boxed);
289        let fixed = unsafe { RawFixedBumpVec::from_cooked(fixed) };
290        Self { fixed, allocator }
291    }
292}
293
294impl<T, A: BumpAllocatorTyped> BumpVec<T, A> {
295    /// Constructs a new empty `BumpVec<T>`.
296    ///
297    /// The vector will not allocate until elements are pushed onto it.
298    ///
299    /// # Examples
300    /// ```
301    /// # use bump_scope::{Bump, BumpVec};
302    /// # let bump: Bump = Bump::new();
303    /// let vec = BumpVec::<i32, _>::new_in(&bump);
304    /// assert_eq!(vec.len(), 0);
305    /// assert_eq!(vec.capacity(), 0);
306    /// ```
307    #[inline]
308    pub const fn new_in(allocator: A) -> Self {
309        Self {
310            fixed: RawFixedBumpVec::EMPTY,
311            allocator,
312        }
313    }
314
315    /// Constructs a new empty vector with at least the specified capacity
316    /// in the provided bump allocator.
317    ///
318    /// The vector will be able to hold `capacity` elements without
319    /// reallocating. If `capacity` is 0, the vector will not allocate.
320    ///
321    /// It is important to note that although the returned vector has the
322    /// minimum *capacity* specified, the vector will have a zero *length*. For
323    /// an explanation of the difference between length and capacity, see
324    /// *[Capacity and reallocation]*.
325    ///
326    /// When `T` is a zero-sized type, there will be no allocation
327    /// and the capacity will always be `usize::MAX`.
328    ///
329    /// [Capacity and reallocation]: alloc_crate::vec::Vec#capacity-and-reallocation
330    ///
331    /// # Panics
332    /// Panics if the allocation fails.
333    ///
334    /// # Examples
335    /// ```
336    /// # use bump_scope::{Bump, BumpVec};
337    /// # let bump: Bump = Bump::new();
338    /// let mut vec = BumpVec::<i32, _>::with_capacity_in(10, &bump);
339    ///
340    /// // The vector contains no items, even though it has capacity for more
341    /// assert_eq!(vec.len(), 0);
342    /// assert!(vec.capacity() >= 10);
343    ///
344    /// // These are all done without reallocating...
345    /// for i in 0..10 {
346    ///     vec.push(i);
347    /// }
348    /// assert_eq!(vec.len(), 10);
349    /// assert!(vec.capacity() >= 10);
350    ///
351    /// // ...but this may make the vector reallocate
352    /// vec.push(11);
353    /// assert_eq!(vec.len(), 11);
354    /// assert!(vec.capacity() >= 11);
355    ///
356    /// // A vector of a zero-sized type will always over-allocate, since no
357    /// // allocation is necessary
358    /// let vec_units = BumpVec::<(), _>::with_capacity_in(10, &bump);
359    /// assert_eq!(vec_units.capacity(), usize::MAX);
360    /// ```
361    #[must_use]
362    #[inline(always)]
363    #[cfg(feature = "panic-on-alloc")]
364    pub fn with_capacity_in(capacity: usize, allocator: A) -> Self {
365        panic_on_error(Self::generic_with_capacity_in(capacity, allocator))
366    }
367
368    /// Constructs a new empty vector with at least the specified capacity
369    /// in the provided bump allocator.
370    ///
371    /// The vector will be able to hold `capacity` elements without
372    /// reallocating. If `capacity` is 0, the vector will not allocate.
373    ///
374    /// It is important to note that although the returned vector has the
375    /// minimum *capacity* specified, the vector will have a zero *length*. For
376    /// an explanation of the difference between length and capacity, see
377    /// *[Capacity and reallocation]*.
378    ///
379    /// When `T` is a zero-sized type, there will be no allocation
380    /// and the capacity will always be `usize::MAX`.
381    ///
382    /// [Capacity and reallocation]: alloc_crate::vec::Vec#capacity-and-reallocation
383    ///
384    /// # Errors
385    /// Errors if the allocation fails.
386    ///
387    /// # Examples
388    /// ```
389    /// # use bump_scope::{Bump, BumpVec};
390    /// # let bump: Bump = Bump::new();
391    /// let mut vec = BumpVec::<i32, _>::try_with_capacity_in(10, &bump)?;
392    ///
393    /// // The vector contains no items, even though it has capacity for more
394    /// assert_eq!(vec.len(), 0);
395    /// assert!(vec.capacity() >= 10);
396    ///
397    /// // These are all done without reallocating...
398    /// for i in 0..10 {
399    ///     vec.push(i);
400    /// }
401    /// assert_eq!(vec.len(), 10);
402    /// assert!(vec.capacity() >= 10);
403    ///
404    /// // ...but this may make the vector reallocate
405    /// vec.push(11);
406    /// assert_eq!(vec.len(), 11);
407    /// assert!(vec.capacity() >= 11);
408    ///
409    /// // A vector of a zero-sized type will always over-allocate, since no
410    /// // allocation is necessary
411    /// let vec_units = BumpVec::<(), _>::try_with_capacity_in(10, &bump)?;
412    /// assert_eq!(vec_units.capacity(), usize::MAX);
413    /// # Ok::<(), bump_scope::alloc::AllocError>(())
414    /// ```
415    #[inline(always)]
416    pub fn try_with_capacity_in(capacity: usize, allocator: A) -> Result<Self, AllocError> {
417        Self::generic_with_capacity_in(capacity, allocator)
418    }
419
420    #[inline]
421    pub(crate) fn generic_with_capacity_in<E: ErrorBehavior>(capacity: usize, allocator: A) -> Result<Self, E> {
422        if T::IS_ZST || capacity == 0 {
423            return Ok(Self {
424                fixed: RawFixedBumpVec::EMPTY,
425                allocator,
426            });
427        }
428
429        Ok(Self {
430            fixed: unsafe { RawFixedBumpVec::allocate(&allocator, capacity)? },
431            allocator,
432        })
433    }
434
435    /// Constructs a new `BumpVec<T>` and pushes `value` `count` times.
436    ///
437    /// # Panics
438    /// Panics if the allocation fails.
439    ///
440    /// # Examples
441    /// ```
442    /// # use bump_scope::{Bump, BumpVec};
443    /// # let bump: Bump = Bump::new();
444    /// let vec = BumpVec::from_elem_in("ho", 3, &bump);
445    /// assert_eq!(vec, ["ho", "ho", "ho"]);
446    /// ```
447    #[must_use]
448    #[inline(always)]
449    #[cfg(feature = "panic-on-alloc")]
450    pub fn from_elem_in(value: T, count: usize, allocator: A) -> Self
451    where
452        T: Clone,
453    {
454        panic_on_error(Self::generic_from_elem_in(value, count, allocator))
455    }
456
457    /// Constructs a new `BumpVec<T>` and pushes `value` `count` times.
458    ///
459    /// # Errors
460    /// Errors if the allocation fails.
461    ///
462    /// # Examples
463    /// ```
464    /// # use bump_scope::{Bump, BumpVec};
465    /// # let bump: Bump = Bump::new();
466    /// let vec = BumpVec::try_from_elem_in("ho", 3, &bump)?;
467    /// assert_eq!(vec, ["ho", "ho", "ho"]);
468    /// # Ok::<(), bump_scope::alloc::AllocError>(())
469    /// ```
470    #[inline(always)]
471    pub fn try_from_elem_in(value: T, count: usize, allocator: A) -> Result<Self, AllocError>
472    where
473        T: Clone,
474    {
475        Self::generic_from_elem_in(value, count, allocator)
476    }
477
478    #[inline]
479    pub(crate) fn generic_from_elem_in<E: ErrorBehavior>(value: T, count: usize, allocator: A) -> Result<Self, E>
480    where
481        T: Clone,
482    {
483        let mut vec = Self::generic_with_capacity_in(count, allocator)?;
484
485        unsafe {
486            if count != 0 {
487                for _ in 0..(count - 1) {
488                    vec.push_unchecked(value.clone());
489                }
490
491                vec.push_unchecked(value);
492            }
493        }
494
495        Ok(vec)
496    }
497
498    /// Constructs a new `BumpVec<T>` from an [`OwnedSlice`].
499    ///
500    /// # Panics
501    /// Panics if the allocation fails.
502    ///
503    /// # Examples
504    /// ```
505    /// # use bump_scope::{Bump, BumpVec};
506    /// # let bump: Bump = Bump::new();
507    /// // by value
508    /// let a = BumpVec::from_owned_slice_in([1, 2], &bump);
509    /// let b = BumpVec::from_owned_slice_in(vec![3, 4], &bump);
510    /// let c = BumpVec::from_owned_slice_in(bump.alloc_iter(5..=6), &bump);
511    ///
512    /// // by mutable reference
513    /// let mut other = vec![7, 8];
514    /// let d = BumpVec::from_owned_slice_in(&mut other, &bump);
515    /// assert!(other.is_empty());
516    ///
517    /// assert_eq!(a, [1, 2]);
518    /// assert_eq!(b, [3, 4]);
519    /// assert_eq!(c, [5, 6]);
520    /// assert_eq!(d, [7, 8]);
521    /// ```
522    #[must_use]
523    #[inline(always)]
524    #[cfg(feature = "panic-on-alloc")]
525    pub fn from_owned_slice_in(owned_slice: impl OwnedSlice<Item = T>, allocator: A) -> Self {
526        panic_on_error(Self::generic_from_owned_slice_in(owned_slice, allocator))
527    }
528
529    /// Constructs a new `BumpVec<T>` from an [`OwnedSlice`].
530    ///
531    /// # Errors
532    /// Errors if the allocation fails.
533    ///
534    /// # Examples
535    /// ```
536    /// # use bump_scope::{Bump, BumpVec};
537    /// # let bump: Bump = Bump::new();
538    /// // by value
539    /// let a = BumpVec::try_from_owned_slice_in([1, 2], &bump)?;
540    /// let b = BumpVec::try_from_owned_slice_in(vec![3, 4], &bump)?;
541    /// let c = BumpVec::try_from_owned_slice_in(bump.alloc_iter(5..=6), &bump)?;
542    ///
543    /// // by mutable reference
544    /// let mut other = vec![7, 8];
545    /// let d = BumpVec::try_from_owned_slice_in(&mut other, &bump)?;
546    /// assert!(other.is_empty());
547    ///
548    /// assert_eq!(a, [1, 2]);
549    /// assert_eq!(b, [3, 4]);
550    /// assert_eq!(c, [5, 6]);
551    /// assert_eq!(d, [7, 8]);
552    /// # Ok::<(), bump_scope::alloc::AllocError>(())
553    /// ```
554    #[inline(always)]
555    pub fn try_from_owned_slice_in(owned_slice: impl OwnedSlice<Item = T>, allocator: A) -> Result<Self, AllocError> {
556        Self::generic_from_owned_slice_in(owned_slice, allocator)
557    }
558
559    #[inline]
560    pub(crate) fn generic_from_owned_slice_in<E: ErrorBehavior>(
561        owned_slice: impl OwnedSlice<Item = T>,
562        allocator: A,
563    ) -> Result<Self, E> {
564        let owned_slice = owned_slice.into_take_owned_slice();
565        let mut this = Self::generic_with_capacity_in(owned_slice.owned_slice_ref().len(), allocator)?;
566        this.generic_append(owned_slice)?;
567        Ok(this)
568    }
569
570    /// Create a new [`BumpVec`] whose elements are taken from an iterator and allocated in the given `bump`.
571    ///
572    /// This is behaviorally identical to [`FromIterator::from_iter`].
573    ///
574    /// If you have an `impl ExactSizeIterator` then you can use [`from_iter_exact_in`](Self::from_iter_exact_in) instead for better performance.
575    ///
576    /// # Panics
577    /// Panics if the allocation fails.
578    ///
579    /// # Examples
580    /// ```
581    /// # use bump_scope::{Bump, BumpVec};
582    /// # let bump: Bump = Bump::new();
583    /// let vec = BumpVec::from_iter_in([1, 2, 3], &bump);
584    /// assert_eq!(vec, [1, 2, 3]);
585    /// ```
586    #[must_use]
587    #[inline(always)]
588    #[cfg(feature = "panic-on-alloc")]
589    pub fn from_iter_in<I>(iter: I, allocator: A) -> Self
590    where
591        I: IntoIterator<Item = T>,
592    {
593        panic_on_error(Self::generic_from_iter_in(iter, allocator))
594    }
595
596    /// Create a new [`BumpVec`] whose elements are taken from an iterator and allocated in the given `bump`.
597    ///
598    /// This is behaviorally identical to [`FromIterator::from_iter`].
599    ///
600    /// If you have an `impl ExactSizeIterator` then you can use [`from_iter_exact_in`](Self::from_iter_exact_in) instead for better performance.
601    ///
602    /// # Errors
603    /// Errors if the allocation fails.
604    ///
605    /// # Examples
606    /// ```
607    /// # use bump_scope::{Bump, BumpVec};
608    /// # let bump: Bump = Bump::new();
609    /// let vec = BumpVec::try_from_iter_in([1, 2, 3], &bump)?;
610    /// assert_eq!(vec, [1, 2, 3]);
611    /// # Ok::<(), bump_scope::alloc::AllocError>(())
612    /// ```
613    #[inline(always)]
614    pub fn try_from_iter_in<I>(iter: I, allocator: A) -> Result<Self, AllocError>
615    where
616        I: IntoIterator<Item = T>,
617    {
618        Self::generic_from_iter_in(iter, allocator)
619    }
620
621    #[inline]
622    pub(crate) fn generic_from_iter_in<E: ErrorBehavior, I>(iter: I, allocator: A) -> Result<Self, E>
623    where
624        I: IntoIterator<Item = T>,
625    {
626        let iter = iter.into_iter();
627        let capacity = iter.size_hint().0;
628
629        let mut vec = Self::generic_with_capacity_in(capacity, allocator)?;
630
631        for value in iter {
632            vec.generic_push(value)?;
633        }
634
635        Ok(vec)
636    }
637
638    /// Create a new [`BumpVec`] whose elements are taken from an iterator and allocated in the given `bump`.
639    ///
640    /// This is just like [`from_iter_in`](Self::from_iter_in) but optimized for an [`ExactSizeIterator`].
641    ///
642    /// # Panics
643    /// Panics if the allocation fails.
644    ///
645    /// # Examples
646    /// ```
647    /// # use bump_scope::{Bump, BumpVec};
648    /// # let bump: Bump = Bump::new();
649    /// let vec = BumpVec::from_iter_exact_in([1, 2, 3], &bump);
650    /// assert_eq!(vec, [1, 2, 3]);
651    /// ```
652    #[must_use]
653    #[inline(always)]
654    #[cfg(feature = "panic-on-alloc")]
655    pub fn from_iter_exact_in<I>(iter: I, allocator: A) -> Self
656    where
657        I: IntoIterator<Item = T>,
658        I::IntoIter: ExactSizeIterator,
659    {
660        panic_on_error(Self::generic_from_iter_exact_in(iter, allocator))
661    }
662
663    /// Create a new [`BumpVec`] whose elements are taken from an iterator and allocated in the given `bump`.
664    ///
665    /// This is just like [`from_iter_in`](Self::from_iter_in) but optimized for an [`ExactSizeIterator`].
666    ///
667    /// # Errors
668    /// Errors if the allocation fails.
669    ///
670    /// # Examples
671    /// ```
672    /// # use bump_scope::{Bump, BumpVec};
673    /// # let bump: Bump = Bump::new();
674    /// let vec = BumpVec::try_from_iter_exact_in([1, 2, 3], &bump)?;
675    /// assert_eq!(vec, [1, 2, 3]);
676    /// # Ok::<(), bump_scope::alloc::AllocError>(())
677    /// ```
678    #[inline(always)]
679    pub fn try_from_iter_exact_in<I>(iter: I, allocator: A) -> Result<Self, AllocError>
680    where
681        I: IntoIterator<Item = T>,
682        I::IntoIter: ExactSizeIterator,
683    {
684        Self::generic_from_iter_exact_in(iter, allocator)
685    }
686
687    #[inline]
688    pub(crate) fn generic_from_iter_exact_in<E: ErrorBehavior, I>(iter: I, allocator: A) -> Result<Self, E>
689    where
690        I: IntoIterator<Item = T>,
691        I::IntoIter: ExactSizeIterator,
692    {
693        let mut iter = iter.into_iter();
694        let len = iter.len();
695
696        let mut vec = Self::generic_with_capacity_in(len, allocator)?;
697
698        while vec.len() != vec.capacity() {
699            match iter.next() {
700                // SAFETY: we checked above that `len != capacity`, so there is space
701                Some(value) => unsafe { vec.push_unchecked(value) },
702                None => break,
703            }
704        }
705
706        Ok(vec)
707    }
708
709    /// Returns the total number of elements the vector can hold without
710    /// reallocating.
711    ///
712    /// # Examples
713    ///
714    /// ```
715    /// # use bump_scope::{Bump, BumpVec};
716    /// # let bump: Bump = Bump::new();
717    /// let vec = BumpVec::<i32, _>::with_capacity_in(2048, &bump);
718    /// assert!(vec.capacity() >= 2048);
719    /// ```
720    #[must_use]
721    #[inline(always)]
722    pub const fn capacity(&self) -> usize {
723        self.fixed.capacity()
724    }
725
726    /// Returns the number of elements in the vector, also referred to
727    /// as its 'length'.
728    ///
729    /// # Examples
730    /// ```
731    /// # use bump_scope::{Bump, bump_vec};
732    /// # let bump: Bump = Bump::new();
733    /// let a = bump_vec![in &bump; 1, 2, 3];
734    /// assert_eq!(a.len(), 3);
735    /// ```
736    #[must_use]
737    #[inline(always)]
738    pub const fn len(&self) -> usize {
739        self.fixed.len()
740    }
741
742    /// Returns `true` if the vector contains no elements.
743    ///
744    /// # Examples
745    /// ```
746    /// # use bump_scope::{Bump, BumpVec};
747    /// # let bump: Bump = Bump::new();
748    /// let mut v = BumpVec::new_in(&bump);
749    /// assert!(v.is_empty());
750    ///
751    /// v.push(1);
752    /// assert!(!v.is_empty());
753    /// ```
754    #[must_use]
755    #[inline(always)]
756    pub const fn is_empty(&self) -> bool {
757        self.fixed.len() == 0
758    }
759
760    /// Splits the vector into two by removing the specified range.
761    ///
762    /// This method does not allocate and does not change the order of the elements.
763    ///
764    /// The excess capacity may end up in either vector.
765    /// This behavior is different from <code>Vec::[split_off](alloc_crate::vec::Vec::split_off)</code> which allocates a new vector for the split-off elements
766    /// so the original vector keeps its capacity.
767    /// If you rather want that behavior then you can write this instead:
768    /// ```
769    /// # use bump_scope::{Bump, BumpVec};
770    /// # let bump: Bump = Bump::new();
771    /// # let mut vec = BumpVec::from_owned_slice_in(['a', 'b', 'c', 'd', 'e'], &bump);
772    /// # let start = 1;
773    /// # let end = 4;
774    /// let mut other = BumpVec::new_in(*vec.allocator());
775    /// other.append(vec.drain(start..end));
776    /// # assert_eq!(vec, ['a', 'e']);
777    /// # assert_eq!(other, ['b', 'c', 'd']);
778    /// ```
779    ///
780    /// # Panics
781    ///
782    /// Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
783    ///
784    /// # Complexity
785    ///
786    /// This operation takes `O(1)` time if either the range starts at 0, ends at `len`, or is empty.
787    /// Otherwise it takes `O(min(end, len - start))` time.
788    ///
789    /// # Examples
790    ///
791    /// ```
792    /// # use bump_scope::{Bump, BumpVec};
793    /// # let bump: Bump = Bump::new();
794    /// let mut vec = BumpVec::with_capacity_in(10, &bump);
795    /// vec.append([1, 2, 3, 4, 5, 6, 7, 8]);
796    ///
797    /// let front = vec.split_off(..2);
798    /// assert_eq!(front, [1, 2]);
799    /// assert_eq!(vec, [3, 4, 5, 6, 7, 8]);
800    ///
801    /// let back = vec.split_off(4..);
802    /// assert_eq!(back, [7, 8]);
803    /// assert_eq!(vec, [3, 4, 5, 6]);
804    ///
805    /// let middle = vec.split_off(1..3);
806    /// assert_eq!(middle, [4, 5]);
807    /// assert_eq!(vec, [3, 6]);
808    ///
809    /// let rest = vec.split_off(..);
810    /// assert_eq!(rest, [3, 6]);
811    /// assert_eq!(vec, []);
812    /// ```
813    #[inline]
814    #[expect(clippy::return_self_not_must_use)]
815    pub fn split_off(&mut self, range: impl RangeBounds<usize>) -> Self
816    where
817        A: Clone,
818    {
819        let other = unsafe { self.fixed.cook_mut() }.split_off(range);
820
821        Self {
822            fixed: unsafe { RawFixedBumpVec::from_cooked(other) },
823            allocator: self.allocator.clone(),
824        }
825    }
826
827    /// Removes the last element from a vector and returns it, or [`None`] if it
828    /// is empty.
829    ///
830    /// # Examples
831    /// ```
832    /// # use bump_scope::{Bump, bump_vec};
833    /// # let bump: Bump = Bump::new();
834    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
835    /// assert_eq!(vec.pop(), Some(3));
836    /// assert_eq!(vec, [1, 2]);
837    /// ```
838    ///
839    /// # Time complexity
840    /// Takes *O*(1) time.
841    #[inline(always)]
842    pub fn pop(&mut self) -> Option<T> {
843        unsafe { self.fixed.cook_mut() }.pop()
844    }
845
846    /// Removes and returns the last element from a vector if the predicate
847    /// returns `true`, or [`None`] if the predicate returns false or the vector
848    /// is empty (the predicate will not be called in that case).
849    ///
850    /// # Examples
851    ///
852    /// ```
853    /// # use bump_scope::{Bump, bump_vec};
854    /// # let bump: Bump = Bump::new();
855    /// let mut vec = bump_vec![in &bump; 1, 2, 3, 4];
856    /// let pred = |x: &mut i32| *x % 2 == 0;
857    ///
858    /// assert_eq!(vec.pop_if(pred), Some(4));
859    /// assert_eq!(vec, [1, 2, 3]);
860    /// assert_eq!(vec.pop_if(pred), None);
861    /// ```
862    pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T> {
863        let last = self.last_mut()?;
864        if predicate(last) { self.pop() } else { None }
865    }
866
867    /// Clears the vector, removing all values.
868    ///
869    /// # Examples
870    /// ```
871    /// # use bump_scope::{Bump, bump_vec};
872    /// # let bump: Bump = Bump::new();
873    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
874    /// vec.clear();
875    /// assert!(vec.is_empty());
876    /// ```
877    #[inline(always)]
878    pub fn clear(&mut self) {
879        unsafe { self.fixed.cook_mut() }.clear();
880    }
881
882    /// Shortens the vector, keeping the first `len` elements and dropping
883    /// the rest.
884    ///
885    /// If `len` is greater than the vector's current length, this has no
886    /// effect.
887    ///
888    /// The [`drain`] method can emulate `truncate`, but causes the excess
889    /// elements to be returned instead of dropped.
890    ///
891    /// Note that this method has no effect on the allocated capacity
892    /// of the vector.
893    ///
894    /// # Examples
895    ///
896    /// Truncating a five element vector to two elements:
897    ///
898    /// ```
899    /// # use bump_scope::{Bump, bump_vec};
900    /// # let bump: Bump = Bump::new();
901    /// #
902    /// let mut vec = bump_vec![in &bump; 1, 2, 3, 4, 5];
903    /// vec.truncate(2);
904    /// assert_eq!(vec, [1, 2]);
905    /// ```
906    ///
907    /// No truncation occurs when `len` is greater than the vector's current
908    /// length:
909    ///
910    /// ```
911    /// # use bump_scope::{Bump, bump_vec};
912    /// # let bump: Bump = Bump::new();
913    /// #
914    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
915    /// vec.truncate(8);
916    /// assert_eq!(vec, [1, 2, 3]);
917    /// ```
918    ///
919    /// Truncating when `len == 0` is equivalent to calling the [`clear`]
920    /// method.
921    ///
922    /// ```
923    /// # use bump_scope::{Bump, bump_vec};
924    /// # let bump: Bump = Bump::new();
925    /// #
926    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
927    /// vec.truncate(0);
928    /// assert_eq!(vec, []);
929    /// ```
930    ///
931    /// [`clear`]: Self::clear
932    /// [`drain`]: Self::drain
933    pub fn truncate(&mut self, len: usize) {
934        unsafe { self.fixed.cook_mut() }.truncate(len);
935    }
936
937    /// Removes and returns the element at position `index` within the vector,
938    /// shifting all elements after it to the left.
939    ///
940    /// Note: Because this shifts over the remaining elements, it has a
941    /// worst-case performance of *O*(*n*). If you don't need the order of elements
942    /// to be preserved, use [`swap_remove`] instead.
943    ///
944    /// # Panics
945    /// Panics if `index` is out of bounds.
946    ///
947    /// [`swap_remove`]: Self::swap_remove
948    /// # Examples
949    /// ```
950    /// # use bump_scope::{Bump, bump_vec};
951    /// # let bump: Bump = Bump::new();
952    /// let mut v = bump_vec![in &bump; 1, 2, 3];
953    /// assert_eq!(v.remove(1), 2);
954    /// assert_eq!(v, [1, 3]);
955    /// ```
956    #[track_caller]
957    pub fn remove(&mut self, index: usize) -> T {
958        unsafe { self.fixed.cook_mut() }.remove(index)
959    }
960
961    /// Removes an element from the vector and returns it.
962    ///
963    /// The removed element is replaced by the last element of the vector.
964    ///
965    /// This does not preserve ordering, but is *O*(1).
966    /// If you need to preserve the element order, use [`remove`] instead.
967    ///
968    /// # Panics
969    /// Panics if `index` is out of bounds.
970    ///
971    /// [`remove`]: Self::remove
972    /// # Examples
973    /// ```
974    /// # use bump_scope::{Bump, bump_vec};
975    /// # let bump: Bump = Bump::new();
976    /// #
977    /// let mut v = bump_vec![in &bump; "foo", "bar", "baz", "qux"];
978    ///
979    /// assert_eq!(v.swap_remove(1), "bar");
980    /// assert_eq!(v, ["foo", "qux", "baz"]);
981    ///
982    /// assert_eq!(v.swap_remove(0), "foo");
983    /// assert_eq!(v, ["baz", "qux"]);
984    /// ```
985    #[inline]
986    pub fn swap_remove(&mut self, index: usize) -> T {
987        unsafe { self.fixed.cook_mut() }.swap_remove(index)
988    }
989
990    /// Extracts a slice containing the entire vector.
991    ///
992    /// Equivalent to `&s[..]`.
993    #[must_use]
994    #[inline(always)]
995    pub const fn as_slice(&self) -> &[T] {
996        unsafe { self.fixed.cook_ref() }.as_slice()
997    }
998
999    /// Extracts a mutable slice containing the entire vector.
1000    ///
1001    /// Equivalent to `&mut s[..]`.
1002    #[must_use]
1003    #[inline(always)]
1004    pub fn as_mut_slice(&mut self) -> &mut [T] {
1005        unsafe { self.fixed.cook_mut() }.as_mut_slice()
1006    }
1007
1008    /// Returns a raw pointer to the slice, or a dangling raw pointer
1009    /// valid for zero sized reads.
1010    #[inline]
1011    #[must_use]
1012    pub fn as_ptr(&self) -> *const T {
1013        self.fixed.as_ptr()
1014    }
1015
1016    /// Returns an unsafe mutable pointer to slice, or a dangling
1017    /// raw pointer valid for zero sized reads.
1018    #[inline]
1019    pub fn as_mut_ptr(&mut self) -> *mut T {
1020        self.fixed.as_mut_ptr()
1021    }
1022
1023    /// Returns a `NonNull` pointer to the vector's buffer, or a dangling
1024    /// `NonNull` pointer valid for zero sized reads if the vector didn't allocate.
1025    ///
1026    /// The caller must ensure that the vector outlives the pointer this
1027    /// function returns, or else it will end up dangling.
1028    /// Modifying the vector may cause its buffer to be reallocated,
1029    /// which would also make any pointers to it invalid.
1030    ///
1031    /// This method guarantees that for the purpose of the aliasing model, this method
1032    /// does not materialize a reference to the underlying slice, and thus the returned pointer
1033    /// will remain valid when mixed with other calls to [`as_ptr`], [`as_mut_ptr`],
1034    /// and [`as_non_null`].
1035    /// Note that calling other methods that materialize references to the slice,
1036    /// or references to specific elements you are planning on accessing through this pointer,
1037    /// may still invalidate this pointer.
1038    /// See the second example below for how this guarantee can be used.
1039    ///
1040    /// # Examples
1041    ///
1042    /// ```
1043    /// # use bump_scope::{Bump, BumpVec};
1044    /// # let bump: Bump = Bump::new();
1045    /// // Allocate vector big enough for 4 elements.
1046    /// let size = 4;
1047    /// let mut x: BumpVec<i32, _> = BumpVec::with_capacity_in(size, &bump);
1048    /// let x_ptr = x.as_non_null();
1049    ///
1050    /// // Initialize elements via raw pointer writes, then set length.
1051    /// unsafe {
1052    ///     for i in 0..size {
1053    ///         x_ptr.add(i).write(i as i32);
1054    ///     }
1055    ///     x.set_len(size);
1056    /// }
1057    /// assert_eq!(&*x, &[0, 1, 2, 3]);
1058    /// ```
1059    ///
1060    /// Due to the aliasing guarantee, the following code is legal:
1061    ///
1062    /// ```
1063    /// # use bump_scope::{Bump, bump_vec};
1064    /// # let bump: Bump = Bump::new();
1065    /// unsafe {
1066    ///     let v = bump_vec![in &bump; 0];
1067    ///     let ptr1 = v.as_non_null();
1068    ///     ptr1.write(1);
1069    ///     let ptr2 = v.as_non_null();
1070    ///     ptr2.write(2);
1071    ///     // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
1072    ///     ptr1.write(3);
1073    /// }
1074    /// ```
1075    ///
1076    /// [`as_mut_ptr`]: Self::as_mut_ptr
1077    /// [`as_ptr`]: Self::as_ptr
1078    /// [`as_non_null`]: Self::as_non_null
1079    #[must_use]
1080    #[inline(always)]
1081    pub const fn as_non_null(&self) -> NonNull<T> {
1082        self.fixed.as_non_null()
1083    }
1084
1085    /// Appends an element to the back of the collection.
1086    ///
1087    /// # Safety
1088    /// Vector must not be full.
1089    ///
1090    /// # Examples
1091    /// ```
1092    /// # use bump_scope::{Bump, BumpVec};
1093    /// # let bump: Bump = Bump::new();
1094    /// let mut vec = BumpVec::with_capacity_in(3, &bump);
1095    /// vec.append([1, 2]);
1096    /// unsafe { vec.push_unchecked(3) };
1097    /// assert_eq!(vec, [1, 2, 3]);
1098    /// ```
1099    #[inline(always)]
1100    pub unsafe fn push_unchecked(&mut self, value: T) {
1101        _ = unsafe { self.push_mut_unchecked(value) };
1102    }
1103
1104    /// Appends an element to the back of the collection, returning a reference to it.
1105    ///
1106    /// # Safety
1107    /// Vector must not be full.
1108    ///
1109    /// # Examples
1110    /// ```
1111    /// # use bump_scope::{Bump, BumpVec};
1112    /// # let bump: Bump = Bump::new();
1113    /// let mut vec = BumpVec::with_capacity_in(4, &bump);
1114    /// vec.append([1, 2]);
1115    ///
1116    /// let last = unsafe { vec.push_mut_unchecked(3) };
1117    /// assert_eq!(*last, 3);
1118    /// assert_eq!(vec, [1, 2, 3]);
1119    ///
1120    /// let last = unsafe { vec.push_mut_unchecked(3) };
1121    /// *last += 1;
1122    /// assert_eq!(vec, [1, 2, 3, 4]);
1123    /// ```
1124    #[inline(always)]
1125    #[must_use = "if you don't need a reference to the value, use `push_unchecked` instead"]
1126    pub unsafe fn push_mut_unchecked(&mut self, value: T) -> &mut T {
1127        unsafe { self.fixed.cook_mut().push_mut_unchecked(value) }
1128    }
1129
1130    /// Appends an element to the back of the collection.
1131    ///
1132    /// # Safety
1133    /// Vector must not be full.
1134    #[inline(always)]
1135    #[deprecated = "use `push_unchecked(f())` instead"]
1136    pub unsafe fn push_with_unchecked(&mut self, f: impl FnOnce() -> T) {
1137        unsafe { self.push_unchecked(f()) }
1138    }
1139
1140    /// Forces the length of the vector to `new_len`.
1141    ///
1142    /// This is a low-level operation that maintains none of the normal
1143    /// invariants of the type. Normally changing the length of a vector
1144    /// is done using one of the safe operations instead, such as
1145    /// [`resize`], [`truncate`], [`extend`], or [`clear`].
1146    ///
1147    /// # Safety
1148    /// - `new_len` must be less than or equal to the [`capacity`].
1149    /// - The elements at `old_len..new_len` must be initialized.
1150    ///
1151    /// [`resize`]: Self::resize
1152    /// [`truncate`]: Self::truncate
1153    /// [`extend`]: Self::extend
1154    /// [`clear`]: Self::clear
1155    /// [`capacity`]: Self::capacity
1156    #[inline]
1157    pub unsafe fn set_len(&mut self, new_len: usize) {
1158        unsafe { self.fixed.set_len(new_len) };
1159    }
1160
1161    #[inline]
1162    pub(crate) unsafe fn inc_len(&mut self, amount: usize) {
1163        unsafe { self.fixed.cook_mut().inc_len(amount) };
1164    }
1165
1166    /// Appends an element to the back of a collection.
1167    ///
1168    /// # Panics
1169    /// Panics if the allocation fails.
1170    ///
1171    /// # Examples
1172    /// ```
1173    /// # use bump_scope::{bump_vec, Bump};
1174    /// # let bump: Bump = Bump::new();
1175    /// let mut vec = bump_vec![in &bump; 1, 2];
1176    /// vec.push(3);
1177    /// assert_eq!(vec, [1, 2, 3]);
1178    /// ```
1179    #[inline(always)]
1180    #[cfg(feature = "panic-on-alloc")]
1181    pub fn push(&mut self, value: T) {
1182        panic_on_error(self.generic_push(value));
1183    }
1184
1185    /// Appends an element to the back of a collection.
1186    ///
1187    /// # Errors
1188    /// Errors if the allocation fails.
1189    ///
1190    /// # Examples
1191    /// ```
1192    /// # use bump_scope::{bump_vec, Bump};
1193    /// # let bump: Bump = Bump::new();
1194    /// let mut vec = bump_vec![try in &bump; 1, 2]?;
1195    /// vec.try_push(3)?;
1196    /// assert_eq!(vec, [1, 2, 3]);
1197    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1198    /// ```
1199    #[inline(always)]
1200    pub fn try_push(&mut self, value: T) -> Result<(), AllocError> {
1201        self.generic_push(value)
1202    }
1203
1204    #[inline]
1205    pub(crate) fn generic_push<E: ErrorBehavior>(&mut self, value: T) -> Result<(), E> {
1206        self.generic_push_with(|| value)
1207    }
1208
1209    /// Reserves space for one more element, then calls `f`
1210    /// to produce the value that is appended.
1211    ///
1212    /// In some cases this could be more performant than `push(f())` because it
1213    /// permits the compiler to directly place `T` in the vector instead of
1214    /// constructing it on the stack and copying it over.
1215    ///
1216    /// # Panics
1217    /// Panics if the allocation fails.
1218    ///
1219    /// # Examples
1220    /// ```
1221    /// # use bump_scope::{Bump, bump_vec};
1222    /// # let bump: Bump = Bump::new();
1223    /// let mut vec = bump_vec![in &bump; 1, 2];
1224    /// vec.push_with(|| 3);
1225    /// assert_eq!(vec, [1, 2, 3]);
1226    /// ```
1227    #[inline(always)]
1228    #[cfg(feature = "panic-on-alloc")]
1229    pub fn push_with(&mut self, f: impl FnOnce() -> T) {
1230        panic_on_error(self.generic_push_with(f));
1231    }
1232
1233    /// Reserves space for one more element, then calls `f`
1234    /// to produce the value that is appended.
1235    ///
1236    /// In some cases this could be more performant than `push(f())` because it
1237    /// permits the compiler to directly place `T` in the vector instead of
1238    /// constructing it on the stack and copying it over.
1239    ///
1240    /// # Errors
1241    /// Errors if the allocation fails.
1242    ///
1243    /// # Examples
1244    /// ```
1245    /// # use bump_scope::{Bump, bump_vec};
1246    /// # let bump: Bump = Bump::new();
1247    /// let mut vec = bump_vec![in &bump; 1, 2];
1248    /// vec.try_push_with(|| 3)?;
1249    /// assert_eq!(vec, [1, 2, 3]);
1250    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1251    /// ```
1252    #[inline(always)]
1253    pub fn try_push_with(&mut self, f: impl FnOnce() -> T) -> Result<(), AllocError> {
1254        self.generic_push_with(f)
1255    }
1256
1257    #[inline]
1258    pub(crate) fn generic_push_with<E: ErrorBehavior>(&mut self, f: impl FnOnce() -> T) -> Result<(), E> {
1259        self.generic_push_mut_with(f).map(drop)
1260    }
1261
1262    /// Appends an element to the back of a collection.
1263    ///
1264    /// # Panics
1265    /// Panics if the allocation fails.
1266    ///
1267    /// # Examples
1268    /// ```
1269    /// # use bump_scope::{Bump, bump_vec};
1270    /// # let bump: Bump = Bump::new();
1271    /// let mut vec = bump_vec![in &bump; 1, 2];
1272    ///
1273    /// let last = vec.push_mut(3);
1274    /// assert_eq!(*last, 3);
1275    /// assert_eq!(vec, [1, 2, 3]);
1276    ///
1277    /// let last = vec.push_mut(3);
1278    /// *last += 1;
1279    /// assert_eq!(vec, [1, 2, 3, 4]);
1280    /// ```
1281    #[inline(always)]
1282    #[cfg(feature = "panic-on-alloc")]
1283    #[must_use = "if you don't need a reference to the value, use `push` instead"]
1284    pub fn push_mut(&mut self, value: T) -> &mut T {
1285        panic_on_error(self.generic_push_mut(value))
1286    }
1287
1288    /// Appends an element to the back of a collection.
1289    ///
1290    /// # Errors
1291    /// Errors if the allocation fails.
1292    ///
1293    /// # Examples
1294    /// ```
1295    /// # use bump_scope::{Bump, mut_bump_vec};
1296    /// # let mut bump: Bump = Bump::new();
1297    /// let mut vec = mut_bump_vec![try in &mut bump; 1, 2]?;
1298    ///
1299    /// let last = vec.try_push_mut(3)?;
1300    /// assert_eq!(*last, 3);
1301    /// assert_eq!(vec, [1, 2, 3]);
1302    ///
1303    /// let last = vec.try_push_mut(3)?;
1304    /// *last += 1;
1305    /// assert_eq!(vec, [1, 2, 3, 4]);
1306    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1307    /// ```
1308    #[inline(always)]
1309    #[must_use = "if you don't need a reference to the value, use `push` instead"]
1310    pub fn try_push_mut(&mut self, value: T) -> Result<&mut T, AllocError> {
1311        self.generic_push_mut(value)
1312    }
1313
1314    #[inline]
1315    pub(crate) fn generic_push_mut<E: ErrorBehavior>(&mut self, value: T) -> Result<&mut T, E> {
1316        self.generic_push_mut_with(|| value)
1317    }
1318
1319    /// Reserves space for one more element, then calls `f`
1320    /// to produce the value that is appended.
1321    ///
1322    /// In some cases this could be more performant than `push(f())` because it
1323    /// permits the compiler to directly place `T` in the vector instead of
1324    /// constructing it on the stack and copying it over.
1325    ///
1326    /// # Panics
1327    /// Panics if the allocation fails.
1328    ///
1329    /// # Examples
1330    /// ```
1331    /// # use bump_scope::{Bump, BumpVec};
1332    /// # let bump: Bump = Bump::new();
1333    /// let mut vec = BumpVec::new_in(&bump);
1334    /// let item = vec.push_mut_with(i32::default);
1335    /// *item += 1;
1336    /// *item += 2;
1337    /// assert_eq!(*item, 3);
1338    /// ```
1339    #[inline(always)]
1340    #[cfg(feature = "panic-on-alloc")]
1341    #[must_use = "if you don't need a reference to the value, use `push` instead"]
1342    pub fn push_mut_with(&mut self, f: impl FnOnce() -> T) -> &mut T {
1343        panic_on_error(self.generic_push_mut_with(f))
1344    }
1345
1346    /// Reserves space for one more element, then calls `f`
1347    /// to produce the value that is appended.
1348    ///
1349    /// In some cases this could be more performant than `push(f())` because it
1350    /// permits the compiler to directly place `T` in the vector instead of
1351    /// constructing it on the stack and copying it over.
1352    ///
1353    /// # Errors
1354    /// Errors if the allocation fails.
1355    ///
1356    /// # Examples
1357    /// ```
1358    /// # use bump_scope::{Bump, BumpVec};
1359    /// # let bump: Bump = Bump::new();
1360    /// let mut vec = BumpVec::new_in(&bump);
1361    /// let item = vec.try_push_mut_with(i32::default)?;
1362    /// *item += 1;
1363    /// *item += 2;
1364    /// assert_eq!(*item, 3);
1365    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1366    /// ```
1367    #[inline(always)]
1368    #[must_use = "if you don't need a reference to the value, use `push` instead"]
1369    pub fn try_push_mut_with(&mut self, f: impl FnOnce() -> T) -> Result<&mut T, AllocError> {
1370        self.generic_push_mut_with(f)
1371    }
1372
1373    #[inline]
1374    pub(crate) fn generic_push_mut_with<E: ErrorBehavior>(&mut self, f: impl FnOnce() -> T) -> Result<&mut T, E> {
1375        self.generic_reserve_one()?;
1376        Ok(unsafe { self.push_mut_unchecked(f()) })
1377    }
1378
1379    /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1380    ///
1381    /// # Panics
1382    /// Panics if the allocation fails.
1383    ///
1384    /// Panics if `index > len`.
1385    ///
1386    /// # Examples
1387    /// ```
1388    /// # use bump_scope::{Bump, bump_vec};
1389    /// # let bump: Bump = Bump::new();
1390    /// let mut vec = bump_vec![in &bump; 'b', 'd'];
1391    /// vec.insert(1, 'c');
1392    /// assert_eq!(vec, ['b', 'c', 'd']);
1393    /// vec.insert(0, 'a');
1394    /// assert_eq!(vec, ['a', 'b', 'c', 'd']);
1395    /// vec.insert(4, 'e');
1396    /// assert_eq!(vec, ['a', 'b', 'c', 'd', 'e']);
1397    /// ```
1398    #[inline(always)]
1399    #[cfg(feature = "panic-on-alloc")]
1400    pub fn insert(&mut self, index: usize, element: T) {
1401        panic_on_error(self.generic_insert_mut(index, element));
1402    }
1403
1404    /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1405    ///
1406    /// # Panics
1407    /// Panics if `index > len`.
1408    ///
1409    /// # Errors
1410    /// Errors if the allocation fails.
1411    ///
1412    /// # Examples
1413    /// ```
1414    /// # use bump_scope::{Bump, bump_vec};
1415    /// # let bump: Bump = Bump::new();
1416    /// let mut vec = bump_vec![try in &bump; 'b', 'd']?;
1417    /// vec.try_insert(1, 'c')?;
1418    /// assert_eq!(vec, ['b', 'c', 'd']);
1419    /// vec.try_insert(0, 'a')?;
1420    /// assert_eq!(vec, ['a', 'b', 'c', 'd']);
1421    /// vec.try_insert(4, 'e')?;
1422    /// assert_eq!(vec, ['a', 'b', 'c', 'd', 'e']);
1423    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1424    /// ```
1425    #[inline(always)]
1426    pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), AllocError> {
1427        self.generic_insert_mut(index, element).map(drop)
1428    }
1429
1430    /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1431    ///
1432    /// # Panics
1433    /// Panics if the vector does not have enough capacity.
1434    ///
1435    /// Panics if `index > len`.
1436    ///
1437    /// # Examples
1438    /// ```
1439    /// # use bump_scope::{Bump, bump_vec};
1440    /// # let bump: Bump = Bump::new();
1441    /// let mut vec = bump_vec![in &bump; 1, 3, 5, 9];
1442    /// let x = vec.insert_mut(3, 6);
1443    /// *x += 1;
1444    /// assert_eq!(vec, [1, 3, 5, 7, 9]);
1445    /// ```
1446    #[inline(always)]
1447    #[cfg(feature = "panic-on-alloc")]
1448    #[must_use = "if you don't need a reference to the value, use `insert` instead"]
1449    pub fn insert_mut(&mut self, index: usize, element: T) -> &mut T {
1450        panic_on_error(self.generic_insert_mut(index, element))
1451    }
1452
1453    /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1454    ///
1455    /// # Panics
1456    /// Panics if `index > len`.
1457    ///
1458    /// # Errors
1459    /// Errors if the vector does not have enough capacity.
1460    ///
1461    /// # Examples
1462    /// ```
1463    /// # use bump_scope::{Bump, bump_vec};
1464    /// # let bump: Bump = Bump::new();
1465    /// let mut vec = bump_vec![try in &bump; 1, 3, 5, 9]?;
1466    /// let x = vec.try_insert_mut(3, 6)?;
1467    /// *x += 1;
1468    /// assert_eq!(vec, [1, 3, 5, 7, 9]);
1469    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1470    /// ```
1471    #[inline(always)]
1472    #[must_use = "if you don't need a reference to the value, use `try_insert` instead"]
1473    pub fn try_insert_mut(&mut self, index: usize, element: T) -> Result<&mut T, AllocError> {
1474        self.generic_insert_mut(index, element)
1475    }
1476
1477    #[inline]
1478    pub(crate) fn generic_insert_mut<E: ErrorBehavior>(&mut self, index: usize, element: T) -> Result<&mut T, E> {
1479        #[cold]
1480        #[track_caller]
1481        #[inline(never)]
1482        fn assert_failed(index: usize, len: usize) -> ! {
1483            panic!("insertion index (is {index}) should be <= len (is {len})");
1484        }
1485
1486        if index > self.len() {
1487            assert_failed(index, self.len());
1488        }
1489
1490        self.generic_reserve_one()?;
1491
1492        unsafe {
1493            let pos = self.as_mut_ptr().add(index);
1494
1495            if index != self.len() {
1496                let len = self.len() - index;
1497                ptr::copy(pos, pos.add(1), len);
1498            }
1499
1500            pos.write(element);
1501            self.inc_len(1);
1502            Ok(&mut *pos)
1503        }
1504    }
1505
1506    /// Copies and appends all elements in a slice to the `BumpVec`.
1507    ///
1508    /// Iterates over the `slice`, copies each element, and then appends
1509    /// it to this `BumpVec`. The `slice` is traversed in-order.
1510    ///
1511    /// Note that this function is same as [`extend`] except that it is
1512    /// specialized to work with copyable slices instead.
1513    ///
1514    /// [`extend`]: Self::extend
1515    ///
1516    /// # Panics
1517    /// Panics if the allocation fails.
1518    ///
1519    /// # Examples
1520    /// ```
1521    /// # use bump_scope::{ Bump, bump_vec };
1522    /// # let bump: Bump = Bump::new();
1523    /// let mut vec = bump_vec![in &bump; 1];
1524    /// vec.extend_from_slice_copy(&[2, 3, 4]);
1525    /// assert_eq!(vec, [1, 2, 3, 4]);
1526    /// ```
1527    #[inline(always)]
1528    #[cfg(feature = "panic-on-alloc")]
1529    pub fn extend_from_slice_copy(&mut self, slice: &[T])
1530    where
1531        T: Copy,
1532    {
1533        panic_on_error(self.generic_extend_from_slice_copy(slice));
1534    }
1535
1536    /// Copies and appends all elements in a slice to the `BumpVec`.
1537    ///
1538    /// Iterates over the `slice`, copies each element, and then appends
1539    /// it to this `BumpVec`. The `slice` is traversed in-order.
1540    ///
1541    /// Note that this function is same as [`extend`] except that it is
1542    /// specialized to work with copyable slices instead.
1543    ///
1544    /// [`extend`]: Self::extend
1545    ///
1546    /// # Errors
1547    /// Errors if the allocation fails.
1548    ///
1549    /// # Examples
1550    /// ```
1551    /// # use bump_scope::{ Bump, bump_vec };
1552    /// # let bump: Bump = Bump::new();
1553    /// let mut vec = bump_vec![try in &bump; 1]?;
1554    /// vec.try_extend_from_slice_copy(&[2, 3, 4])?;
1555    /// assert_eq!(vec, [1, 2, 3, 4]);
1556    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1557    /// ```
1558    #[inline(always)]
1559    pub fn try_extend_from_slice_copy(&mut self, slice: &[T]) -> Result<(), AllocError>
1560    where
1561        T: Copy,
1562    {
1563        self.generic_extend_from_slice_copy(slice)
1564    }
1565
1566    #[inline]
1567    pub(crate) fn generic_extend_from_slice_copy<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1568    where
1569        T: Copy,
1570    {
1571        unsafe { self.extend_by_copy_nonoverlapping(slice) }
1572    }
1573
1574    /// Clones and appends all elements in a slice to the `BumpVec`.
1575    ///
1576    /// Iterates over the `slice`, clones each element, and then appends
1577    /// it to this `BumpVec`. The `slice` is traversed in-order.
1578    ///
1579    /// Note that this function is same as [`extend`] except that it is
1580    /// specialized to work with slices instead.
1581    ///
1582    /// [`extend`]: Self::extend
1583    ///
1584    /// # Panics
1585    /// Panics if the allocation fails.
1586    ///
1587    /// # Examples
1588    /// ```
1589    /// # use std::string::String;
1590    /// # use bump_scope::{ Bump, bump_vec };
1591    /// # let bump: Bump = Bump::new();
1592    /// let mut vec = bump_vec![in &bump; String::from("a")];
1593    /// vec.extend_from_slice_clone(&[String::from("b"), String::from("c")]);
1594    /// assert_eq!(vec, ["a", "b", "c"]);
1595    /// ```
1596    #[inline(always)]
1597    #[cfg(feature = "panic-on-alloc")]
1598    pub fn extend_from_slice_clone(&mut self, slice: &[T])
1599    where
1600        T: Clone,
1601    {
1602        panic_on_error(self.generic_extend_from_slice_clone(slice));
1603    }
1604
1605    /// Clones and appends all elements in a slice to the `BumpVec`.
1606    ///
1607    /// Iterates over the `slice`, clones each element, and then appends
1608    /// it to this `BumpVec`. The `slice` is traversed in-order.
1609    ///
1610    /// Note that this function is same as [`extend`] except that it is
1611    /// specialized to work with slices instead.
1612    ///
1613    /// [`extend`]: Self::extend
1614    ///
1615    /// # Errors
1616    /// Errors if the allocation fails.
1617    ///
1618    /// # Examples
1619    /// ```
1620    /// # use std::string::String;
1621    /// # use bump_scope::{ Bump, bump_vec };
1622    /// # let bump: Bump = Bump::new();
1623    /// let mut vec = bump_vec![try in &bump; String::from("a")]?;
1624    /// vec.try_extend_from_slice_clone(&[String::from("b"), String::from("c")])?;
1625    /// assert_eq!(vec, ["a", "b", "c"]);
1626    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1627    /// ```
1628    #[inline(always)]
1629    pub fn try_extend_from_slice_clone(&mut self, slice: &[T]) -> Result<(), AllocError>
1630    where
1631        T: Clone,
1632    {
1633        self.generic_extend_from_slice_clone(slice)
1634    }
1635
1636    #[inline]
1637    pub(crate) fn generic_extend_from_slice_clone<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1638    where
1639        T: Clone,
1640    {
1641        self.generic_reserve(slice.len())?;
1642
1643        unsafe {
1644            let mut pos = 0usize;
1645
1646            while likely(pos != slice.len()) {
1647                let elem = slice.get_unchecked(pos);
1648                self.push_unchecked(elem.clone());
1649                pos += 1;
1650            }
1651        }
1652
1653        Ok(())
1654    }
1655
1656    /// Copies elements from `src` range to the end of the vector.
1657    ///
1658    /// # Panics
1659    /// Panics if the allocation fails.
1660    ///
1661    /// Panics if the starting point is greater than the end point or if
1662    /// the end point is greater than the length of the vector.
1663    ///
1664    /// # Examples
1665    /// ```
1666    /// # use bump_scope::{Bump, bump_vec};
1667    /// # let bump: Bump = Bump::new();
1668    /// let mut vec = bump_vec![in &bump; 0, 1, 2, 3, 4];
1669    ///
1670    /// vec.extend_from_within_copy(2..);
1671    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1672    ///
1673    /// vec.extend_from_within_copy(..2);
1674    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1675    ///
1676    /// vec.extend_from_within_copy(4..8);
1677    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1678    /// ```
1679    #[inline(always)]
1680    #[cfg(feature = "panic-on-alloc")]
1681    pub fn extend_from_within_copy<R>(&mut self, src: R)
1682    where
1683        T: Copy,
1684        R: RangeBounds<usize>,
1685    {
1686        panic_on_error(self.generic_extend_from_within_copy(src));
1687    }
1688
1689    /// Copies elements from `src` range to the end of the vector.
1690    ///
1691    /// # Panics
1692    /// Panics if the starting point is greater than the end point or if
1693    /// the end point is greater than the length of the vector.
1694    ///
1695    /// # Errors
1696    /// Errors if the allocation fails.
1697    ///
1698    /// # Examples
1699    /// ```
1700    /// # use bump_scope::{Bump, bump_vec};
1701    /// # let bump: Bump = Bump::new();
1702    /// let mut vec = bump_vec![try in &bump; 0, 1, 2, 3, 4]?;
1703    ///
1704    /// vec.try_extend_from_within_copy(2..)?;
1705    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1706    ///
1707    /// vec.try_extend_from_within_copy(..2)?;
1708    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1709    ///
1710    /// vec.try_extend_from_within_copy(4..8)?;
1711    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1712    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1713    /// ```
1714    #[inline(always)]
1715    pub fn try_extend_from_within_copy<R>(&mut self, src: R) -> Result<(), AllocError>
1716    where
1717        T: Copy,
1718        R: RangeBounds<usize>,
1719    {
1720        self.generic_extend_from_within_copy(src)
1721    }
1722
1723    #[inline]
1724    pub(crate) fn generic_extend_from_within_copy<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1725    where
1726        T: Copy,
1727        R: RangeBounds<usize>,
1728    {
1729        let range = slice::range(src, ..self.len());
1730        let count = range.len();
1731
1732        self.generic_reserve(count)?;
1733
1734        // SAFETY:
1735        // - `slice::range` guarantees that the given range is valid for indexing self
1736        unsafe {
1737            let ptr = self.as_mut_ptr();
1738
1739            let src = ptr.add(range.start);
1740            let dst = ptr.add(self.len());
1741            ptr::copy_nonoverlapping(src, dst, count);
1742
1743            self.inc_len(count);
1744            Ok(())
1745        }
1746    }
1747
1748    /// Clones elements from `src` range to the end of the vector.
1749    ///
1750    /// # Panics
1751    /// Panics if the allocation fails.
1752    ///
1753    /// Panics if the starting point is greater than the end point or if
1754    /// the end point is greater than the length of the vector.
1755    ///
1756    /// # Examples
1757    /// ```
1758    /// # use bump_scope::{Bump, bump_vec};
1759    /// # let bump: Bump = Bump::new();
1760    /// let mut vec = bump_vec![in &bump; 0, 1, 2, 3, 4];
1761    ///
1762    /// vec.extend_from_within_clone(2..);
1763    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1764    ///
1765    /// vec.extend_from_within_clone(..2);
1766    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1767    ///
1768    /// vec.extend_from_within_clone(4..8);
1769    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1770    /// ```
1771    #[inline(always)]
1772    #[cfg(feature = "panic-on-alloc")]
1773    pub fn extend_from_within_clone<R>(&mut self, src: R)
1774    where
1775        T: Clone,
1776        R: RangeBounds<usize>,
1777    {
1778        panic_on_error(self.generic_extend_from_within_clone(src));
1779    }
1780
1781    /// Clones elements from `src` range to the end of the vector.
1782    ///
1783    /// # Panics
1784    /// Panics if the starting point is greater than the end point or if
1785    /// the end point is greater than the length of the vector.
1786    ///
1787    /// # Errors
1788    /// Errors if the allocation fails.
1789    ///
1790    /// # Examples
1791    /// ```
1792    /// # use bump_scope::{Bump, bump_vec};
1793    /// # let bump: Bump = Bump::new();
1794    /// let mut vec = bump_vec![try in &bump; 0, 1, 2, 3, 4]?;
1795    ///
1796    /// vec.try_extend_from_within_clone(2..)?;
1797    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1798    ///
1799    /// vec.try_extend_from_within_clone(..2)?;
1800    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1801    ///
1802    /// vec.try_extend_from_within_clone(4..8)?;
1803    /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1804    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1805    /// ```
1806    #[inline(always)]
1807    pub fn try_extend_from_within_clone<R>(&mut self, src: R) -> Result<(), AllocError>
1808    where
1809        T: Clone,
1810        R: RangeBounds<usize>,
1811    {
1812        self.generic_extend_from_within_clone(src)
1813    }
1814
1815    #[inline]
1816    pub(crate) fn generic_extend_from_within_clone<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1817    where
1818        T: Clone,
1819        R: RangeBounds<usize>,
1820    {
1821        let range = slice::range(src, ..self.len());
1822        let count = range.len();
1823
1824        self.generic_reserve(count)?;
1825
1826        if T::IS_ZST {
1827            unsafe {
1828                // We can materialize ZST's from nothing.
1829                #[expect(clippy::uninit_assumed_init)]
1830                let fake = ManuallyDrop::new(MaybeUninit::<T>::uninit().assume_init());
1831
1832                for _ in 0..count {
1833                    self.push_unchecked((*fake).clone());
1834                }
1835
1836                return Ok(());
1837            }
1838        }
1839
1840        // SAFETY:
1841        // - `slice::range` guarantees that the given range is valid for indexing self
1842        unsafe {
1843            let ptr = self.as_mut_ptr();
1844
1845            let mut src = ptr.add(range.start);
1846            let mut dst = ptr.add(self.len());
1847
1848            let src_end = src.add(count);
1849
1850            while src != src_end {
1851                dst.write((*src).clone());
1852
1853                src = src.add(1);
1854                dst = dst.add(1);
1855                self.inc_len(1);
1856            }
1857        }
1858
1859        Ok(())
1860    }
1861
1862    /// Reserves capacity for at least `additional` more elements to be inserted
1863    /// in the given `BumpVec<T>`. The collection may reserve more space to
1864    /// speculatively avoid frequent reallocations. After calling `reserve`,
1865    /// capacity will be greater than or equal to `self.len() + additional`.
1866    /// Does nothing if capacity is already sufficient.
1867    ///
1868    /// # Panics
1869    /// Panics if the allocation fails.
1870    ///
1871    /// # Examples
1872    /// ```
1873    /// # use bump_scope::{Bump, bump_vec};
1874    /// # let bump: Bump = Bump::new();
1875    /// let mut vec = bump_vec![in &bump; 1];
1876    /// vec.reserve(10);
1877    /// assert!(vec.capacity() >= 11);
1878    /// ```
1879    #[inline(always)]
1880    #[cfg(feature = "panic-on-alloc")]
1881    pub fn reserve(&mut self, additional: usize) {
1882        panic_on_error(self.generic_reserve(additional));
1883    }
1884
1885    /// Reserves capacity for at least `additional` more elements to be inserted
1886    /// in the given `BumpVec<T>`. The collection may reserve more space to
1887    /// speculatively avoid frequent reallocations. After calling `reserve`,
1888    /// capacity will be greater than or equal to `self.len() + additional`.
1889    /// Does nothing if capacity is already sufficient.
1890    ///
1891    /// # Errors
1892    /// Errors if the allocation fails.
1893    ///
1894    /// # Examples
1895    /// ```
1896    /// # use bump_scope::{Bump, bump_vec};
1897    /// # let bump: Bump = Bump::new();
1898    /// let mut vec = bump_vec![try in &bump; 1]?;
1899    /// vec.try_reserve(10)?;
1900    /// assert!(vec.capacity() >= 11);
1901    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1902    /// ```
1903    #[inline(always)]
1904    pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError> {
1905        self.generic_reserve(additional)
1906    }
1907
1908    #[inline]
1909    pub(crate) fn generic_reserve<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1910        if additional > (self.capacity() - self.len()) {
1911            self.generic_grow_amortized(additional)?;
1912        }
1913
1914        Ok(())
1915    }
1916
1917    /// Reserves the minimum capacity for at least `additional` more elements to
1918    /// be inserted in the given `BumpVec<T>`. Unlike [`reserve`], this will not
1919    /// deliberately over-allocate to speculatively avoid frequent allocations.
1920    /// After calling `reserve_exact`, capacity will be greater than or equal to
1921    /// `self.len() + additional`. Does nothing if the capacity is already
1922    /// sufficient.
1923    ///
1924    /// Note that the allocator may give the collection more space than it
1925    /// requests. Therefore, capacity cannot be relied upon to be precisely
1926    /// minimal. Prefer [`reserve`] if future insertions are expected.
1927    ///
1928    /// [`reserve`]: Self::reserve
1929    ///
1930    /// # Panics
1931    /// Panics if the allocation fails.
1932    ///
1933    /// # Examples
1934    /// ```
1935    /// # use bump_scope::{Bump, bump_vec};
1936    /// # let bump: Bump = Bump::new();
1937    /// let mut vec = bump_vec![in &bump; 1];
1938    /// vec.reserve_exact(10);
1939    /// assert!(vec.capacity() >= 11);
1940    /// ```
1941    #[inline(always)]
1942    #[cfg(feature = "panic-on-alloc")]
1943    pub fn reserve_exact(&mut self, additional: usize) {
1944        panic_on_error(self.generic_reserve_exact(additional));
1945    }
1946
1947    /// Reserves the minimum capacity for at least `additional` more elements to
1948    /// be inserted in the given `BumpVec<T>`. Unlike [`reserve`], this will not
1949    /// deliberately over-allocate to speculatively avoid frequent allocations.
1950    /// After calling `reserve_exact`, capacity will be greater than or equal to
1951    /// `self.len() + additional`. Does nothing if the capacity is already
1952    /// sufficient.
1953    ///
1954    /// Note that the allocator may give the collection more space than it
1955    /// requests. Therefore, capacity cannot be relied upon to be precisely
1956    /// minimal. Prefer [`reserve`] if future insertions are expected.
1957    ///
1958    /// [`reserve`]: Self::reserve
1959    ///
1960    /// # Errors
1961    /// Errors if the allocation fails.
1962    ///
1963    /// # Examples
1964    /// ```
1965    /// # use bump_scope::{Bump, bump_vec};
1966    /// # let bump: Bump = Bump::new();
1967    /// let mut vec = bump_vec![try in &bump; 1]?;
1968    /// vec.try_reserve_exact(10)?;
1969    /// assert!(vec.capacity() >= 11);
1970    /// # Ok::<(), bump_scope::alloc::AllocError>(())
1971    /// ```
1972    #[inline(always)]
1973    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), AllocError> {
1974        self.generic_reserve_exact(additional)
1975    }
1976
1977    #[inline]
1978    pub(crate) fn generic_reserve_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1979        if additional > (self.capacity() - self.len()) {
1980            self.generic_grow_exact(additional)?;
1981        }
1982
1983        Ok(())
1984    }
1985
1986    /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
1987    ///
1988    /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
1989    /// difference, with each additional slot filled with `value`.
1990    /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
1991    ///
1992    /// This method requires `T` to implement [`Clone`],
1993    /// in order to be able to clone the passed value.
1994    /// If you need more flexibility (or want to rely on [`Default`] instead of
1995    /// [`Clone`]), use [`resize_with`].
1996    /// If you only need to resize to a smaller size, use [`truncate`].
1997    ///
1998    /// [`resize_with`]: Self::resize_with
1999    /// [`truncate`]: Self::truncate
2000    ///
2001    /// # Panics
2002    /// Panics if the allocation fails.
2003    ///
2004    /// # Examples
2005    /// ```
2006    /// # use bump_scope::{Bump, bump_vec};
2007    /// # let bump: Bump = Bump::new();
2008    /// let mut vec = bump_vec![in &bump; "hello"];
2009    /// vec.resize(3, "world");
2010    /// assert_eq!(vec, ["hello", "world", "world"]);
2011    /// drop(vec);
2012    ///
2013    /// let mut vec = bump_vec![in &bump; 1, 2, 3, 4];
2014    /// vec.resize(2, 0);
2015    /// assert_eq!(vec, [1, 2]);
2016    /// ```
2017    #[inline(always)]
2018    #[cfg(feature = "panic-on-alloc")]
2019    pub fn resize(&mut self, new_len: usize, value: T)
2020    where
2021        T: Clone,
2022    {
2023        panic_on_error(self.generic_resize(new_len, value));
2024    }
2025
2026    /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
2027    ///
2028    /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
2029    /// difference, with each additional slot filled with `value`.
2030    /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
2031    ///
2032    /// This method requires `T` to implement [`Clone`],
2033    /// in order to be able to clone the passed value.
2034    /// If you need more flexibility (or want to rely on [`Default`] instead of
2035    /// [`Clone`]), use [`resize_with`].
2036    /// If you only need to resize to a smaller size, use [`truncate`].
2037    ///
2038    /// [`resize_with`]: Self::resize_with
2039    /// [`truncate`]: Self::truncate
2040    ///
2041    /// # Errors
2042    /// Errors if the allocation fails.
2043    ///
2044    /// # Examples
2045    /// ```
2046    /// # use bump_scope::{Bump, bump_vec};
2047    /// # let bump: Bump = Bump::new();
2048    /// let mut vec = bump_vec![try in &bump; "hello"]?;
2049    /// vec.try_resize(3, "world")?;
2050    /// assert_eq!(vec, ["hello", "world", "world"]);
2051    /// drop(vec);
2052    ///
2053    /// let mut vec = bump_vec![try in &bump; 1, 2, 3, 4]?;
2054    /// vec.try_resize(2, 0)?;
2055    /// assert_eq!(vec, [1, 2]);
2056    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2057    /// ```
2058    #[inline(always)]
2059    pub fn try_resize(&mut self, new_len: usize, value: T) -> Result<(), AllocError>
2060    where
2061        T: Clone,
2062    {
2063        self.generic_resize(new_len, value)
2064    }
2065
2066    #[inline]
2067    pub(crate) fn generic_resize<E: ErrorBehavior>(&mut self, new_len: usize, value: T) -> Result<(), E>
2068    where
2069        T: Clone,
2070    {
2071        let len = self.len();
2072
2073        if new_len > len {
2074            self.extend_with(new_len - len, value)
2075        } else {
2076            self.truncate(new_len);
2077            Ok(())
2078        }
2079    }
2080
2081    /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
2082    ///
2083    /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
2084    /// difference, with each additional slot filled with the result of
2085    /// calling the closure `f`. The return values from `f` will end up
2086    /// in the `BumpVec` in the order they have been generated.
2087    ///
2088    /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
2089    ///
2090    /// This method uses a closure to create new values on every push. If
2091    /// you'd rather [`Clone`] a given value, use [`BumpVec::resize`]. If you
2092    /// want to use the [`Default`] trait to generate values, you can
2093    /// pass [`Default::default`] as the second argument.
2094    ///
2095    /// # Panics
2096    /// Panics if the allocation fails.
2097    ///
2098    /// # Examples
2099    /// ```
2100    /// # use bump_scope::{Bump, bump_vec};
2101    /// # let bump: Bump = Bump::new();
2102    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
2103    /// vec.resize_with(5, Default::default);
2104    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
2105    /// drop(vec);
2106    ///
2107    /// let mut vec = bump_vec![in &bump];
2108    /// let mut p = 1;
2109    /// vec.resize_with(4, || { p *= 2; p });
2110    /// assert_eq!(vec, [2, 4, 8, 16]);
2111    /// ```
2112    #[inline(always)]
2113    #[cfg(feature = "panic-on-alloc")]
2114    pub fn resize_with<F>(&mut self, new_len: usize, f: F)
2115    where
2116        F: FnMut() -> T,
2117    {
2118        panic_on_error(self.generic_resize_with(new_len, f));
2119    }
2120
2121    /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
2122    ///
2123    /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
2124    /// difference, with each additional slot filled with the result of
2125    /// calling the closure `f`. The return values from `f` will end up
2126    /// in the `BumpVec` in the order they have been generated.
2127    ///
2128    /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
2129    ///
2130    /// This method uses a closure to create new values on every push. If
2131    /// you'd rather [`Clone`] a given value, use [`BumpVec::resize`]. If you
2132    /// want to use the [`Default`] trait to generate values, you can
2133    /// pass [`Default::default`] as the second argument.
2134    ///
2135    /// # Errors
2136    /// Errors if the allocation fails.
2137    ///
2138    /// # Examples
2139    /// ```
2140    /// # use bump_scope::{Bump, bump_vec};
2141    /// # let bump: Bump = Bump::new();
2142    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
2143    /// vec.try_resize_with(5, Default::default)?;
2144    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
2145    /// drop(vec);
2146    ///
2147    /// let mut vec = bump_vec![try in &bump]?;
2148    /// let mut p = 1;
2149    /// vec.try_resize_with(4, || { p *= 2; p })?;
2150    /// assert_eq!(vec, [2, 4, 8, 16]);
2151    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2152    /// ```
2153    #[inline(always)]
2154    pub fn try_resize_with<F>(&mut self, new_len: usize, f: F) -> Result<(), AllocError>
2155    where
2156        F: FnMut() -> T,
2157    {
2158        self.generic_resize_with(new_len, f)
2159    }
2160
2161    #[inline]
2162    pub(crate) fn generic_resize_with<E: ErrorBehavior, F>(&mut self, new_len: usize, f: F) -> Result<(), E>
2163    where
2164        F: FnMut() -> T,
2165    {
2166        let len = self.len();
2167        if new_len > len {
2168            unsafe { self.extend_trusted(iter::repeat_with(f).take(new_len - len)) }
2169        } else {
2170            self.truncate(new_len);
2171            Ok(())
2172        }
2173    }
2174
2175    /// Moves all the elements of `other` into `self`, leaving `other` empty.
2176    ///
2177    /// # Panics
2178    /// Panics if the allocation fails.
2179    ///
2180    /// # Examples
2181    /// ```
2182    /// # use bump_scope::{Bump, BumpVec};
2183    /// # let bump: Bump = Bump::new();
2184    /// let mut vec = BumpVec::new_in(&bump);
2185    ///
2186    /// // append by value
2187    /// vec.append([1, 2]);
2188    /// vec.append(vec![3, 4]);
2189    /// vec.append(bump.alloc_iter(5..=6));
2190    ///
2191    /// // append by mutable reference
2192    /// let mut other = vec![7, 8];
2193    /// vec.append(&mut other);
2194    ///
2195    /// assert_eq!(other, []);
2196    /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7, 8]);
2197    /// ```
2198    #[inline(always)]
2199    #[cfg(feature = "panic-on-alloc")]
2200    pub fn append(&mut self, other: impl OwnedSlice<Item = T>) {
2201        panic_on_error(self.generic_append(other));
2202    }
2203
2204    /// Moves all the elements of `other` into `self`, leaving `other` empty.
2205    ///
2206    /// # Errors
2207    /// Errors if the allocation fails.
2208    ///
2209    /// # Examples
2210    /// ```
2211    /// # use bump_scope::{Bump, BumpVec};
2212    /// # let bump: Bump = Bump::new();
2213    /// let mut vec = BumpVec::new_in(&bump);
2214    ///
2215    /// // append by value
2216    /// vec.try_append([1, 2])?;
2217    /// vec.try_append(vec![3, 4])?;
2218    /// vec.try_append(bump.alloc_iter(5..=6))?;
2219    ///
2220    /// // append by mutable reference
2221    /// let mut other = vec![7, 8];
2222    /// vec.try_append(&mut other)?;
2223    ///
2224    /// assert_eq!(other, []);
2225    /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7, 8]);
2226    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2227    /// ```
2228    #[inline(always)]
2229    pub fn try_append(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), AllocError> {
2230        self.generic_append(other)
2231    }
2232
2233    #[inline]
2234    pub(crate) fn generic_append<E: ErrorBehavior>(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), E> {
2235        unsafe {
2236            let mut owned_slice = other.into_take_owned_slice();
2237
2238            let slice = NonNull::from(owned_slice.owned_slice_ref());
2239            self.generic_reserve(slice.len())?;
2240
2241            let src = slice.cast::<T>().as_ptr();
2242            let dst = self.as_mut_ptr().add(self.len());
2243            ptr::copy_nonoverlapping(src, dst, slice.len());
2244
2245            owned_slice.take_owned_slice();
2246            self.inc_len(slice.len());
2247            Ok(())
2248        }
2249    }
2250
2251    /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2252    ///
2253    /// Compared to `from_iter_in(into_iter().map(f), ...)` this method has the advantage that it can reuse the existing allocation.
2254    ///
2255    /// # Panics
2256    /// Panics if the allocation fails. An allocation only occurs when the alignment or size of `U` is greater than that of `T`.
2257    ///
2258    /// # Examples
2259    /// Mapping to a type with an equal alignment and size (no allocation):
2260    /// ```
2261    /// # use bump_scope::{Bump, BumpVec};
2262    /// # use core::num::NonZero;
2263    /// # let bump: Bump = Bump::new();
2264    /// let a = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2265    /// let b = a.map(NonZero::new);
2266    /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2267    /// ```
2268    ///
2269    /// Mapping to a type with a smaller alignment and size (no allocation, capacity may grow):
2270    /// ```
2271    /// # use bump_scope::{Bump, BumpVec};
2272    /// # let bump: Bump = Bump::new();
2273    /// let vec_a: BumpVec<u32, _> = BumpVec::from_iter_in([1, 2, 3, 4], &bump);
2274    /// assert_eq!(vec_a.capacity(), 4);
2275    /// assert_eq!(bump.stats().allocated(), 4 * 4);
2276    ///
2277    /// let vec_b: BumpVec<u16, _> = vec_a.map(|i| i as u16);
2278    /// assert_eq!(vec_b.capacity(), 8);
2279    /// assert_eq!(bump.stats().allocated(), 4 * 4);
2280    /// ```
2281    ///
2282    /// Mapping to a type with a higher alignment or size is equivalent to
2283    /// calling `from_iter_in(into_iter().map(f), ...)`:
2284    /// ```
2285    /// # use bump_scope::{Bump, BumpVec};
2286    /// # let bump: Bump = Bump::new();
2287    /// let vec_a: BumpVec<u16, _> = BumpVec::from_iter_in([1, 2, 3, 4], &bump);
2288    /// assert_eq!(vec_a.capacity(), 4);
2289    /// assert_eq!(bump.stats().allocated(), 4 * 2);
2290    ///
2291    /// let vec_b: BumpVec<u32, _> = vec_a.map(|i| i as u32);
2292    /// assert_eq!(vec_b.capacity(), 4);
2293    /// assert_eq!(bump.stats().allocated(), 4 * 2 + 4 * 4);
2294    /// ```
2295    #[inline(always)]
2296    #[cfg(feature = "panic-on-alloc")]
2297    pub fn map<U>(self, f: impl FnMut(T) -> U) -> BumpVec<U, A>
2298    where
2299        A: Clone,
2300    {
2301        panic_on_error(self.generic_map(f))
2302    }
2303
2304    /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2305    ///
2306    /// Compared to `try_from_iter_in(into_iter().map(f), ...)` this method has the advantage that it can reuse the existing allocation.
2307    ///
2308    /// # Errors
2309    /// Errors if the allocation fails. An allocation can only occurs when the alignment or size of `U` is greater than that of `T`.
2310    ///
2311    /// # Examples
2312    /// Mapping to a type with an equal alignment and size (no allocation):
2313    /// ```
2314    /// # use bump_scope::{Bump, BumpVec};
2315    /// # use core::num::NonZero;
2316    /// # let bump: Bump = Bump::new();
2317    /// let a = BumpVec::try_from_iter_exact_in([0, 1, 2], &bump)?;
2318    /// let b = a.try_map(NonZero::new)?;
2319    /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2320    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2321    /// ```
2322    ///
2323    /// Mapping to a type with a smaller alignment and size (no allocation, capacity may grow):
2324    /// ```
2325    /// # use bump_scope::{Bump, BumpVec};
2326    /// # let bump: Bump = Bump::new();
2327    /// let vec_a: BumpVec<u32, _> = BumpVec::try_from_iter_in([1, 2, 3, 4], &bump)?;
2328    /// assert_eq!(vec_a.capacity(), 4);
2329    /// assert_eq!(bump.stats().allocated(), 4 * 4);
2330    ///
2331    /// let vec_b: BumpVec<u16, _> = vec_a.try_map(|i| i as u16)?;
2332    /// assert_eq!(vec_b.capacity(), 8);
2333    /// assert_eq!(bump.stats().allocated(), 4 * 4);
2334    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2335    /// ```
2336    ///
2337    /// Mapping to a type with a higher alignment or size is equivalent to
2338    /// calling `try_from_iter_in(into_iter().map(f), ...)`:
2339    /// ```
2340    /// # use bump_scope::{Bump, BumpVec};
2341    /// # let bump: Bump = Bump::new();
2342    /// let vec_a: BumpVec<u16, _> = BumpVec::try_from_iter_in([1, 2, 3, 4], &bump)?;
2343    /// assert_eq!(vec_a.capacity(), 4);
2344    /// assert_eq!(bump.stats().allocated(), 4 * 2);
2345    ///
2346    /// let vec_b: BumpVec<u32, _> = vec_a.try_map(|i| i as u32)?;
2347    /// assert_eq!(vec_b.capacity(), 4);
2348    /// assert_eq!(bump.stats().allocated(), 4 * 2 + 4 * 4);
2349    /// # Ok::<(), bump_scope::alloc::AllocError>(())
2350    /// ```
2351    #[inline(always)]
2352    pub fn try_map<U>(self, f: impl FnMut(T) -> U) -> Result<BumpVec<U, A>, AllocError>
2353    where
2354        A: Clone,
2355    {
2356        self.generic_map(f)
2357    }
2358
2359    #[inline]
2360    fn generic_map<B: ErrorBehavior, U>(self, mut f: impl FnMut(T) -> U) -> Result<BumpVec<U, A>, B>
2361    where
2362        A: Clone,
2363    {
2364        if !T::IS_ZST && !U::IS_ZST && T::ALIGN >= U::ALIGN && T::SIZE >= U::SIZE {
2365            struct DropGuard<T, U, A: BumpAllocatorTyped> {
2366                ptr: NonNull<T>,
2367                cap: usize,
2368                end: *mut T,
2369                src: *mut T,
2370                dst: *mut U,
2371                allocator: A,
2372            }
2373
2374            impl<T, U, A: BumpAllocatorTyped> DropGuard<T, U, A> {
2375                fn into_allocator(self) -> A {
2376                    destructure!(let Self { allocator } = self);
2377                    allocator
2378                }
2379            }
2380
2381            impl<T, U, A: BumpAllocatorTyped> Drop for DropGuard<T, U, A> {
2382                fn drop(&mut self) {
2383                    unsafe {
2384                        // drop `T`s
2385                        let drop_ptr = self.src.add(1);
2386                        let drop_len = pointer::offset_from_unsigned(self.end, drop_ptr);
2387                        ptr::slice_from_raw_parts_mut(drop_ptr, drop_len).drop_in_place();
2388
2389                        // drop `U`s
2390                        let drop_ptr = self.ptr.as_ptr().cast::<U>();
2391                        let drop_len = pointer::offset_from_unsigned(self.dst, drop_ptr);
2392                        ptr::slice_from_raw_parts_mut(drop_ptr, drop_len).drop_in_place();
2393
2394                        if T::IS_ZST || self.cap == 0 {
2395                            return;
2396                        }
2397
2398                        let layout = Layout::from_size_align_unchecked(self.cap * T::SIZE, T::ALIGN);
2399                        self.allocator.deallocate(self.ptr.cast(), layout);
2400                    }
2401                }
2402            }
2403
2404            destructure!(let Self { fixed, allocator } = self);
2405            let cap = fixed.capacity();
2406            let slice = unsafe { fixed.cook() }.into_boxed_slice().into_raw();
2407            let ptr = slice.cast::<T>();
2408            let len = slice.len();
2409
2410            unsafe {
2411                let mut guard = DropGuard::<T, U, A> {
2412                    ptr,
2413                    cap,
2414                    end: ptr.as_ptr().add(len),
2415                    src: ptr.as_ptr(),
2416                    dst: ptr.as_ptr().cast(),
2417                    allocator,
2418                };
2419
2420                while guard.src < guard.end {
2421                    let src_value = guard.src.read();
2422                    let dst_value = f(src_value);
2423                    guard.dst.write(dst_value);
2424                    guard.src = guard.src.add(1);
2425                    guard.dst = guard.dst.add(1);
2426                }
2427
2428                let allocator = guard.into_allocator();
2429                let new_cap = (cap * T::SIZE) / U::SIZE;
2430
2431                Ok(BumpVec {
2432                    fixed: RawFixedBumpVec::from_raw_parts(NonNull::slice_from_raw_parts(ptr.cast(), len), new_cap),
2433                    allocator,
2434                })
2435            }
2436        } else {
2437            // fallback
2438            let allocator = self.allocator.clone();
2439            Ok(BumpVec::generic_from_iter_exact_in(self.into_iter().map(f), allocator)?)
2440        }
2441    }
2442
2443    /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2444    ///
2445    /// This function only compiles when `U`s size and alignment is less or equal to `T`'s or if `U` has a size of 0.
2446    ///
2447    /// # Examples
2448    /// Mapping to a type with an equal alignment and size:
2449    /// ```
2450    /// # use bump_scope::{Bump, BumpVec};
2451    /// # use core::num::NonZero;
2452    /// # let bump: Bump = Bump::new();
2453    /// let a = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2454    /// let b = a.map_in_place(NonZero::new);
2455    /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2456    /// ```
2457    ///
2458    /// Mapping to a type with a smaller alignment and size:
2459    /// ```
2460    /// # use bump_scope::{Bump, BumpVec};
2461    /// # let bump: Bump = Bump::new();
2462    /// let a: BumpVec<u32, _> = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2463    /// assert_eq!(a.capacity(), 3);
2464    ///
2465    /// let b: BumpVec<u16, _> = a.map_in_place(|i| i as u16);
2466    /// assert_eq!(b.capacity(), 6);
2467    ///
2468    /// assert_eq!(b, [0, 1, 2]);
2469    /// ```
2470    ///
2471    /// Mapping to a type with a greater alignment or size won't compile:
2472    /// ```compile_fail,E0080
2473    /// # use bump_scope::{Bump, BumpVec};
2474    /// # let bump: Bump = Bump::new();
2475    /// let a: BumpVec<u16, _> = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2476    /// let b: BumpVec<u32, _> = a.map_in_place(|i| i as u32);
2477    /// # _ = b;
2478    /// ```
2479    ///
2480    /// Mapping to a type with a greater size won't compile:
2481    /// ```compile_fail,E0080
2482    /// # use bump_scope::{Bump, BumpVec};
2483    /// # let bump: Bump = Bump::new();
2484    /// let a: BumpVec<u32, _> = BumpVec::from_iter_exact_in([42], &bump);
2485    /// let b: BumpVec<[u32; 2], _> = a.map_in_place(|i| [i; 2]);
2486    /// # _ = b;
2487    /// ```
2488    pub fn map_in_place<U>(self, f: impl FnMut(T) -> U) -> BumpVec<U, A> {
2489        destructure!(let Self { fixed, allocator } = self);
2490
2491        // `FixedBumpVec::map_in_place` handles dropping `T`s and `U`s on panic.
2492        // What is left to do is deallocating the memory.
2493
2494        struct DropGuard<T, A: BumpAllocatorTyped> {
2495            ptr: NonNull<T>,
2496            cap: usize,
2497            allocator: A,
2498        }
2499
2500        impl<T, A: BumpAllocatorTyped> DropGuard<T, A> {
2501            fn into_allocator(self) -> A {
2502                destructure!(let Self { allocator } = self);
2503                allocator
2504            }
2505        }
2506
2507        impl<T, A: BumpAllocatorTyped> Drop for DropGuard<T, A> {
2508            fn drop(&mut self) {
2509                unsafe {
2510                    if T::IS_ZST || self.cap == 0 {
2511                        return;
2512                    }
2513
2514                    let layout = Layout::from_size_align_unchecked(self.cap * T::SIZE, T::ALIGN);
2515                    self.allocator.deallocate(self.ptr.cast(), layout);
2516                }
2517            }
2518        }
2519
2520        let guard = DropGuard::<T, _> {
2521            ptr: fixed.as_non_null(),
2522            cap: fixed.capacity(),
2523            allocator,
2524        };
2525
2526        let fixed = unsafe { RawFixedBumpVec::from_cooked(fixed.cook().map_in_place(f)) };
2527        let allocator = guard.into_allocator();
2528
2529        BumpVec { fixed, allocator }
2530    }
2531
2532    /// Creates a splicing iterator that replaces the specified range in the vector
2533    /// with the given `replace_with` iterator and yields the removed items.
2534    /// `replace_with` does not need to be the same length as `range`.
2535    ///
2536    /// `range` is removed even if the iterator is not consumed until the end.
2537    ///
2538    /// It is unspecified how many elements are removed from the vector
2539    /// if the `Splice` value is leaked.
2540    ///
2541    /// The input iterator `replace_with` is only consumed when the `Splice` value is dropped.
2542    ///
2543    /// This is optimal if:
2544    ///
2545    /// * The tail (elements in the vector after `range`) is empty,
2546    /// * or `replace_with` yields fewer or equal elements than `range`’s length
2547    /// * or the lower bound of its `size_hint()` is exact.
2548    ///
2549    /// Otherwise, a temporary vector is allocated and the tail is moved twice.
2550    ///
2551    /// # Panics
2552    ///
2553    /// Panics if the starting point is greater than the end point or if
2554    /// the end point is greater than the length of the vector.
2555    ///
2556    /// # Examples
2557    ///
2558    /// ```
2559    /// # use bump_scope::{Bump, bump_vec};
2560    /// # let bump: Bump = Bump::new();
2561    /// let mut v = bump_vec![in &bump; 1, 2, 3, 4];
2562    /// let new = [7, 8, 9];
2563    /// let u = bump.alloc_iter(v.splice(1..3, new));
2564    /// assert_eq!(v, [1, 7, 8, 9, 4]);
2565    /// assert_eq!(u, [2, 3]);
2566    /// ```
2567    #[cfg(feature = "panic-on-alloc")]
2568    #[inline]
2569    pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A>
2570    where
2571        R: RangeBounds<usize>,
2572        I: IntoIterator<Item = T>,
2573    {
2574        // Memory safety
2575        //
2576        // When the Drain is first created, it shortens the length of
2577        // the source vector to make sure no uninitialized or moved-from elements
2578        // are accessible at all if the Drain's destructor never gets to run.
2579        //
2580        // Drain will ptr::read out the values to remove.
2581        // When finished, remaining tail of the vec is copied back to cover
2582        // the hole, and the vector length is restored to the new length.
2583
2584        use core::ops::Range;
2585        let len = self.len();
2586        let Range { start, end } = slice::range(range, ..len);
2587
2588        let drain = unsafe {
2589            // set self.vec length's to start, to be safe in case Drain is leaked
2590            self.set_len(start);
2591            let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start);
2592
2593            Drain {
2594                tail_start: end,
2595                tail_len: len - end,
2596                iter: range_slice.iter(),
2597                vec: NonNull::from(self),
2598            }
2599        };
2600
2601        Splice {
2602            drain,
2603            replace_with: replace_with.into_iter(),
2604        }
2605    }
2606
2607    /// Like [`reserve`] but allows you to provide a different `len`.
2608    ///
2609    /// This helps with algorithms from the standard library that make use of
2610    /// `RawVec::reserve` which behaves the same.
2611    ///
2612    /// # Safety
2613    ///
2614    /// - `len` must be less than or equal to `self.capacity()`
2615    ///
2616    /// [`reserve`]: Self::reserve
2617    #[inline(always)]
2618    #[cfg(feature = "panic-on-alloc")]
2619    pub(crate) unsafe fn buf_reserve(&mut self, len: usize, additional: usize) {
2620        unsafe {
2621            if additional > (self.capacity() - len) {
2622                panic_on_error(self.generic_grow_amortized_buf(len, additional));
2623            }
2624        }
2625    }
2626
2627    /// Extend the vector by `n` clones of value.
2628    fn extend_with<B: ErrorBehavior>(&mut self, n: usize, value: T) -> Result<(), B>
2629    where
2630        T: Clone,
2631    {
2632        self.generic_reserve(n)?;
2633        unsafe {
2634            self.fixed.cook_mut().extend_with_unchecked(n, value);
2635        }
2636        Ok(())
2637    }
2638
2639    #[inline(always)]
2640    unsafe fn extend_by_copy_nonoverlapping<E: ErrorBehavior>(&mut self, other: *const [T]) -> Result<(), E> {
2641        unsafe {
2642            let len = other.len();
2643            self.generic_reserve(len)?;
2644
2645            let src = other.cast::<T>();
2646            let dst = self.as_mut_ptr().add(self.len());
2647            ptr::copy_nonoverlapping(src, dst, len);
2648
2649            self.inc_len(len);
2650            Ok(())
2651        }
2652    }
2653
2654    #[inline]
2655    fn generic_reserve_one<E: ErrorBehavior>(&mut self) -> Result<(), E> {
2656        if self.capacity() == self.len() {
2657            self.generic_grow_amortized::<E>(1)?;
2658        }
2659
2660        Ok(())
2661    }
2662
2663    #[cold]
2664    #[inline(never)]
2665    fn generic_grow_amortized<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2666        if T::IS_ZST {
2667            // This function is only called after we checked that the current capacity is not
2668            // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2669            return Err(E::capacity_overflow());
2670        }
2671
2672        let Some(required_cap) = self.len().checked_add(additional) else {
2673            return Err(E::capacity_overflow())?;
2674        };
2675
2676        let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
2677        let new_cap = new_cap.max(min_non_zero_cap(T::SIZE));
2678
2679        unsafe { self.generic_grow_to(new_cap) }
2680    }
2681
2682    /// Like [`reserve`] but allows you to provide a different `len`.
2683    ///
2684    /// This is only used for [`buf_reserve`], read its documentation for more.
2685    ///
2686    /// # Safety
2687    ///
2688    /// - `len` must be less than or equal to `self.capacity()`
2689    ///
2690    /// [`reserve`]: Self::reserve
2691    /// [`buf_reserve`]: Self::buf_reserve
2692    #[cold]
2693    #[inline(never)]
2694    #[cfg(feature = "panic-on-alloc")]
2695    unsafe fn generic_grow_amortized_buf<E: ErrorBehavior>(&mut self, len: usize, additional: usize) -> Result<(), E> {
2696        if T::IS_ZST {
2697            // This function is only called after we checked that the current capacity is not
2698            // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2699            return Err(E::capacity_overflow());
2700        }
2701
2702        let Some(required_cap) = len.checked_add(additional) else {
2703            return Err(E::capacity_overflow())?;
2704        };
2705
2706        // This guarantees exponential growth. The doubling cannot overflow
2707        // because `capacity <= isize::MAX` and the type of `capacity` is usize;
2708        let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));
2709
2710        unsafe { self.generic_grow_to(new_cap) }
2711    }
2712
2713    #[cold]
2714    #[inline(never)]
2715    fn generic_grow_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2716        if T::IS_ZST {
2717            // This function is only called after we checked that the current capacity is not
2718            // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2719            return Err(E::capacity_overflow());
2720        }
2721
2722        let Some(required_cap) = self.len().checked_add(additional) else {
2723            return Err(E::capacity_overflow())?;
2724        };
2725
2726        unsafe { self.generic_grow_to(required_cap) }
2727    }
2728
2729    /// # Safety
2730    ///
2731    /// `new_capacity` must be greater than the current capacity.
2732    unsafe fn generic_grow_to<E: ErrorBehavior>(&mut self, new_capacity: usize) -> Result<(), E> {
2733        unsafe {
2734            let new_cap = new_capacity;
2735
2736            if self.capacity() == 0 {
2737                self.fixed = RawFixedBumpVec::allocate(&self.allocator, new_cap)?;
2738                return Ok(());
2739            }
2740
2741            let old_ptr = self.as_non_null().cast();
2742
2743            let old_size = self.capacity() * T::SIZE; // we already allocated that amount so this can't overflow
2744            let Some(new_size) = new_cap.checked_mul(T::SIZE) else {
2745                return Err(E::capacity_overflow());
2746            };
2747
2748            let old_layout = Layout::from_size_align_unchecked(old_size, T::ALIGN);
2749            let Ok(new_layout) = Layout::from_size_align(new_size, T::ALIGN) else {
2750                return Err(E::capacity_overflow());
2751            };
2752
2753            let new_ptr = match self.allocator.grow(old_ptr, old_layout, new_layout) {
2754                Ok(ok) => ok.cast(),
2755                Err(_) => return Err(E::allocation(new_layout)),
2756            };
2757
2758            self.fixed.set_ptr(new_ptr);
2759            self.fixed.set_cap(new_cap);
2760
2761            Ok(())
2762        }
2763    }
2764
2765    /// Shrinks the capacity of the vector as much as possible.
2766    ///
2767    /// Shrinking will only take place if this vector owns the last allocation.
2768    ///
2769    /// # Examples
2770    /// ```
2771    /// # use bump_scope::{Bump, BumpVec, bump_vec};
2772    /// # let bump: Bump = Bump::new();
2773    /// let mut vec: BumpVec<u8, _> = bump_vec![in &bump; 1, 2, 3];
2774    /// assert!(vec.len() == 3);
2775    /// assert!(vec.capacity() == 3);
2776    /// assert_eq!(bump.stats().allocated(), 3);
2777    ///
2778    /// vec.pop();
2779    /// vec.shrink_to_fit();
2780    /// assert!(vec.len() == 2);
2781    /// assert!(vec.capacity() == 2);
2782    /// assert_eq!(bump.stats().allocated(), 2);
2783    ///
2784    /// // allocate something so the vec is no longer the last allocation
2785    /// bump.alloc::<u8>(123);
2786    ///
2787    /// // now shrinking won't take place, capacity stays the same
2788    /// vec.pop();
2789    /// vec.shrink_to_fit();
2790    /// assert!(vec.len() == 1);
2791    /// assert!(vec.capacity() == 2);
2792    /// ```
2793    pub fn shrink_to_fit(&mut self) {
2794        let Self { fixed, allocator } = self;
2795
2796        let old_ptr = fixed.as_non_null();
2797        let old_cap = fixed.capacity();
2798        let new_cap = fixed.len();
2799
2800        if T::IS_ZST || old_cap == 0 {
2801            return;
2802        }
2803
2804        unsafe {
2805            if let Some(new_ptr) = allocator.shrink_slice(old_ptr, old_cap, new_cap) {
2806                fixed.set_ptr(new_ptr);
2807                fixed.set_cap(new_cap);
2808            }
2809        }
2810    }
2811
2812    /// # Safety
2813    ///
2814    /// `iterator` must satisfy the invariants of nightly's `TrustedLen`.
2815    unsafe fn extend_trusted<B: ErrorBehavior>(&mut self, iterator: impl Iterator<Item = T>) -> Result<(), B> {
2816        unsafe {
2817            let (low, high) = iterator.size_hint();
2818            if let Some(additional) = high {
2819                debug_assert_eq!(
2820                    low,
2821                    additional,
2822                    "TrustedLen iterator's size hint is not exact: {:?}",
2823                    (low, high)
2824                );
2825
2826                self.generic_reserve(additional)?;
2827
2828                let ptr = self.as_mut_ptr();
2829                let mut local_len = self.fixed.set_len_on_drop();
2830
2831                iterator.for_each(move |element| {
2832                    let dst = ptr.add(local_len.current_len());
2833
2834                    ptr::write(dst, element);
2835                    // Since the loop executes user code which can panic we have to update
2836                    // the length every step to correctly drop what we've written.
2837                    // NB can't overflow since we would have had to alloc the address space
2838                    local_len.increment_len(1);
2839                });
2840
2841                Ok(())
2842            } else {
2843                // Per TrustedLen contract a `None` upper bound means that the iterator length
2844                // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
2845                // Since the other branch already panics eagerly (via `reserve()`) we do the same here.
2846                // This avoids additional codegen for a fallback code path which would eventually
2847                // panic anyway.
2848                Err(B::capacity_overflow())
2849            }
2850        }
2851    }
2852
2853    /// Retains only the elements specified by the predicate, passing a mutable reference to it.
2854    ///
2855    /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
2856    /// This method operates in place, visiting each element exactly once in the
2857    /// original order, and preserves the order of the retained elements.
2858    ///
2859    /// # Examples
2860    ///
2861    /// ```
2862    /// # use bump_scope::{Bump, bump_vec};
2863    /// # let bump: Bump = Bump::new();
2864    /// #
2865    /// let mut vec = bump_vec![in &bump; 1, 2, 3, 4];
2866    ///
2867    /// vec.retain(|x| if *x <= 3 {
2868    ///     *x += 1;
2869    ///     true
2870    /// } else {
2871    ///     false
2872    /// });
2873    ///
2874    /// assert_eq!(vec, [2, 3, 4]);
2875    /// ```
2876    #[expect(clippy::pedantic)]
2877    pub fn retain<F>(&mut self, f: F)
2878    where
2879        F: FnMut(&mut T) -> bool,
2880    {
2881        unsafe { self.fixed.cook_mut() }.retain(f)
2882    }
2883
2884    /// Removes the specified range from the vector in bulk, returning all
2885    /// removed elements as an iterator. If the iterator is dropped before
2886    /// being fully consumed, it drops the remaining removed elements.
2887    ///
2888    /// The returned iterator keeps a mutable borrow on the vector to optimize
2889    /// its implementation.
2890    ///
2891    /// # Panics
2892    ///
2893    /// Panics if the starting point is greater than the end point or if
2894    /// the end point is greater than the length of the vector.
2895    ///
2896    /// # Leaking
2897    ///
2898    /// If the returned iterator goes out of scope without being dropped (due to
2899    /// [`mem::forget`](core::mem::forget), for example), the vector may have lost and leaked
2900    /// elements arbitrarily, including elements outside the range.
2901    ///
2902    /// # Examples
2903    ///
2904    /// ```
2905    /// # use bump_scope::{Bump, bump_vec};
2906    /// # let bump: Bump = Bump::new();
2907    /// let mut v = bump_vec![in &bump; 1, 2, 3];
2908    /// let u = bump.alloc_iter(v.drain(1..));
2909    /// assert_eq!(v, [1]);
2910    /// assert_eq!(u, [2, 3]);
2911    ///
2912    /// // A full range clears the vector, like `clear()` does
2913    /// v.drain(..);
2914    /// assert_eq!(v, []);
2915    /// ```
2916    pub fn drain<R>(&mut self, range: R) -> owned_slice::Drain<'_, T>
2917    where
2918        R: RangeBounds<usize>,
2919    {
2920        unsafe { self.fixed.cook_mut() }.drain(range)
2921    }
2922
2923    /// Creates an iterator which uses a closure to determine if an element should be removed.
2924    ///
2925    /// If the closure returns true, then the element is removed and yielded.
2926    /// If the closure returns false, the element will remain in the vector and will not be yielded
2927    /// by the iterator.
2928    ///
2929    /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
2930    /// or the iteration short-circuits, then the remaining elements will be retained.
2931    /// Use [`retain`] with a negated predicate if you do not need the returned iterator.
2932    ///
2933    /// Using this method is equivalent to the following code:
2934    ///
2935    /// ```
2936    /// # use bump_scope::{Bump, bump_vec};
2937    /// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 };
2938    /// # let bump: Bump = Bump::new();
2939    /// # let mut vec = bump_vec![in &bump; 1, 2, 3, 4, 5, 6];
2940    /// let mut i = 0;
2941    /// while i < vec.len() {
2942    ///     if some_predicate(&mut vec[i]) {
2943    ///         let val = vec.remove(i);
2944    ///         // your code here
2945    ///         # _ = val;
2946    ///     } else {
2947    ///         i += 1;
2948    ///     }
2949    /// }
2950    ///
2951    /// # assert_eq!(vec, [1, 4, 5]);
2952    /// ```
2953    ///
2954    /// But `extract_if` is easier to use. `extract_if` is also more efficient,
2955    /// because it can backshift the elements of the array in bulk.
2956    ///
2957    /// Note that `extract_if` also lets you mutate every element in the filter closure,
2958    /// regardless of whether you choose to keep or remove it.
2959    ///
2960    /// # Examples
2961    ///
2962    /// Splitting an array into evens and odds, reusing the original allocation:
2963    ///
2964    /// ```
2965    /// # use bump_scope::{Bump, bump_vec};
2966    /// # let bump: Bump = Bump::new();
2967    /// let mut numbers = bump_vec![in &bump; 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15];
2968    ///
2969    /// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
2970    /// let odds = numbers;
2971    ///
2972    /// assert_eq!(evens, [2, 4, 6, 8, 14]);
2973    /// assert_eq!(odds, [1, 3, 5, 9, 11, 13, 15]);
2974    /// ```
2975    ///
2976    /// [`retain`]: Self::retain
2977    pub fn extract_if<F>(&mut self, filter: F) -> owned_slice::ExtractIf<'_, T, F>
2978    where
2979        F: FnMut(&mut T) -> bool,
2980    {
2981        unsafe { self.fixed.cook_mut() }.extract_if(filter)
2982    }
2983
2984    /// Removes consecutive repeated elements in the vector according to the
2985    /// [`PartialEq`] trait implementation.
2986    ///
2987    /// If the vector is sorted, this removes all duplicates.
2988    ///
2989    /// # Examples
2990    ///
2991    /// ```
2992    /// # use bump_scope::{Bump, bump_vec};
2993    /// # let bump: Bump = Bump::new();
2994    /// let mut vec = bump_vec![in &bump; 1, 2, 2, 3, 2];
2995    ///
2996    /// vec.dedup();
2997    ///
2998    /// assert_eq!(vec, [1, 2, 3, 2]);
2999    /// ```
3000    #[inline]
3001    pub fn dedup(&mut self)
3002    where
3003        T: PartialEq,
3004    {
3005        unsafe { self.fixed.cook_mut() }.dedup();
3006    }
3007
3008    /// Removes all but the first of consecutive elements in the vector that resolve to the same
3009    /// key.
3010    ///
3011    /// If the vector is sorted, this removes all duplicates.
3012    ///
3013    /// # Examples
3014    ///
3015    /// ```
3016    /// # use bump_scope::{Bump, bump_vec};
3017    /// # let bump: Bump = Bump::new();
3018    /// let mut vec = bump_vec![in &bump; 10, 20, 21, 30, 20];
3019    ///
3020    /// vec.dedup_by_key(|i| *i / 10);
3021    ///
3022    /// assert_eq!(vec, [10, 20, 30, 20]);
3023    /// ```
3024    #[inline]
3025    pub fn dedup_by_key<F, K>(&mut self, key: F)
3026    where
3027        F: FnMut(&mut T) -> K,
3028        K: PartialEq,
3029    {
3030        unsafe { self.fixed.cook_mut() }.dedup_by_key(key);
3031    }
3032
3033    /// Removes all but the first of consecutive elements in the vector satisfying a given equality
3034    /// relation.
3035    ///
3036    /// The `same_bucket` function is passed references to two elements from the vector and
3037    /// must determine if the elements compare equal. The elements are passed in opposite order
3038    /// from their order in the vector, so if `same_bucket(a, b)` returns `true`, `a` is removed.
3039    ///
3040    /// If the vector is sorted, this removes all duplicates.
3041    ///
3042    /// # Examples
3043    ///
3044    /// ```
3045    /// # use bump_scope::{Bump, bump_vec};
3046    /// # let bump: Bump = Bump::new();
3047    /// let mut vec = bump_vec![in &bump; "foo", "bar", "Bar", "baz", "bar"];
3048    ///
3049    /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
3050    ///
3051    /// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
3052    /// ```
3053    pub fn dedup_by<F>(&mut self, same_bucket: F)
3054    where
3055        F: FnMut(&mut T, &mut T) -> bool,
3056    {
3057        unsafe { self.fixed.cook_mut() }.dedup_by(same_bucket);
3058    }
3059
3060    /// Returns the remaining spare capacity of the vector as a slice of
3061    /// `MaybeUninit<T>`.
3062    ///
3063    /// The returned slice can be used to fill the vector with data (e.g. by
3064    /// reading from a file) before marking the data as initialized using the
3065    /// [`set_len`] method.
3066    ///
3067    /// [`set_len`]: Self::set_len
3068    ///
3069    /// # Examples
3070    ///
3071    /// ```
3072    /// # use bump_scope::{Bump, BumpVec};
3073    /// # let bump: Bump = Bump::new();
3074    /// // Allocate vector big enough for 10 elements.
3075    /// let mut v = BumpVec::with_capacity_in(10, &bump);
3076    ///
3077    /// // Fill in the first 3 elements.
3078    /// let uninit = v.spare_capacity_mut();
3079    /// uninit[0].write(0);
3080    /// uninit[1].write(1);
3081    /// uninit[2].write(2);
3082    ///
3083    /// // Mark the first 3 elements of the vector as being initialized.
3084    /// unsafe {
3085    ///     v.set_len(3);
3086    /// }
3087    ///
3088    /// assert_eq!(&v, &[0, 1, 2]);
3089    /// ```
3090    #[inline]
3091    pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
3092        // Note:
3093        // This method is not implemented in terms of `split_at_spare_mut`,
3094        // to prevent invalidation of pointers to the buffer.
3095        unsafe {
3096            slice::from_raw_parts_mut(
3097                self.as_mut_ptr().add(self.len()).cast::<MaybeUninit<T>>(),
3098                self.capacity() - self.len(),
3099            )
3100        }
3101    }
3102
3103    /// Returns vector content as a slice of `T`, along with the remaining spare
3104    /// capacity of the vector as a slice of `MaybeUninit<T>`.
3105    ///
3106    /// The returned spare capacity slice can be used to fill the vector with data
3107    /// (e.g. by reading from a file) before marking the data as initialized using
3108    /// the [`set_len`] method.
3109    ///
3110    /// [`set_len`]: Self::set_len
3111    ///
3112    /// Note that this is a low-level API, which should be used with care for
3113    /// optimization purposes. If you need to append data to a `BumpVec`
3114    /// you can use [`push`], [`extend`], `extend_from_slice`[`_copy`](BumpVec::extend_from_slice_copy)`/`[`_clone`](BumpVec::extend_from_within_clone),
3115    /// `extend_from_within`[`_copy`](BumpVec::extend_from_within_copy)`/`[`_clone`](BumpVec::extend_from_within_clone), [`insert`], [`resize`] or
3116    /// [`resize_with`], depending on your exact needs.
3117    ///
3118    /// [`push`]: Self::push
3119    /// [`extend`]: Self::extend
3120    /// [`insert`]: Self::insert
3121    /// [`append`]: Self::append
3122    /// [`resize`]: Self::resize
3123    /// [`resize_with`]: Self::resize_with
3124    #[inline]
3125    pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
3126        let ptr = self.as_mut_ptr();
3127
3128        // SAFETY:
3129        // - `ptr` is guaranteed to be valid for `self.len` elements
3130        // - but the allocation extends out to `self.buf.capacity()` elements, possibly
3131        // uninitialized
3132        let spare_ptr = unsafe { ptr.add(self.len()) };
3133        let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
3134        let spare_len = self.capacity() - self.len();
3135
3136        // SAFETY:
3137        // - `ptr` is guaranteed to be valid for `self.len` elements
3138        // - `spare_ptr` is pointing one element past the buffer, so it doesn't overlap with `initialized`
3139        unsafe {
3140            let initialized = slice::from_raw_parts_mut(ptr, self.len());
3141            let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
3142
3143            (initialized, spare)
3144        }
3145    }
3146
3147    /// Returns a reference to the allocator.
3148    #[must_use]
3149    #[inline(always)]
3150    pub fn allocator(&self) -> &A {
3151        &self.allocator
3152    }
3153
3154    /// Returns a type which provides statistics about the memory usage of the bump allocator.
3155    ///
3156    /// This is equivalent to calling `.allocator().stats()`.
3157    /// This merely exists for api parity with `Mut*` collections which can't have a `allocator` method.
3158    #[must_use]
3159    #[inline(always)]
3160    pub fn allocator_stats(&self) -> A::TypedStats<'_> {
3161        self.allocator.typed_stats()
3162    }
3163}
3164
3165impl<'a, T, A: BumpAllocatorTypedScope<'a>> BumpVec<T, A> {
3166    /// Turns this `BumpVec<T>` into a `FixedBumpVec<T>`.
3167    ///
3168    /// This retains the unused capacity unlike <code>[into_](Self::into_slice)([boxed_](Self::into_boxed_slice))[slice](Self::into_slice)</code>.
3169    #[must_use]
3170    #[inline(always)]
3171    pub fn into_fixed_vec(self) -> FixedBumpVec<'a, T> {
3172        self.into_parts().0
3173    }
3174
3175    /// Turns this `BumpVec<T>` into a `BumpBox<[T]>`.
3176    #[must_use]
3177    #[inline(always)]
3178    pub fn into_boxed_slice(mut self) -> BumpBox<'a, [T]> {
3179        self.shrink_to_fit();
3180        self.into_fixed_vec().into_boxed_slice()
3181    }
3182
3183    /// Turns this `BumpVec<T>` into a `&[T]` that is live for this bump scope.
3184    ///
3185    /// This is only available for [`NoDrop`] types so you don't omit dropping a value for which it matters.
3186    ///
3187    /// `!NoDrop` types can still be turned into slices via <code>BumpBox::[leak](BumpBox::leak)(vec.[into_boxed_slice](Self::into_boxed_slice)())</code>.
3188    #[must_use]
3189    #[inline(always)]
3190    pub fn into_slice(self) -> &'a mut [T]
3191    where
3192        [T]: NoDrop,
3193    {
3194        self.into_boxed_slice().into_mut()
3195    }
3196
3197    /// Creates a `BumpVec<T>` from its parts.
3198    ///
3199    /// The provided `bump` does not have to be the one the `fixed_vec` was allocated in.
3200    ///
3201    /// ```
3202    /// # use bump_scope::{Bump, FixedBumpVec, BumpVec};
3203    /// # let bump: Bump = Bump::new();
3204    /// let mut fixed_vec = FixedBumpVec::with_capacity_in(3, &bump);
3205    /// fixed_vec.push(1);
3206    /// fixed_vec.push(2);
3207    /// fixed_vec.push(3);
3208    /// let mut vec = BumpVec::from_parts(fixed_vec, &bump);
3209    /// vec.push(4);
3210    /// vec.push(5);
3211    /// assert_eq!(vec, [1, 2, 3, 4, 5]);
3212    /// ```
3213    #[must_use]
3214    #[inline(always)]
3215    pub fn from_parts(vec: FixedBumpVec<'a, T>, allocator: A) -> Self {
3216        Self {
3217            fixed: unsafe { RawFixedBumpVec::from_cooked(vec) },
3218            allocator,
3219        }
3220    }
3221
3222    /// Turns this `BumpVec<T>` into its parts.
3223    /// ```
3224    /// # use bump_scope::{Bump, BumpVec};
3225    /// # let bump: Bump = Bump::new();
3226    /// let mut vec = BumpVec::new_in(&bump);
3227    /// vec.reserve(10);
3228    /// vec.push(1);
3229    /// let fixed_vec = vec.into_parts().0;
3230    /// assert_eq!(fixed_vec.capacity(), 10);
3231    /// assert_eq!(fixed_vec, [1]);
3232    /// ```
3233    #[must_use]
3234    #[inline(always)]
3235    pub fn into_parts(self) -> (FixedBumpVec<'a, T>, A) {
3236        destructure!(let Self { fixed, allocator } = self);
3237        (unsafe { fixed.cook() }, allocator)
3238    }
3239}
3240
3241impl<T, const N: usize, A: BumpAllocatorTyped> BumpVec<[T; N], A> {
3242    /// Takes a `BumpVec<[T; N]>` and flattens it into a `BumpVec<T>`.
3243    ///
3244    /// # Panics
3245    ///
3246    /// Panics if the length of the resulting vector would overflow a `usize`.
3247    ///
3248    /// This is only possible when flattening a vector of arrays of zero-sized
3249    /// types, and thus tends to be irrelevant in practice. If
3250    /// `size_of::<T>() > 0`, this will never panic.
3251    ///
3252    /// # Examples
3253    ///
3254    /// ```
3255    /// # use bump_scope::{Bump, bump_vec};
3256    /// # let bump: Bump = Bump::new();
3257    /// #
3258    /// let mut vec = bump_vec![in &bump; [1, 2, 3], [4, 5, 6], [7, 8, 9]];
3259    /// assert_eq!(vec.pop(), Some([7, 8, 9]));
3260    ///
3261    /// let mut flattened = vec.into_flattened();
3262    /// assert_eq!(flattened.pop(), Some(6));
3263    /// ```
3264    #[must_use]
3265    pub fn into_flattened(self) -> BumpVec<T, A> {
3266        destructure!(let Self { fixed, allocator } = self);
3267        let fixed = unsafe { RawFixedBumpVec::from_cooked(fixed.cook().into_flattened()) };
3268        BumpVec { fixed, allocator }
3269    }
3270}
3271
3272impl<T: Debug, A: BumpAllocatorTyped> Debug for BumpVec<T, A> {
3273    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3274        Debug::fmt(self.as_slice(), f)
3275    }
3276}
3277
3278impl<T, A: BumpAllocatorTyped, I: SliceIndex<[T]>> Index<I> for BumpVec<T, A> {
3279    type Output = I::Output;
3280
3281    #[inline(always)]
3282    fn index(&self, index: I) -> &Self::Output {
3283        Index::index(self.as_slice(), index)
3284    }
3285}
3286
3287impl<T, A: BumpAllocatorTyped, I: SliceIndex<[T]>> IndexMut<I> for BumpVec<T, A> {
3288    #[inline(always)]
3289    fn index_mut(&mut self, index: I) -> &mut Self::Output {
3290        IndexMut::index_mut(self.as_mut_slice(), index)
3291    }
3292}
3293
3294#[cfg(feature = "panic-on-alloc")]
3295impl<T, A: BumpAllocatorTyped> Extend<T> for BumpVec<T, A> {
3296    #[inline]
3297    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
3298        let iter = iter.into_iter();
3299
3300        self.reserve(iter.size_hint().0);
3301
3302        for value in iter {
3303            self.push(value);
3304        }
3305    }
3306}
3307
3308#[cfg(feature = "panic-on-alloc")]
3309impl<'t, T: Clone + 't, A: BumpAllocatorTyped> Extend<&'t T> for BumpVec<T, A> {
3310    #[inline]
3311    fn extend<I: IntoIterator<Item = &'t T>>(&mut self, iter: I) {
3312        let iter = iter.into_iter();
3313
3314        self.reserve(iter.size_hint().0);
3315
3316        for value in iter {
3317            self.push(value.clone());
3318        }
3319    }
3320}
3321
3322impl<T, A: BumpAllocatorTyped> IntoIterator for BumpVec<T, A> {
3323    type Item = T;
3324    type IntoIter = IntoIter<T, A>;
3325
3326    #[inline]
3327    fn into_iter(self) -> Self::IntoIter {
3328        unsafe {
3329            destructure!(let Self { fixed, allocator } = self);
3330
3331            let (slice, cap) = fixed.into_raw_parts();
3332            let begin = slice.cast::<T>();
3333
3334            let end = if T::IS_ZST {
3335                non_null::wrapping_byte_add(begin, slice.len())
3336            } else {
3337                begin.add(slice.len())
3338            };
3339
3340            IntoIter {
3341                buf: begin,
3342                cap,
3343
3344                ptr: begin,
3345                end,
3346
3347                allocator,
3348                marker: PhantomData,
3349            }
3350        }
3351    }
3352}
3353
3354impl<'c, T, A: BumpAllocatorTyped> IntoIterator for &'c BumpVec<T, A> {
3355    type Item = &'c T;
3356    type IntoIter = slice::Iter<'c, T>;
3357
3358    #[inline(always)]
3359    fn into_iter(self) -> Self::IntoIter {
3360        self.as_slice().iter()
3361    }
3362}
3363
3364impl<'c, T, A: BumpAllocatorTyped> IntoIterator for &'c mut BumpVec<T, A> {
3365    type Item = &'c mut T;
3366    type IntoIter = slice::IterMut<'c, T>;
3367
3368    #[inline(always)]
3369    fn into_iter(self) -> Self::IntoIter {
3370        self.as_mut_slice().iter_mut()
3371    }
3372}
3373
3374impl<T, A: BumpAllocatorTyped> AsRef<[T]> for BumpVec<T, A> {
3375    #[inline(always)]
3376    fn as_ref(&self) -> &[T] {
3377        self
3378    }
3379}
3380
3381impl<T, A: BumpAllocatorTyped> AsMut<[T]> for BumpVec<T, A> {
3382    #[inline(always)]
3383    fn as_mut(&mut self) -> &mut [T] {
3384        self
3385    }
3386}
3387
3388impl<T, A: BumpAllocatorTyped> Borrow<[T]> for BumpVec<T, A> {
3389    #[inline(always)]
3390    fn borrow(&self) -> &[T] {
3391        self
3392    }
3393}
3394
3395impl<T, A: BumpAllocatorTyped> BorrowMut<[T]> for BumpVec<T, A> {
3396    #[inline(always)]
3397    fn borrow_mut(&mut self) -> &mut [T] {
3398        self
3399    }
3400}
3401
3402impl<T: Hash, A: BumpAllocatorTyped> Hash for BumpVec<T, A> {
3403    #[inline(always)]
3404    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
3405        self.as_slice().hash(state);
3406    }
3407}
3408
3409/// Returns [`ErrorKind::OutOfMemory`](std::io::ErrorKind::OutOfMemory) when allocations fail.
3410#[cfg(feature = "std")]
3411impl<A: BumpAllocatorTyped> std::io::Write for BumpVec<u8, A> {
3412    #[inline(always)]
3413    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
3414        if self.try_extend_from_slice_copy(buf).is_err() {
3415            return Err(std::io::ErrorKind::OutOfMemory.into());
3416        }
3417
3418        Ok(buf.len())
3419    }
3420
3421    #[inline(always)]
3422    fn flush(&mut self) -> std::io::Result<()> {
3423        Ok(())
3424    }
3425
3426    #[inline]
3427    fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
3428        let len = bufs.iter().map(|b| b.len()).sum();
3429        self.try_reserve(len).map_err(|_| std::io::ErrorKind::OutOfMemory)?;
3430        for buf in bufs {
3431            self.try_extend_from_slice_copy(buf)
3432                .map_err(|_| std::io::ErrorKind::OutOfMemory)?;
3433        }
3434        Ok(len)
3435    }
3436
3437    #[inline(always)]
3438    fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
3439        if self.try_extend_from_slice_copy(buf).is_err() {
3440            return Err(std::io::ErrorKind::OutOfMemory.into());
3441        }
3442
3443        Ok(())
3444    }
3445}
3446
3447#[cfg(feature = "panic-on-alloc")]
3448impl<T, A: BumpAllocatorTyped + Default> FromIterator<T> for BumpVec<T, A> {
3449    #[inline]
3450    #[track_caller]
3451    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3452        Self::from_iter_in(iter, A::default())
3453    }
3454}