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/// This is 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 ≎ 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 ≎ 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 ≎ 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 ≎ 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_with_unchecked(|| value.clone());
489 }
490
491 vec.push_with_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 ≎ 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 ≎ 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 ≎ 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 ≎ 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 ≎ 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 ≎ 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 ≎ 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 ≎ 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 ≎ "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 ≎ 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 #[inline(always)]
1090 pub unsafe fn push_unchecked(&mut self, value: T) {
1091 unsafe { self.fixed.cook_mut().push_unchecked(value) };
1092 }
1093
1094 /// Appends an element to the back of the collection.
1095 ///
1096 /// # Safety
1097 /// Vector must not be full.
1098 #[inline(always)]
1099 pub unsafe fn push_with_unchecked(&mut self, f: impl FnOnce() -> T) {
1100 unsafe { self.fixed.cook_mut().push_with_unchecked(f) };
1101 }
1102
1103 /// Forces the length of the vector to `new_len`.
1104 ///
1105 /// This is a low-level operation that maintains none of the normal
1106 /// invariants of the type. Normally changing the length of a vector
1107 /// is done using one of the safe operations instead, such as
1108 /// [`resize`], [`truncate`], [`extend`], or [`clear`].
1109 ///
1110 /// # Safety
1111 /// - `new_len` must be less than or equal to the [`capacity`].
1112 /// - The elements at `old_len..new_len` must be initialized.
1113 ///
1114 /// [`resize`]: Self::resize
1115 /// [`truncate`]: Self::truncate
1116 /// [`extend`]: Self::extend
1117 /// [`clear`]: Self::clear
1118 /// [`capacity`]: Self::capacity
1119 #[inline]
1120 pub unsafe fn set_len(&mut self, new_len: usize) {
1121 unsafe { self.fixed.set_len(new_len) };
1122 }
1123
1124 #[inline]
1125 pub(crate) unsafe fn inc_len(&mut self, amount: usize) {
1126 unsafe { self.fixed.cook_mut().inc_len(amount) };
1127 }
1128
1129 /// Appends an element to the back of a collection.
1130 ///
1131 /// # Panics
1132 /// Panics if the allocation fails.
1133 ///
1134 /// # Examples
1135 /// ```
1136 /// # use bump_scope::{bump_vec, Bump};
1137 /// # let bump: Bump = Bump::new();
1138 /// let mut vec = bump_vec![in ≎ 1, 2];
1139 /// vec.push(3);
1140 /// assert_eq!(vec, [1, 2, 3]);
1141 /// ```
1142 #[inline(always)]
1143 #[cfg(feature = "panic-on-alloc")]
1144 pub fn push(&mut self, value: T) {
1145 panic_on_error(self.generic_push(value));
1146 }
1147
1148 /// Appends an element to the back of a collection.
1149 ///
1150 /// # Errors
1151 /// Errors if the allocation fails.
1152 ///
1153 /// # Examples
1154 /// ```
1155 /// # use bump_scope::{bump_vec, Bump};
1156 /// # let bump: Bump = Bump::new();
1157 /// let mut vec = bump_vec![try in ≎ 1, 2]?;
1158 /// vec.try_push(3)?;
1159 /// assert_eq!(vec, [1, 2, 3]);
1160 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1161 /// ```
1162 #[inline(always)]
1163 pub fn try_push(&mut self, value: T) -> Result<(), AllocError> {
1164 self.generic_push(value)
1165 }
1166
1167 #[inline]
1168 pub(crate) fn generic_push<E: ErrorBehavior>(&mut self, value: T) -> Result<(), E> {
1169 self.generic_push_with(|| value)
1170 }
1171
1172 /// Reserves space for one more element, then calls `f`
1173 /// to produce the value that is appended.
1174 ///
1175 /// In some cases this could be more performant than `push(f())` because it
1176 /// permits the compiler to directly place `T` in the vector instead of
1177 /// constructing it on the stack and copying it over.
1178 ///
1179 /// # Panics
1180 /// Panics if the allocation fails.
1181 ///
1182 /// # Examples
1183 /// ```
1184 /// # use bump_scope::{Bump, bump_vec};
1185 /// # let bump: Bump = Bump::new();
1186 /// let mut vec = bump_vec![in ≎ 1, 2];
1187 /// vec.push_with(|| 3);
1188 /// assert_eq!(vec, [1, 2, 3]);
1189 /// ```
1190 #[inline(always)]
1191 #[cfg(feature = "panic-on-alloc")]
1192 pub fn push_with(&mut self, f: impl FnOnce() -> T) {
1193 panic_on_error(self.generic_push_with(f));
1194 }
1195
1196 /// Reserves space for one more element, then calls `f`
1197 /// to produce the value that is appended.
1198 ///
1199 /// In some cases this could be more performant than `push(f())` because it
1200 /// permits the compiler to directly place `T` in the vector instead of
1201 /// constructing it on the stack and copying it over.
1202 ///
1203 /// # Errors
1204 /// Errors if the allocation fails.
1205 ///
1206 /// # Examples
1207 /// ```
1208 /// # use bump_scope::{Bump, bump_vec};
1209 /// # let bump: Bump = Bump::new();
1210 /// let mut vec = bump_vec![in ≎ 1, 2];
1211 /// vec.try_push_with(|| 3)?;
1212 /// assert_eq!(vec, [1, 2, 3]);
1213 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1214 /// ```
1215 #[inline(always)]
1216 pub fn try_push_with(&mut self, f: impl FnOnce() -> T) -> Result<(), AllocError> {
1217 self.generic_push_with(f)
1218 }
1219
1220 #[inline]
1221 pub(crate) fn generic_push_with<E: ErrorBehavior>(&mut self, f: impl FnOnce() -> T) -> Result<(), E> {
1222 self.generic_reserve_one()?;
1223 unsafe {
1224 self.push_with_unchecked(f);
1225 }
1226 Ok(())
1227 }
1228
1229 /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1230 ///
1231 /// # Panics
1232 /// Panics if the allocation fails.
1233 ///
1234 /// Panics if `index > len`.
1235 ///
1236 /// # Examples
1237 /// ```
1238 /// # use bump_scope::{Bump, bump_vec};
1239 /// # let bump: Bump = Bump::new();
1240 /// let mut vec = bump_vec![in ≎ 1, 2, 3];
1241 /// vec.insert(1, 4);
1242 /// assert_eq!(vec, [1, 4, 2, 3]);
1243 /// vec.insert(4, 5);
1244 /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1245 /// ```
1246 #[inline(always)]
1247 #[cfg(feature = "panic-on-alloc")]
1248 pub fn insert(&mut self, index: usize, element: T) {
1249 panic_on_error(self.generic_insert(index, element));
1250 }
1251
1252 /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1253 ///
1254 /// # Panics
1255 /// Panics if `index > len`.
1256 ///
1257 /// # Errors
1258 /// Errors if the allocation fails.
1259 ///
1260 /// # Examples
1261 /// ```
1262 /// # use bump_scope::{Bump, bump_vec};
1263 /// # let bump: Bump = Bump::new();
1264 /// let mut vec = bump_vec![try in ≎ 1, 2, 3]?;
1265 /// vec.try_insert(1, 4)?;
1266 /// assert_eq!(vec, [1, 4, 2, 3]);
1267 /// vec.try_insert(4, 5)?;
1268 /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1269 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1270 /// ```
1271 #[inline(always)]
1272 pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), AllocError> {
1273 self.generic_insert(index, element)
1274 }
1275
1276 #[inline]
1277 pub(crate) fn generic_insert<E: ErrorBehavior>(&mut self, index: usize, element: T) -> Result<(), E> {
1278 #[cold]
1279 #[track_caller]
1280 #[inline(never)]
1281 fn assert_failed(index: usize, len: usize) -> ! {
1282 panic!("insertion index (is {index}) should be <= len (is {len})");
1283 }
1284
1285 if index > self.len() {
1286 assert_failed(index, self.len());
1287 }
1288
1289 self.generic_reserve_one()?;
1290
1291 unsafe {
1292 let pos = self.as_mut_ptr().add(index);
1293
1294 if index != self.len() {
1295 let len = self.len() - index;
1296 ptr::copy(pos, pos.add(1), len);
1297 }
1298
1299 pos.write(element);
1300 self.inc_len(1);
1301 }
1302
1303 Ok(())
1304 }
1305
1306 /// Copies and appends all elements in a slice to the `BumpVec`.
1307 ///
1308 /// Iterates over the `slice`, copies each element, and then appends
1309 /// it to this `BumpVec`. The `slice` is traversed in-order.
1310 ///
1311 /// Note that this function is same as [`extend`] except that it is
1312 /// specialized to work with copyable slices instead.
1313 ///
1314 /// [`extend`]: Self::extend
1315 ///
1316 /// # Panics
1317 /// Panics if the allocation fails.
1318 ///
1319 /// # Examples
1320 /// ```
1321 /// # use bump_scope::{ Bump, bump_vec };
1322 /// # let bump: Bump = Bump::new();
1323 /// let mut vec = bump_vec![in ≎ 1];
1324 /// vec.extend_from_slice_copy(&[2, 3, 4]);
1325 /// assert_eq!(vec, [1, 2, 3, 4]);
1326 /// ```
1327 #[inline(always)]
1328 #[cfg(feature = "panic-on-alloc")]
1329 pub fn extend_from_slice_copy(&mut self, slice: &[T])
1330 where
1331 T: Copy,
1332 {
1333 panic_on_error(self.generic_extend_from_slice_copy(slice));
1334 }
1335
1336 /// Copies and appends all elements in a slice to the `BumpVec`.
1337 ///
1338 /// Iterates over the `slice`, copies each element, and then appends
1339 /// it to this `BumpVec`. The `slice` is traversed in-order.
1340 ///
1341 /// Note that this function is same as [`extend`] except that it is
1342 /// specialized to work with copyable slices instead.
1343 ///
1344 /// [`extend`]: Self::extend
1345 ///
1346 /// # Errors
1347 /// Errors if the allocation fails.
1348 ///
1349 /// # Examples
1350 /// ```
1351 /// # use bump_scope::{ Bump, bump_vec };
1352 /// # let bump: Bump = Bump::new();
1353 /// let mut vec = bump_vec![try in ≎ 1]?;
1354 /// vec.try_extend_from_slice_copy(&[2, 3, 4])?;
1355 /// assert_eq!(vec, [1, 2, 3, 4]);
1356 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1357 /// ```
1358 #[inline(always)]
1359 pub fn try_extend_from_slice_copy(&mut self, slice: &[T]) -> Result<(), AllocError>
1360 where
1361 T: Copy,
1362 {
1363 self.generic_extend_from_slice_copy(slice)
1364 }
1365
1366 #[inline]
1367 pub(crate) fn generic_extend_from_slice_copy<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1368 where
1369 T: Copy,
1370 {
1371 unsafe { self.extend_by_copy_nonoverlapping(slice) }
1372 }
1373
1374 /// Clones and appends all elements in a slice to the `BumpVec`.
1375 ///
1376 /// Iterates over the `slice`, clones each element, and then appends
1377 /// it to this `BumpVec`. The `slice` is traversed in-order.
1378 ///
1379 /// Note that this function is same as [`extend`] except that it is
1380 /// specialized to work with slices instead.
1381 ///
1382 /// [`extend`]: Self::extend
1383 ///
1384 /// # Panics
1385 /// Panics if the allocation fails.
1386 ///
1387 /// # Examples
1388 /// ```
1389 /// # use std::string::String;
1390 /// # use bump_scope::{ Bump, bump_vec };
1391 /// # let bump: Bump = Bump::new();
1392 /// let mut vec = bump_vec![in ≎ String::from("a")];
1393 /// vec.extend_from_slice_clone(&[String::from("b"), String::from("c")]);
1394 /// assert_eq!(vec, ["a", "b", "c"]);
1395 /// ```
1396 #[inline(always)]
1397 #[cfg(feature = "panic-on-alloc")]
1398 pub fn extend_from_slice_clone(&mut self, slice: &[T])
1399 where
1400 T: Clone,
1401 {
1402 panic_on_error(self.generic_extend_from_slice_clone(slice));
1403 }
1404
1405 /// Clones and appends all elements in a slice to the `BumpVec`.
1406 ///
1407 /// Iterates over the `slice`, clones each element, and then appends
1408 /// it to this `BumpVec`. The `slice` is traversed in-order.
1409 ///
1410 /// Note that this function is same as [`extend`] except that it is
1411 /// specialized to work with slices instead.
1412 ///
1413 /// [`extend`]: Self::extend
1414 ///
1415 /// # Errors
1416 /// Errors if the allocation fails.
1417 ///
1418 /// # Examples
1419 /// ```
1420 /// # use std::string::String;
1421 /// # use bump_scope::{ Bump, bump_vec };
1422 /// # let bump: Bump = Bump::new();
1423 /// let mut vec = bump_vec![try in ≎ String::from("a")]?;
1424 /// vec.try_extend_from_slice_clone(&[String::from("b"), String::from("c")])?;
1425 /// assert_eq!(vec, ["a", "b", "c"]);
1426 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1427 /// ```
1428 #[inline(always)]
1429 pub fn try_extend_from_slice_clone(&mut self, slice: &[T]) -> Result<(), AllocError>
1430 where
1431 T: Clone,
1432 {
1433 self.generic_extend_from_slice_clone(slice)
1434 }
1435
1436 #[inline]
1437 pub(crate) fn generic_extend_from_slice_clone<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1438 where
1439 T: Clone,
1440 {
1441 self.generic_reserve(slice.len())?;
1442
1443 unsafe {
1444 let mut pos = 0usize;
1445
1446 while likely(pos != slice.len()) {
1447 let elem = slice.get_unchecked(pos);
1448 self.push_unchecked(elem.clone());
1449 pos += 1;
1450 }
1451 }
1452
1453 Ok(())
1454 }
1455
1456 /// Copies elements from `src` range to the end of the vector.
1457 ///
1458 /// # Panics
1459 /// Panics if the allocation fails.
1460 ///
1461 /// Panics if the starting point is greater than the end point or if
1462 /// the end point is greater than the length of the vector.
1463 ///
1464 /// # Examples
1465 /// ```
1466 /// # use bump_scope::{Bump, bump_vec};
1467 /// # let bump: Bump = Bump::new();
1468 /// let mut vec = bump_vec![in ≎ 0, 1, 2, 3, 4];
1469 ///
1470 /// vec.extend_from_within_copy(2..);
1471 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1472 ///
1473 /// vec.extend_from_within_copy(..2);
1474 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1475 ///
1476 /// vec.extend_from_within_copy(4..8);
1477 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1478 /// ```
1479 #[inline(always)]
1480 #[cfg(feature = "panic-on-alloc")]
1481 pub fn extend_from_within_copy<R>(&mut self, src: R)
1482 where
1483 T: Copy,
1484 R: RangeBounds<usize>,
1485 {
1486 panic_on_error(self.generic_extend_from_within_copy(src));
1487 }
1488
1489 /// Copies elements from `src` range to the end of the vector.
1490 ///
1491 /// # Panics
1492 /// Panics if the starting point is greater than the end point or if
1493 /// the end point is greater than the length of the vector.
1494 ///
1495 /// # Errors
1496 /// Errors if the allocation fails.
1497 ///
1498 /// # Examples
1499 /// ```
1500 /// # use bump_scope::{Bump, bump_vec};
1501 /// # let bump: Bump = Bump::new();
1502 /// let mut vec = bump_vec![try in ≎ 0, 1, 2, 3, 4]?;
1503 ///
1504 /// vec.try_extend_from_within_copy(2..)?;
1505 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1506 ///
1507 /// vec.try_extend_from_within_copy(..2)?;
1508 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1509 ///
1510 /// vec.try_extend_from_within_copy(4..8)?;
1511 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1512 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1513 /// ```
1514 #[inline(always)]
1515 pub fn try_extend_from_within_copy<R>(&mut self, src: R) -> Result<(), AllocError>
1516 where
1517 T: Copy,
1518 R: RangeBounds<usize>,
1519 {
1520 self.generic_extend_from_within_copy(src)
1521 }
1522
1523 #[inline]
1524 pub(crate) fn generic_extend_from_within_copy<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1525 where
1526 T: Copy,
1527 R: RangeBounds<usize>,
1528 {
1529 let range = slice::range(src, ..self.len());
1530 let count = range.len();
1531
1532 self.generic_reserve(count)?;
1533
1534 // SAFETY:
1535 // - `slice::range` guarantees that the given range is valid for indexing self
1536 unsafe {
1537 let ptr = self.as_mut_ptr();
1538
1539 let src = ptr.add(range.start);
1540 let dst = ptr.add(self.len());
1541 ptr::copy_nonoverlapping(src, dst, count);
1542
1543 self.inc_len(count);
1544 Ok(())
1545 }
1546 }
1547
1548 /// Clones elements from `src` range to the end of the vector.
1549 ///
1550 /// # Panics
1551 /// Panics if the allocation fails.
1552 ///
1553 /// Panics if the starting point is greater than the end point or if
1554 /// the end point is greater than the length of the vector.
1555 ///
1556 /// # Examples
1557 /// ```
1558 /// # use bump_scope::{Bump, bump_vec};
1559 /// # let bump: Bump = Bump::new();
1560 /// let mut vec = bump_vec![in ≎ 0, 1, 2, 3, 4];
1561 ///
1562 /// vec.extend_from_within_clone(2..);
1563 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1564 ///
1565 /// vec.extend_from_within_clone(..2);
1566 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1567 ///
1568 /// vec.extend_from_within_clone(4..8);
1569 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1570 /// ```
1571 #[inline(always)]
1572 #[cfg(feature = "panic-on-alloc")]
1573 pub fn extend_from_within_clone<R>(&mut self, src: R)
1574 where
1575 T: Clone,
1576 R: RangeBounds<usize>,
1577 {
1578 panic_on_error(self.generic_extend_from_within_clone(src));
1579 }
1580
1581 /// Clones elements from `src` range to the end of the vector.
1582 ///
1583 /// # Panics
1584 /// Panics if the starting point is greater than the end point or if
1585 /// the end point is greater than the length of the vector.
1586 ///
1587 /// # Errors
1588 /// Errors if the allocation fails.
1589 ///
1590 /// # Examples
1591 /// ```
1592 /// # use bump_scope::{Bump, bump_vec};
1593 /// # let bump: Bump = Bump::new();
1594 /// let mut vec = bump_vec![try in ≎ 0, 1, 2, 3, 4]?;
1595 ///
1596 /// vec.try_extend_from_within_clone(2..)?;
1597 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4]);
1598 ///
1599 /// vec.try_extend_from_within_clone(..2)?;
1600 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1]);
1601 ///
1602 /// vec.try_extend_from_within_clone(4..8)?;
1603 /// assert_eq!(vec, [0, 1, 2, 3, 4, 2, 3, 4, 0, 1, 4, 2, 3, 4]);
1604 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1605 /// ```
1606 #[inline(always)]
1607 pub fn try_extend_from_within_clone<R>(&mut self, src: R) -> Result<(), AllocError>
1608 where
1609 T: Clone,
1610 R: RangeBounds<usize>,
1611 {
1612 self.generic_extend_from_within_clone(src)
1613 }
1614
1615 #[inline]
1616 pub(crate) fn generic_extend_from_within_clone<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1617 where
1618 T: Clone,
1619 R: RangeBounds<usize>,
1620 {
1621 let range = slice::range(src, ..self.len());
1622 let count = range.len();
1623
1624 self.generic_reserve(count)?;
1625
1626 if T::IS_ZST {
1627 unsafe {
1628 // We can materialize ZST's from nothing.
1629 #[expect(clippy::uninit_assumed_init)]
1630 let fake = ManuallyDrop::new(MaybeUninit::<T>::uninit().assume_init());
1631
1632 for _ in 0..count {
1633 self.push_unchecked((*fake).clone());
1634 }
1635
1636 return Ok(());
1637 }
1638 }
1639
1640 // SAFETY:
1641 // - `slice::range` guarantees that the given range is valid for indexing self
1642 unsafe {
1643 let ptr = self.as_mut_ptr();
1644
1645 let mut src = ptr.add(range.start);
1646 let mut dst = ptr.add(self.len());
1647
1648 let src_end = src.add(count);
1649
1650 while src != src_end {
1651 dst.write((*src).clone());
1652
1653 src = src.add(1);
1654 dst = dst.add(1);
1655 self.inc_len(1);
1656 }
1657 }
1658
1659 Ok(())
1660 }
1661
1662 /// Reserves capacity for at least `additional` more elements to be inserted
1663 /// in the given `BumpVec<T>`. The collection may reserve more space to
1664 /// speculatively avoid frequent reallocations. After calling `reserve`,
1665 /// capacity will be greater than or equal to `self.len() + additional`.
1666 /// Does nothing if capacity is already sufficient.
1667 ///
1668 /// # Panics
1669 /// Panics if the allocation fails.
1670 ///
1671 /// # Examples
1672 /// ```
1673 /// # use bump_scope::{Bump, bump_vec};
1674 /// # let bump: Bump = Bump::new();
1675 /// let mut vec = bump_vec![in ≎ 1];
1676 /// vec.reserve(10);
1677 /// assert!(vec.capacity() >= 11);
1678 /// ```
1679 #[inline(always)]
1680 #[cfg(feature = "panic-on-alloc")]
1681 pub fn reserve(&mut self, additional: usize) {
1682 panic_on_error(self.generic_reserve(additional));
1683 }
1684
1685 /// Reserves capacity for at least `additional` more elements to be inserted
1686 /// in the given `BumpVec<T>`. The collection may reserve more space to
1687 /// speculatively avoid frequent reallocations. After calling `reserve`,
1688 /// capacity will be greater than or equal to `self.len() + additional`.
1689 /// Does nothing if capacity is already sufficient.
1690 ///
1691 /// # Errors
1692 /// Errors if the allocation fails.
1693 ///
1694 /// # Examples
1695 /// ```
1696 /// # use bump_scope::{Bump, bump_vec};
1697 /// # let bump: Bump = Bump::new();
1698 /// let mut vec = bump_vec![try in ≎ 1]?;
1699 /// vec.try_reserve(10)?;
1700 /// assert!(vec.capacity() >= 11);
1701 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1702 /// ```
1703 #[inline(always)]
1704 pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError> {
1705 self.generic_reserve(additional)
1706 }
1707
1708 #[inline]
1709 pub(crate) fn generic_reserve<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1710 if additional > (self.capacity() - self.len()) {
1711 self.generic_grow_amortized(additional)?;
1712 }
1713
1714 Ok(())
1715 }
1716
1717 /// Reserves the minimum capacity for at least `additional` more elements to
1718 /// be inserted in the given `BumpVec<T>`. Unlike [`reserve`], this will not
1719 /// deliberately over-allocate to speculatively avoid frequent allocations.
1720 /// After calling `reserve_exact`, capacity will be greater than or equal to
1721 /// `self.len() + additional`. Does nothing if the capacity is already
1722 /// sufficient.
1723 ///
1724 /// Note that the allocator may give the collection more space than it
1725 /// requests. Therefore, capacity cannot be relied upon to be precisely
1726 /// minimal. Prefer [`reserve`] if future insertions are expected.
1727 ///
1728 /// [`reserve`]: Self::reserve
1729 ///
1730 /// # Panics
1731 /// Panics if the allocation fails.
1732 ///
1733 /// # Examples
1734 /// ```
1735 /// # use bump_scope::{Bump, bump_vec};
1736 /// # let bump: Bump = Bump::new();
1737 /// let mut vec = bump_vec![in ≎ 1];
1738 /// vec.reserve_exact(10);
1739 /// assert!(vec.capacity() >= 11);
1740 /// ```
1741 #[inline(always)]
1742 #[cfg(feature = "panic-on-alloc")]
1743 pub fn reserve_exact(&mut self, additional: usize) {
1744 panic_on_error(self.generic_reserve_exact(additional));
1745 }
1746
1747 /// Reserves the minimum capacity for at least `additional` more elements to
1748 /// be inserted in the given `BumpVec<T>`. Unlike [`reserve`], this will not
1749 /// deliberately over-allocate to speculatively avoid frequent allocations.
1750 /// After calling `reserve_exact`, capacity will be greater than or equal to
1751 /// `self.len() + additional`. Does nothing if the capacity is already
1752 /// sufficient.
1753 ///
1754 /// Note that the allocator may give the collection more space than it
1755 /// requests. Therefore, capacity cannot be relied upon to be precisely
1756 /// minimal. Prefer [`reserve`] if future insertions are expected.
1757 ///
1758 /// [`reserve`]: Self::reserve
1759 ///
1760 /// # Errors
1761 /// Errors if the allocation fails.
1762 ///
1763 /// # Examples
1764 /// ```
1765 /// # use bump_scope::{Bump, bump_vec};
1766 /// # let bump: Bump = Bump::new();
1767 /// let mut vec = bump_vec![try in ≎ 1]?;
1768 /// vec.try_reserve_exact(10)?;
1769 /// assert!(vec.capacity() >= 11);
1770 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1771 /// ```
1772 #[inline(always)]
1773 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), AllocError> {
1774 self.generic_reserve_exact(additional)
1775 }
1776
1777 #[inline]
1778 pub(crate) fn generic_reserve_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1779 if additional > (self.capacity() - self.len()) {
1780 self.generic_grow_exact(additional)?;
1781 }
1782
1783 Ok(())
1784 }
1785
1786 /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
1787 ///
1788 /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
1789 /// difference, with each additional slot filled with `value`.
1790 /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
1791 ///
1792 /// This method requires `T` to implement [`Clone`],
1793 /// in order to be able to clone the passed value.
1794 /// If you need more flexibility (or want to rely on [`Default`] instead of
1795 /// [`Clone`]), use [`resize_with`].
1796 /// If you only need to resize to a smaller size, use [`truncate`].
1797 ///
1798 /// [`resize_with`]: Self::resize_with
1799 /// [`truncate`]: Self::truncate
1800 ///
1801 /// # Panics
1802 /// Panics if the allocation fails.
1803 ///
1804 /// # Examples
1805 /// ```
1806 /// # use bump_scope::{Bump, bump_vec};
1807 /// # let bump: Bump = Bump::new();
1808 /// let mut vec = bump_vec![in ≎ "hello"];
1809 /// vec.resize(3, "world");
1810 /// assert_eq!(vec, ["hello", "world", "world"]);
1811 /// drop(vec);
1812 ///
1813 /// let mut vec = bump_vec![in ≎ 1, 2, 3, 4];
1814 /// vec.resize(2, 0);
1815 /// assert_eq!(vec, [1, 2]);
1816 /// ```
1817 #[inline(always)]
1818 #[cfg(feature = "panic-on-alloc")]
1819 pub fn resize(&mut self, new_len: usize, value: T)
1820 where
1821 T: Clone,
1822 {
1823 panic_on_error(self.generic_resize(new_len, value));
1824 }
1825
1826 /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
1827 ///
1828 /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
1829 /// difference, with each additional slot filled with `value`.
1830 /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
1831 ///
1832 /// This method requires `T` to implement [`Clone`],
1833 /// in order to be able to clone the passed value.
1834 /// If you need more flexibility (or want to rely on [`Default`] instead of
1835 /// [`Clone`]), use [`resize_with`].
1836 /// If you only need to resize to a smaller size, use [`truncate`].
1837 ///
1838 /// [`resize_with`]: Self::resize_with
1839 /// [`truncate`]: Self::truncate
1840 ///
1841 /// # Errors
1842 /// Errors if the allocation fails.
1843 ///
1844 /// # Examples
1845 /// ```
1846 /// # use bump_scope::{Bump, bump_vec};
1847 /// # let bump: Bump = Bump::new();
1848 /// let mut vec = bump_vec![try in ≎ "hello"]?;
1849 /// vec.try_resize(3, "world")?;
1850 /// assert_eq!(vec, ["hello", "world", "world"]);
1851 /// drop(vec);
1852 ///
1853 /// let mut vec = bump_vec![try in ≎ 1, 2, 3, 4]?;
1854 /// vec.try_resize(2, 0)?;
1855 /// assert_eq!(vec, [1, 2]);
1856 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1857 /// ```
1858 #[inline(always)]
1859 pub fn try_resize(&mut self, new_len: usize, value: T) -> Result<(), AllocError>
1860 where
1861 T: Clone,
1862 {
1863 self.generic_resize(new_len, value)
1864 }
1865
1866 #[inline]
1867 pub(crate) fn generic_resize<E: ErrorBehavior>(&mut self, new_len: usize, value: T) -> Result<(), E>
1868 where
1869 T: Clone,
1870 {
1871 let len = self.len();
1872
1873 if new_len > len {
1874 self.extend_with(new_len - len, value)
1875 } else {
1876 self.truncate(new_len);
1877 Ok(())
1878 }
1879 }
1880
1881 /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
1882 ///
1883 /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
1884 /// difference, with each additional slot filled with the result of
1885 /// calling the closure `f`. The return values from `f` will end up
1886 /// in the `BumpVec` in the order they have been generated.
1887 ///
1888 /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
1889 ///
1890 /// This method uses a closure to create new values on every push. If
1891 /// you'd rather [`Clone`] a given value, use [`BumpVec::resize`]. If you
1892 /// want to use the [`Default`] trait to generate values, you can
1893 /// pass [`Default::default`] as the second argument.
1894 ///
1895 /// # Panics
1896 /// Panics if the allocation fails.
1897 ///
1898 /// # Examples
1899 /// ```
1900 /// # use bump_scope::{Bump, bump_vec};
1901 /// # let bump: Bump = Bump::new();
1902 /// let mut vec = bump_vec![in ≎ 1, 2, 3];
1903 /// vec.resize_with(5, Default::default);
1904 /// assert_eq!(vec, [1, 2, 3, 0, 0]);
1905 /// drop(vec);
1906 ///
1907 /// let mut vec = bump_vec![in &bump];
1908 /// let mut p = 1;
1909 /// vec.resize_with(4, || { p *= 2; p });
1910 /// assert_eq!(vec, [2, 4, 8, 16]);
1911 /// ```
1912 #[inline(always)]
1913 #[cfg(feature = "panic-on-alloc")]
1914 pub fn resize_with<F>(&mut self, new_len: usize, f: F)
1915 where
1916 F: FnMut() -> T,
1917 {
1918 panic_on_error(self.generic_resize_with(new_len, f));
1919 }
1920
1921 /// Resizes the `BumpVec` in-place so that `len` is equal to `new_len`.
1922 ///
1923 /// If `new_len` is greater than `len`, the `BumpVec` is extended by the
1924 /// difference, with each additional slot filled with the result of
1925 /// calling the closure `f`. The return values from `f` will end up
1926 /// in the `BumpVec` in the order they have been generated.
1927 ///
1928 /// If `new_len` is less than `len`, the `BumpVec` is simply truncated.
1929 ///
1930 /// This method uses a closure to create new values on every push. If
1931 /// you'd rather [`Clone`] a given value, use [`BumpVec::resize`]. If you
1932 /// want to use the [`Default`] trait to generate values, you can
1933 /// pass [`Default::default`] as the second argument.
1934 ///
1935 /// # Errors
1936 /// Errors if the allocation fails.
1937 ///
1938 /// # Examples
1939 /// ```
1940 /// # use bump_scope::{Bump, bump_vec};
1941 /// # let bump: Bump = Bump::new();
1942 /// let mut vec = bump_vec![try in ≎ 1, 2, 3]?;
1943 /// vec.try_resize_with(5, Default::default)?;
1944 /// assert_eq!(vec, [1, 2, 3, 0, 0]);
1945 /// drop(vec);
1946 ///
1947 /// let mut vec = bump_vec![try in &bump]?;
1948 /// let mut p = 1;
1949 /// vec.try_resize_with(4, || { p *= 2; p })?;
1950 /// assert_eq!(vec, [2, 4, 8, 16]);
1951 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1952 /// ```
1953 #[inline(always)]
1954 pub fn try_resize_with<F>(&mut self, new_len: usize, f: F) -> Result<(), AllocError>
1955 where
1956 F: FnMut() -> T,
1957 {
1958 self.generic_resize_with(new_len, f)
1959 }
1960
1961 #[inline]
1962 pub(crate) fn generic_resize_with<E: ErrorBehavior, F>(&mut self, new_len: usize, f: F) -> Result<(), E>
1963 where
1964 F: FnMut() -> T,
1965 {
1966 let len = self.len();
1967 if new_len > len {
1968 unsafe { self.extend_trusted(iter::repeat_with(f).take(new_len - len)) }
1969 } else {
1970 self.truncate(new_len);
1971 Ok(())
1972 }
1973 }
1974
1975 /// Moves all the elements of `other` into `self`, leaving `other` empty.
1976 ///
1977 /// # Panics
1978 /// Panics if the allocation fails.
1979 ///
1980 /// # Examples
1981 /// ```
1982 /// # use bump_scope::{Bump, BumpVec};
1983 /// # let bump: Bump = Bump::new();
1984 /// let mut vec = BumpVec::new_in(&bump);
1985 ///
1986 /// // append by value
1987 /// vec.append([1, 2]);
1988 /// vec.append(vec![3, 4]);
1989 /// vec.append(bump.alloc_iter(5..=6));
1990 ///
1991 /// // append by mutable reference
1992 /// let mut other = vec![7, 8];
1993 /// vec.append(&mut other);
1994 ///
1995 /// assert_eq!(other, []);
1996 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7, 8]);
1997 /// ```
1998 #[inline(always)]
1999 #[cfg(feature = "panic-on-alloc")]
2000 pub fn append(&mut self, other: impl OwnedSlice<Item = T>) {
2001 panic_on_error(self.generic_append(other));
2002 }
2003
2004 /// Moves all the elements of `other` into `self`, leaving `other` empty.
2005 ///
2006 /// # Errors
2007 /// Errors if the allocation fails.
2008 ///
2009 /// # Examples
2010 /// ```
2011 /// # use bump_scope::{Bump, BumpVec};
2012 /// # let bump: Bump = Bump::new();
2013 /// let mut vec = BumpVec::new_in(&bump);
2014 ///
2015 /// // append by value
2016 /// vec.try_append([1, 2])?;
2017 /// vec.try_append(vec![3, 4])?;
2018 /// vec.try_append(bump.alloc_iter(5..=6))?;
2019 ///
2020 /// // append by mutable reference
2021 /// let mut other = vec![7, 8];
2022 /// vec.try_append(&mut other)?;
2023 ///
2024 /// assert_eq!(other, []);
2025 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7, 8]);
2026 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2027 /// ```
2028 #[inline(always)]
2029 pub fn try_append(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), AllocError> {
2030 self.generic_append(other)
2031 }
2032
2033 #[inline]
2034 pub(crate) fn generic_append<E: ErrorBehavior>(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), E> {
2035 unsafe {
2036 let mut owned_slice = other.into_take_owned_slice();
2037
2038 let slice = NonNull::from(owned_slice.owned_slice_ref());
2039 self.generic_reserve(slice.len())?;
2040
2041 let src = slice.cast::<T>().as_ptr();
2042 let dst = self.as_mut_ptr().add(self.len());
2043 ptr::copy_nonoverlapping(src, dst, slice.len());
2044
2045 owned_slice.take_owned_slice();
2046 self.inc_len(slice.len());
2047 Ok(())
2048 }
2049 }
2050
2051 /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2052 ///
2053 /// Compared to `from_iter_in(into_iter().map(f), ...)` this method has the advantage that it can reuse the existing allocation.
2054 ///
2055 /// # Panics
2056 /// Panics if the allocation fails. An allocation only occurs when the alignment or size of `U` is greater than that of `T`.
2057 ///
2058 /// # Examples
2059 /// Mapping to a type with an equal alignment and size (no allocation):
2060 /// ```
2061 /// # use bump_scope::{Bump, BumpVec};
2062 /// # use core::num::NonZero;
2063 /// # let bump: Bump = Bump::new();
2064 /// let a = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2065 /// let b = a.map(NonZero::new);
2066 /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2067 /// ```
2068 ///
2069 /// Mapping to a type with a smaller alignment and size (no allocation, capacity may grow):
2070 /// ```
2071 /// # use bump_scope::{Bump, BumpVec};
2072 /// # let bump: Bump = Bump::new();
2073 /// let vec_a: BumpVec<u32, _> = BumpVec::from_iter_in([1, 2, 3, 4], &bump);
2074 /// assert_eq!(vec_a.capacity(), 4);
2075 /// assert_eq!(bump.stats().allocated(), 4 * 4);
2076 ///
2077 /// let vec_b: BumpVec<u16, _> = vec_a.map(|i| i as u16);
2078 /// assert_eq!(vec_b.capacity(), 8);
2079 /// assert_eq!(bump.stats().allocated(), 4 * 4);
2080 /// ```
2081 ///
2082 /// Mapping to a type with a higher alignment or size is equivalent to
2083 /// calling `from_iter_in(into_iter().map(f), ...)`:
2084 /// ```
2085 /// # use bump_scope::{Bump, BumpVec};
2086 /// # let bump: Bump = Bump::new();
2087 /// let vec_a: BumpVec<u16, _> = BumpVec::from_iter_in([1, 2, 3, 4], &bump);
2088 /// assert_eq!(vec_a.capacity(), 4);
2089 /// assert_eq!(bump.stats().allocated(), 4 * 2);
2090 ///
2091 /// let vec_b: BumpVec<u32, _> = vec_a.map(|i| i as u32);
2092 /// assert_eq!(vec_b.capacity(), 4);
2093 /// assert_eq!(bump.stats().allocated(), 4 * 2 + 4 * 4);
2094 /// ```
2095 #[inline(always)]
2096 #[cfg(feature = "panic-on-alloc")]
2097 pub fn map<U>(self, f: impl FnMut(T) -> U) -> BumpVec<U, A>
2098 where
2099 A: Clone,
2100 {
2101 panic_on_error(self.generic_map(f))
2102 }
2103
2104 /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2105 ///
2106 /// Compared to `try_from_iter_in(into_iter().map(f), ...)` this method has the advantage that it can reuse the existing allocation.
2107 ///
2108 /// # Errors
2109 /// Errors if the allocation fails. An allocation can only occurs when the alignment or size of `U` is greater than that of `T`.
2110 ///
2111 /// # Examples
2112 /// Mapping to a type with an equal alignment and size (no allocation):
2113 /// ```
2114 /// # use bump_scope::{Bump, BumpVec};
2115 /// # use core::num::NonZero;
2116 /// # let bump: Bump = Bump::new();
2117 /// let a = BumpVec::try_from_iter_exact_in([0, 1, 2], &bump)?;
2118 /// let b = a.try_map(NonZero::new)?;
2119 /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2120 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2121 /// ```
2122 ///
2123 /// Mapping to a type with a smaller alignment and size (no allocation, capacity may grow):
2124 /// ```
2125 /// # use bump_scope::{Bump, BumpVec};
2126 /// # let bump: Bump = Bump::new();
2127 /// let vec_a: BumpVec<u32, _> = BumpVec::try_from_iter_in([1, 2, 3, 4], &bump)?;
2128 /// assert_eq!(vec_a.capacity(), 4);
2129 /// assert_eq!(bump.stats().allocated(), 4 * 4);
2130 ///
2131 /// let vec_b: BumpVec<u16, _> = vec_a.try_map(|i| i as u16)?;
2132 /// assert_eq!(vec_b.capacity(), 8);
2133 /// assert_eq!(bump.stats().allocated(), 4 * 4);
2134 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2135 /// ```
2136 ///
2137 /// Mapping to a type with a higher alignment or size is equivalent to
2138 /// calling `try_from_iter_in(into_iter().map(f), ...)`:
2139 /// ```
2140 /// # use bump_scope::{Bump, BumpVec};
2141 /// # let bump: Bump = Bump::new();
2142 /// let vec_a: BumpVec<u16, _> = BumpVec::try_from_iter_in([1, 2, 3, 4], &bump)?;
2143 /// assert_eq!(vec_a.capacity(), 4);
2144 /// assert_eq!(bump.stats().allocated(), 4 * 2);
2145 ///
2146 /// let vec_b: BumpVec<u32, _> = vec_a.try_map(|i| i as u32)?;
2147 /// assert_eq!(vec_b.capacity(), 4);
2148 /// assert_eq!(bump.stats().allocated(), 4 * 2 + 4 * 4);
2149 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2150 /// ```
2151 #[inline(always)]
2152 pub fn try_map<U>(self, f: impl FnMut(T) -> U) -> Result<BumpVec<U, A>, AllocError>
2153 where
2154 A: Clone,
2155 {
2156 self.generic_map(f)
2157 }
2158
2159 #[inline]
2160 fn generic_map<B: ErrorBehavior, U>(self, mut f: impl FnMut(T) -> U) -> Result<BumpVec<U, A>, B>
2161 where
2162 A: Clone,
2163 {
2164 if !T::IS_ZST && !U::IS_ZST && T::ALIGN >= U::ALIGN && T::SIZE >= U::SIZE {
2165 struct DropGuard<T, U, A: BumpAllocatorTyped> {
2166 ptr: NonNull<T>,
2167 cap: usize,
2168 end: *mut T,
2169 src: *mut T,
2170 dst: *mut U,
2171 allocator: A,
2172 }
2173
2174 impl<T, U, A: BumpAllocatorTyped> DropGuard<T, U, A> {
2175 fn into_allocator(self) -> A {
2176 destructure!(let Self { allocator } = self);
2177 allocator
2178 }
2179 }
2180
2181 impl<T, U, A: BumpAllocatorTyped> Drop for DropGuard<T, U, A> {
2182 fn drop(&mut self) {
2183 unsafe {
2184 // drop `T`s
2185 let drop_ptr = self.src.add(1);
2186 let drop_len = pointer::offset_from_unsigned(self.end, drop_ptr);
2187 ptr::slice_from_raw_parts_mut(drop_ptr, drop_len).drop_in_place();
2188
2189 // drop `U`s
2190 let drop_ptr = self.ptr.as_ptr().cast::<U>();
2191 let drop_len = pointer::offset_from_unsigned(self.dst, drop_ptr);
2192 ptr::slice_from_raw_parts_mut(drop_ptr, drop_len).drop_in_place();
2193
2194 if T::IS_ZST || self.cap == 0 {
2195 return;
2196 }
2197
2198 let layout = Layout::from_size_align_unchecked(self.cap * T::SIZE, T::ALIGN);
2199 self.allocator.deallocate(self.ptr.cast(), layout);
2200 }
2201 }
2202 }
2203
2204 destructure!(let Self { fixed, allocator } = self);
2205 let cap = fixed.capacity();
2206 let slice = unsafe { fixed.cook() }.into_boxed_slice().into_raw();
2207 let ptr = slice.cast::<T>();
2208 let len = slice.len();
2209
2210 unsafe {
2211 let mut guard = DropGuard::<T, U, A> {
2212 ptr,
2213 cap,
2214 end: ptr.as_ptr().add(len),
2215 src: ptr.as_ptr(),
2216 dst: ptr.as_ptr().cast(),
2217 allocator,
2218 };
2219
2220 while guard.src < guard.end {
2221 let src_value = guard.src.read();
2222 let dst_value = f(src_value);
2223 guard.dst.write(dst_value);
2224 guard.src = guard.src.add(1);
2225 guard.dst = guard.dst.add(1);
2226 }
2227
2228 let allocator = guard.into_allocator();
2229 let new_cap = (cap * T::SIZE) / U::SIZE;
2230
2231 Ok(BumpVec {
2232 fixed: RawFixedBumpVec::from_raw_parts(NonNull::slice_from_raw_parts(ptr.cast(), len), new_cap),
2233 allocator,
2234 })
2235 }
2236 } else {
2237 // fallback
2238 let allocator = self.allocator.clone();
2239 Ok(BumpVec::generic_from_iter_exact_in(self.into_iter().map(f), allocator)?)
2240 }
2241 }
2242
2243 /// Returns a vector of the same size as `self`, with function `f` applied to each element in order.
2244 ///
2245 /// 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.
2246 ///
2247 /// # Examples
2248 /// Mapping to a type with an equal alignment and size:
2249 /// ```
2250 /// # use bump_scope::{Bump, BumpVec};
2251 /// # use core::num::NonZero;
2252 /// # let bump: Bump = Bump::new();
2253 /// let a = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2254 /// let b = a.map_in_place(NonZero::new);
2255 /// assert_eq!(format!("{b:?}"), "[None, Some(1), Some(2)]");
2256 /// ```
2257 ///
2258 /// Mapping to a type with a smaller alignment and size:
2259 /// ```
2260 /// # use bump_scope::{Bump, BumpVec};
2261 /// # let bump: Bump = Bump::new();
2262 /// let a: BumpVec<u32, _> = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2263 /// assert_eq!(a.capacity(), 3);
2264 ///
2265 /// let b: BumpVec<u16, _> = a.map_in_place(|i| i as u16);
2266 /// assert_eq!(b.capacity(), 6);
2267 ///
2268 /// assert_eq!(b, [0, 1, 2]);
2269 /// ```
2270 ///
2271 /// Mapping to a type with a greater alignment or size won't compile:
2272 /// ```compile_fail,E0080
2273 /// # use bump_scope::{Bump, BumpVec};
2274 /// # let bump: Bump = Bump::new();
2275 /// let a: BumpVec<u16, _> = BumpVec::from_iter_exact_in([0, 1, 2], &bump);
2276 /// let b: BumpVec<u32, _> = a.map_in_place(|i| i as u32);
2277 /// # _ = b;
2278 /// ```
2279 ///
2280 /// Mapping to a type with a greater size won't compile:
2281 /// ```compile_fail,E0080
2282 /// # use bump_scope::{Bump, BumpVec};
2283 /// # let bump: Bump = Bump::new();
2284 /// let a: BumpVec<u32, _> = BumpVec::from_iter_exact_in([42], &bump);
2285 /// let b: BumpVec<[u32; 2], _> = a.map_in_place(|i| [i; 2]);
2286 /// # _ = b;
2287 /// ```
2288 pub fn map_in_place<U>(self, f: impl FnMut(T) -> U) -> BumpVec<U, A> {
2289 destructure!(let Self { fixed, allocator } = self);
2290
2291 // `FixedBumpVec::map_in_place` handles dropping `T`s and `U`s on panic.
2292 // What is left to do is deallocating the memory.
2293
2294 struct DropGuard<T, A: BumpAllocatorTyped> {
2295 ptr: NonNull<T>,
2296 cap: usize,
2297 allocator: A,
2298 }
2299
2300 impl<T, A: BumpAllocatorTyped> DropGuard<T, A> {
2301 fn into_allocator(self) -> A {
2302 destructure!(let Self { allocator } = self);
2303 allocator
2304 }
2305 }
2306
2307 impl<T, A: BumpAllocatorTyped> Drop for DropGuard<T, A> {
2308 fn drop(&mut self) {
2309 unsafe {
2310 if T::IS_ZST || self.cap == 0 {
2311 return;
2312 }
2313
2314 let layout = Layout::from_size_align_unchecked(self.cap * T::SIZE, T::ALIGN);
2315 self.allocator.deallocate(self.ptr.cast(), layout);
2316 }
2317 }
2318 }
2319
2320 let guard = DropGuard::<T, _> {
2321 ptr: fixed.as_non_null(),
2322 cap: fixed.capacity(),
2323 allocator,
2324 };
2325
2326 let fixed = unsafe { RawFixedBumpVec::from_cooked(fixed.cook().map_in_place(f)) };
2327 let allocator = guard.into_allocator();
2328
2329 BumpVec { fixed, allocator }
2330 }
2331
2332 /// Creates a splicing iterator that replaces the specified range in the vector
2333 /// with the given `replace_with` iterator and yields the removed items.
2334 /// `replace_with` does not need to be the same length as `range`.
2335 ///
2336 /// `range` is removed even if the iterator is not consumed until the end.
2337 ///
2338 /// It is unspecified how many elements are removed from the vector
2339 /// if the `Splice` value is leaked.
2340 ///
2341 /// The input iterator `replace_with` is only consumed when the `Splice` value is dropped.
2342 ///
2343 /// This is optimal if:
2344 ///
2345 /// * The tail (elements in the vector after `range`) is empty,
2346 /// * or `replace_with` yields fewer or equal elements than `range`’s length
2347 /// * or the lower bound of its `size_hint()` is exact.
2348 ///
2349 /// Otherwise, a temporary vector is allocated and the tail is moved twice.
2350 ///
2351 /// # Panics
2352 ///
2353 /// Panics if the starting point is greater than the end point or if
2354 /// the end point is greater than the length of the vector.
2355 ///
2356 /// # Examples
2357 ///
2358 /// ```
2359 /// # use bump_scope::{Bump, bump_vec};
2360 /// # let bump: Bump = Bump::new();
2361 /// let mut v = bump_vec![in ≎ 1, 2, 3, 4];
2362 /// let new = [7, 8, 9];
2363 /// let u = bump.alloc_iter(v.splice(1..3, new));
2364 /// assert_eq!(v, [1, 7, 8, 9, 4]);
2365 /// assert_eq!(u, [2, 3]);
2366 /// ```
2367 #[cfg(feature = "panic-on-alloc")]
2368 #[inline]
2369 pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A>
2370 where
2371 R: RangeBounds<usize>,
2372 I: IntoIterator<Item = T>,
2373 {
2374 // Memory safety
2375 //
2376 // When the Drain is first created, it shortens the length of
2377 // the source vector to make sure no uninitialized or moved-from elements
2378 // are accessible at all if the Drain's destructor never gets to run.
2379 //
2380 // Drain will ptr::read out the values to remove.
2381 // When finished, remaining tail of the vec is copied back to cover
2382 // the hole, and the vector length is restored to the new length.
2383
2384 use core::ops::Range;
2385 let len = self.len();
2386 let Range { start, end } = slice::range(range, ..len);
2387
2388 let drain = unsafe {
2389 // set self.vec length's to start, to be safe in case Drain is leaked
2390 self.set_len(start);
2391 let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start);
2392
2393 Drain {
2394 tail_start: end,
2395 tail_len: len - end,
2396 iter: range_slice.iter(),
2397 vec: NonNull::from(self),
2398 }
2399 };
2400
2401 Splice {
2402 drain,
2403 replace_with: replace_with.into_iter(),
2404 }
2405 }
2406
2407 /// Like [`reserve`] but allows you to provide a different `len`.
2408 ///
2409 /// This helps with algorithms from the standard library that make use of
2410 /// `RawVec::reserve` which behaves the same.
2411 ///
2412 /// # Safety
2413 ///
2414 /// - `len` must be less than or equal to `self.capacity()`
2415 ///
2416 /// [`reserve`]: Self::reserve
2417 #[inline(always)]
2418 #[cfg(feature = "panic-on-alloc")]
2419 pub(crate) unsafe fn buf_reserve(&mut self, len: usize, additional: usize) {
2420 unsafe {
2421 if additional > (self.capacity() - len) {
2422 panic_on_error(self.generic_grow_amortized_buf(len, additional));
2423 }
2424 }
2425 }
2426
2427 /// Extend the vector by `n` clones of value.
2428 fn extend_with<B: ErrorBehavior>(&mut self, n: usize, value: T) -> Result<(), B>
2429 where
2430 T: Clone,
2431 {
2432 self.generic_reserve(n)?;
2433 unsafe {
2434 self.fixed.cook_mut().extend_with_unchecked(n, value);
2435 }
2436 Ok(())
2437 }
2438
2439 #[inline(always)]
2440 unsafe fn extend_by_copy_nonoverlapping<E: ErrorBehavior>(&mut self, other: *const [T]) -> Result<(), E> {
2441 unsafe {
2442 let len = other.len();
2443 self.generic_reserve(len)?;
2444
2445 let src = other.cast::<T>();
2446 let dst = self.as_mut_ptr().add(self.len());
2447 ptr::copy_nonoverlapping(src, dst, len);
2448
2449 self.inc_len(len);
2450 Ok(())
2451 }
2452 }
2453
2454 #[inline]
2455 fn generic_reserve_one<E: ErrorBehavior>(&mut self) -> Result<(), E> {
2456 if self.capacity() == self.len() {
2457 self.generic_grow_amortized::<E>(1)?;
2458 }
2459
2460 Ok(())
2461 }
2462
2463 #[cold]
2464 #[inline(never)]
2465 fn generic_grow_amortized<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2466 if T::IS_ZST {
2467 // This function is only called after we checked that the current capacity is not
2468 // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2469 return Err(E::capacity_overflow());
2470 }
2471
2472 let Some(required_cap) = self.len().checked_add(additional) else {
2473 return Err(E::capacity_overflow())?;
2474 };
2475
2476 let new_cap = self.capacity().checked_mul(2).unwrap_or(required_cap).max(required_cap);
2477 let new_cap = new_cap.max(min_non_zero_cap(T::SIZE));
2478
2479 unsafe { self.generic_grow_to(new_cap) }
2480 }
2481
2482 /// Like [`reserve`] but allows you to provide a different `len`.
2483 ///
2484 /// This is only used for [`buf_reserve`], read its documentation for more.
2485 ///
2486 /// # Safety
2487 ///
2488 /// - `len` must be less than or equal to `self.capacity()`
2489 ///
2490 /// [`reserve`]: Self::reserve
2491 /// [`buf_reserve`]: Self::buf_reserve
2492 #[cold]
2493 #[inline(never)]
2494 #[cfg(feature = "panic-on-alloc")]
2495 unsafe fn generic_grow_amortized_buf<E: ErrorBehavior>(&mut self, len: usize, additional: usize) -> Result<(), E> {
2496 if T::IS_ZST {
2497 // This function is only called after we checked that the current capacity is not
2498 // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2499 return Err(E::capacity_overflow());
2500 }
2501
2502 let Some(required_cap) = len.checked_add(additional) else {
2503 return Err(E::capacity_overflow())?;
2504 };
2505
2506 // This guarantees exponential growth. The doubling cannot overflow
2507 // because `capacity <= isize::MAX` and the type of `capacity` is usize;
2508 let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));
2509
2510 unsafe { self.generic_grow_to(new_cap) }
2511 }
2512
2513 #[cold]
2514 #[inline(never)]
2515 fn generic_grow_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2516 if T::IS_ZST {
2517 // This function is only called after we checked that the current capacity is not
2518 // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2519 return Err(E::capacity_overflow());
2520 }
2521
2522 let Some(required_cap) = self.len().checked_add(additional) else {
2523 return Err(E::capacity_overflow())?;
2524 };
2525
2526 unsafe { self.generic_grow_to(required_cap) }
2527 }
2528
2529 /// # Safety
2530 ///
2531 /// `new_capacity` must be greater than the current capacity.
2532 unsafe fn generic_grow_to<E: ErrorBehavior>(&mut self, new_capacity: usize) -> Result<(), E> {
2533 unsafe {
2534 let new_cap = new_capacity;
2535
2536 if self.capacity() == 0 {
2537 self.fixed = RawFixedBumpVec::allocate(&self.allocator, new_cap)?;
2538 return Ok(());
2539 }
2540
2541 let old_ptr = self.as_non_null().cast();
2542
2543 let old_size = self.capacity() * T::SIZE; // we already allocated that amount so this can't overflow
2544 let Some(new_size) = new_cap.checked_mul(T::SIZE) else {
2545 return Err(E::capacity_overflow());
2546 };
2547
2548 let old_layout = Layout::from_size_align_unchecked(old_size, T::ALIGN);
2549 let Ok(new_layout) = Layout::from_size_align(new_size, T::ALIGN) else {
2550 return Err(E::capacity_overflow());
2551 };
2552
2553 let new_ptr = match self.allocator.grow(old_ptr, old_layout, new_layout) {
2554 Ok(ok) => ok.cast(),
2555 Err(_) => return Err(E::allocation(new_layout)),
2556 };
2557
2558 self.fixed.set_ptr(new_ptr);
2559 self.fixed.set_cap(new_cap);
2560
2561 Ok(())
2562 }
2563 }
2564
2565 /// Shrinks the capacity of the vector as much as possible.
2566 ///
2567 /// Shrinking will only take place if this vector owns the last allocation.
2568 ///
2569 /// # Examples
2570 /// ```
2571 /// # use bump_scope::{Bump, BumpVec, bump_vec};
2572 /// # let bump: Bump = Bump::new();
2573 /// let mut vec: BumpVec<u8, _> = bump_vec![in ≎ 1, 2, 3];
2574 /// assert!(vec.len() == 3);
2575 /// assert!(vec.capacity() == 3);
2576 /// assert_eq!(bump.stats().allocated(), 3);
2577 ///
2578 /// vec.pop();
2579 /// vec.shrink_to_fit();
2580 /// assert!(vec.len() == 2);
2581 /// assert!(vec.capacity() == 2);
2582 /// assert_eq!(bump.stats().allocated(), 2);
2583 ///
2584 /// // allocate something so the vec is no longer the last allocation
2585 /// bump.alloc::<u8>(123);
2586 ///
2587 /// // now shrinking won't take place, capacity stays the same
2588 /// vec.pop();
2589 /// vec.shrink_to_fit();
2590 /// assert!(vec.len() == 1);
2591 /// assert!(vec.capacity() == 2);
2592 /// ```
2593 pub fn shrink_to_fit(&mut self) {
2594 let Self { fixed, allocator } = self;
2595
2596 let old_ptr = fixed.as_non_null();
2597 let old_cap = fixed.capacity();
2598 let new_cap = fixed.len();
2599
2600 if T::IS_ZST || old_cap == 0 {
2601 return;
2602 }
2603
2604 unsafe {
2605 if let Some(new_ptr) = allocator.shrink_slice(old_ptr, old_cap, new_cap) {
2606 fixed.set_ptr(new_ptr);
2607 fixed.set_cap(new_cap);
2608 }
2609 }
2610 }
2611
2612 /// # Safety
2613 ///
2614 /// `iterator` must satisfy the invariants of nightly's `TrustedLen`.
2615 unsafe fn extend_trusted<B: ErrorBehavior>(&mut self, iterator: impl Iterator<Item = T>) -> Result<(), B> {
2616 unsafe {
2617 let (low, high) = iterator.size_hint();
2618 if let Some(additional) = high {
2619 debug_assert_eq!(
2620 low,
2621 additional,
2622 "TrustedLen iterator's size hint is not exact: {:?}",
2623 (low, high)
2624 );
2625
2626 self.generic_reserve(additional)?;
2627
2628 let ptr = self.as_mut_ptr();
2629 let mut local_len = self.fixed.set_len_on_drop();
2630
2631 iterator.for_each(move |element| {
2632 let dst = ptr.add(local_len.current_len());
2633
2634 ptr::write(dst, element);
2635 // Since the loop executes user code which can panic we have to update
2636 // the length every step to correctly drop what we've written.
2637 // NB can't overflow since we would have had to alloc the address space
2638 local_len.increment_len(1);
2639 });
2640
2641 Ok(())
2642 } else {
2643 // Per TrustedLen contract a `None` upper bound means that the iterator length
2644 // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
2645 // Since the other branch already panics eagerly (via `reserve()`) we do the same here.
2646 // This avoids additional codegen for a fallback code path which would eventually
2647 // panic anyway.
2648 Err(B::capacity_overflow())
2649 }
2650 }
2651 }
2652
2653 /// Retains only the elements specified by the predicate, passing a mutable reference to it.
2654 ///
2655 /// In other words, remove all elements `e` such that `f(&mut e)` returns `false`.
2656 /// This method operates in place, visiting each element exactly once in the
2657 /// original order, and preserves the order of the retained elements.
2658 ///
2659 /// # Examples
2660 ///
2661 /// ```
2662 /// # use bump_scope::{Bump, bump_vec};
2663 /// # let bump: Bump = Bump::new();
2664 /// #
2665 /// let mut vec = bump_vec![in ≎ 1, 2, 3, 4];
2666 ///
2667 /// vec.retain(|x| if *x <= 3 {
2668 /// *x += 1;
2669 /// true
2670 /// } else {
2671 /// false
2672 /// });
2673 ///
2674 /// assert_eq!(vec, [2, 3, 4]);
2675 /// ```
2676 #[expect(clippy::pedantic)]
2677 pub fn retain<F>(&mut self, f: F)
2678 where
2679 F: FnMut(&mut T) -> bool,
2680 {
2681 unsafe { self.fixed.cook_mut() }.retain(f)
2682 }
2683
2684 /// Removes the specified range from the vector in bulk, returning all
2685 /// removed elements as an iterator. If the iterator is dropped before
2686 /// being fully consumed, it drops the remaining removed elements.
2687 ///
2688 /// The returned iterator keeps a mutable borrow on the vector to optimize
2689 /// its implementation.
2690 ///
2691 /// # Panics
2692 ///
2693 /// Panics if the starting point is greater than the end point or if
2694 /// the end point is greater than the length of the vector.
2695 ///
2696 /// # Leaking
2697 ///
2698 /// If the returned iterator goes out of scope without being dropped (due to
2699 /// [`mem::forget`](core::mem::forget), for example), the vector may have lost and leaked
2700 /// elements arbitrarily, including elements outside the range.
2701 ///
2702 /// # Examples
2703 ///
2704 /// ```
2705 /// # use bump_scope::{Bump, bump_vec};
2706 /// # let bump: Bump = Bump::new();
2707 /// let mut v = bump_vec![in ≎ 1, 2, 3];
2708 /// let u = bump.alloc_iter(v.drain(1..));
2709 /// assert_eq!(v, [1]);
2710 /// assert_eq!(u, [2, 3]);
2711 ///
2712 /// // A full range clears the vector, like `clear()` does
2713 /// v.drain(..);
2714 /// assert_eq!(v, []);
2715 /// ```
2716 pub fn drain<R>(&mut self, range: R) -> owned_slice::Drain<'_, T>
2717 where
2718 R: RangeBounds<usize>,
2719 {
2720 unsafe { self.fixed.cook_mut() }.drain(range)
2721 }
2722
2723 /// Creates an iterator which uses a closure to determine if an element should be removed.
2724 ///
2725 /// If the closure returns true, then the element is removed and yielded.
2726 /// If the closure returns false, the element will remain in the vector and will not be yielded
2727 /// by the iterator.
2728 ///
2729 /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
2730 /// or the iteration short-circuits, then the remaining elements will be retained.
2731 /// Use [`retain`] with a negated predicate if you do not need the returned iterator.
2732 ///
2733 /// Using this method is equivalent to the following code:
2734 ///
2735 /// ```
2736 /// # use bump_scope::{Bump, bump_vec};
2737 /// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 };
2738 /// # let bump: Bump = Bump::new();
2739 /// # let mut vec = bump_vec![in ≎ 1, 2, 3, 4, 5, 6];
2740 /// let mut i = 0;
2741 /// while i < vec.len() {
2742 /// if some_predicate(&mut vec[i]) {
2743 /// let val = vec.remove(i);
2744 /// // your code here
2745 /// # _ = val;
2746 /// } else {
2747 /// i += 1;
2748 /// }
2749 /// }
2750 ///
2751 /// # assert_eq!(vec, [1, 4, 5]);
2752 /// ```
2753 ///
2754 /// But `extract_if` is easier to use. `extract_if` is also more efficient,
2755 /// because it can backshift the elements of the array in bulk.
2756 ///
2757 /// Note that `extract_if` also lets you mutate every element in the filter closure,
2758 /// regardless of whether you choose to keep or remove it.
2759 ///
2760 /// # Examples
2761 ///
2762 /// Splitting an array into evens and odds, reusing the original allocation:
2763 ///
2764 /// ```
2765 /// # use bump_scope::{Bump, bump_vec};
2766 /// # let bump: Bump = Bump::new();
2767 /// let mut numbers = bump_vec![in ≎ 1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15];
2768 ///
2769 /// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
2770 /// let odds = numbers;
2771 ///
2772 /// assert_eq!(evens, [2, 4, 6, 8, 14]);
2773 /// assert_eq!(odds, [1, 3, 5, 9, 11, 13, 15]);
2774 /// ```
2775 ///
2776 /// [`retain`]: Self::retain
2777 pub fn extract_if<F>(&mut self, filter: F) -> owned_slice::ExtractIf<'_, T, F>
2778 where
2779 F: FnMut(&mut T) -> bool,
2780 {
2781 unsafe { self.fixed.cook_mut() }.extract_if(filter)
2782 }
2783
2784 /// Removes consecutive repeated elements in the vector according to the
2785 /// [`PartialEq`] trait implementation.
2786 ///
2787 /// If the vector is sorted, this removes all duplicates.
2788 ///
2789 /// # Examples
2790 ///
2791 /// ```
2792 /// # use bump_scope::{Bump, bump_vec};
2793 /// # let bump: Bump = Bump::new();
2794 /// let mut vec = bump_vec![in ≎ 1, 2, 2, 3, 2];
2795 ///
2796 /// vec.dedup();
2797 ///
2798 /// assert_eq!(vec, [1, 2, 3, 2]);
2799 /// ```
2800 #[inline]
2801 pub fn dedup(&mut self)
2802 where
2803 T: PartialEq,
2804 {
2805 unsafe { self.fixed.cook_mut() }.dedup();
2806 }
2807
2808 /// Removes all but the first of consecutive elements in the vector that resolve to the same
2809 /// key.
2810 ///
2811 /// If the vector is sorted, this removes all duplicates.
2812 ///
2813 /// # Examples
2814 ///
2815 /// ```
2816 /// # use bump_scope::{Bump, bump_vec};
2817 /// # let bump: Bump = Bump::new();
2818 /// let mut vec = bump_vec![in ≎ 10, 20, 21, 30, 20];
2819 ///
2820 /// vec.dedup_by_key(|i| *i / 10);
2821 ///
2822 /// assert_eq!(vec, [10, 20, 30, 20]);
2823 /// ```
2824 #[inline]
2825 pub fn dedup_by_key<F, K>(&mut self, key: F)
2826 where
2827 F: FnMut(&mut T) -> K,
2828 K: PartialEq,
2829 {
2830 unsafe { self.fixed.cook_mut() }.dedup_by_key(key);
2831 }
2832
2833 /// Removes all but the first of consecutive elements in the vector satisfying a given equality
2834 /// relation.
2835 ///
2836 /// The `same_bucket` function is passed references to two elements from the vector and
2837 /// must determine if the elements compare equal. The elements are passed in opposite order
2838 /// from their order in the vector, so if `same_bucket(a, b)` returns `true`, `a` is removed.
2839 ///
2840 /// If the vector is sorted, this removes all duplicates.
2841 ///
2842 /// # Examples
2843 ///
2844 /// ```
2845 /// # use bump_scope::{Bump, bump_vec};
2846 /// # let bump: Bump = Bump::new();
2847 /// let mut vec = bump_vec![in ≎ "foo", "bar", "Bar", "baz", "bar"];
2848 ///
2849 /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
2850 ///
2851 /// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
2852 /// ```
2853 pub fn dedup_by<F>(&mut self, same_bucket: F)
2854 where
2855 F: FnMut(&mut T, &mut T) -> bool,
2856 {
2857 unsafe { self.fixed.cook_mut() }.dedup_by(same_bucket);
2858 }
2859
2860 /// Returns the remaining spare capacity of the vector as a slice of
2861 /// `MaybeUninit<T>`.
2862 ///
2863 /// The returned slice can be used to fill the vector with data (e.g. by
2864 /// reading from a file) before marking the data as initialized using the
2865 /// [`set_len`] method.
2866 ///
2867 /// [`set_len`]: Self::set_len
2868 ///
2869 /// # Examples
2870 ///
2871 /// ```
2872 /// # use bump_scope::{Bump, BumpVec};
2873 /// # let bump: Bump = Bump::new();
2874 /// // Allocate vector big enough for 10 elements.
2875 /// let mut v = BumpVec::with_capacity_in(10, &bump);
2876 ///
2877 /// // Fill in the first 3 elements.
2878 /// let uninit = v.spare_capacity_mut();
2879 /// uninit[0].write(0);
2880 /// uninit[1].write(1);
2881 /// uninit[2].write(2);
2882 ///
2883 /// // Mark the first 3 elements of the vector as being initialized.
2884 /// unsafe {
2885 /// v.set_len(3);
2886 /// }
2887 ///
2888 /// assert_eq!(&v, &[0, 1, 2]);
2889 /// ```
2890 #[inline]
2891 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
2892 // Note:
2893 // This method is not implemented in terms of `split_at_spare_mut`,
2894 // to prevent invalidation of pointers to the buffer.
2895 unsafe {
2896 slice::from_raw_parts_mut(
2897 self.as_mut_ptr().add(self.len()).cast::<MaybeUninit<T>>(),
2898 self.capacity() - self.len(),
2899 )
2900 }
2901 }
2902
2903 /// Returns vector content as a slice of `T`, along with the remaining spare
2904 /// capacity of the vector as a slice of `MaybeUninit<T>`.
2905 ///
2906 /// The returned spare capacity slice can be used to fill the vector with data
2907 /// (e.g. by reading from a file) before marking the data as initialized using
2908 /// the [`set_len`] method.
2909 ///
2910 /// [`set_len`]: Self::set_len
2911 ///
2912 /// Note that this is a low-level API, which should be used with care for
2913 /// optimization purposes. If you need to append data to a `BumpVec`
2914 /// you can use [`push`], [`extend`], `extend_from_slice`[`_copy`](BumpVec::extend_from_slice_copy)`/`[`_clone`](BumpVec::extend_from_within_clone),
2915 /// `extend_from_within`[`_copy`](BumpVec::extend_from_within_copy)`/`[`_clone`](BumpVec::extend_from_within_clone), [`insert`], [`resize`] or
2916 /// [`resize_with`], depending on your exact needs.
2917 ///
2918 /// [`push`]: Self::push
2919 /// [`extend`]: Self::extend
2920 /// [`insert`]: Self::insert
2921 /// [`append`]: Self::append
2922 /// [`resize`]: Self::resize
2923 /// [`resize_with`]: Self::resize_with
2924 #[inline]
2925 pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
2926 let ptr = self.as_mut_ptr();
2927
2928 // SAFETY:
2929 // - `ptr` is guaranteed to be valid for `self.len` elements
2930 // - but the allocation extends out to `self.buf.capacity()` elements, possibly
2931 // uninitialized
2932 let spare_ptr = unsafe { ptr.add(self.len()) };
2933 let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
2934 let spare_len = self.capacity() - self.len();
2935
2936 // SAFETY:
2937 // - `ptr` is guaranteed to be valid for `self.len` elements
2938 // - `spare_ptr` is pointing one element past the buffer, so it doesn't overlap with `initialized`
2939 unsafe {
2940 let initialized = slice::from_raw_parts_mut(ptr, self.len());
2941 let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
2942
2943 (initialized, spare)
2944 }
2945 }
2946
2947 /// Returns a reference to the allocator.
2948 #[must_use]
2949 #[inline(always)]
2950 pub fn allocator(&self) -> &A {
2951 &self.allocator
2952 }
2953
2954 /// Returns a type which provides statistics about the memory usage of the bump allocator.
2955 ///
2956 /// This is equivalent to calling `.allocator().stats()`.
2957 /// This merely exists for api parity with `Mut*` collections which can't have a `allocator` method.
2958 #[must_use]
2959 #[inline(always)]
2960 pub fn allocator_stats(&self) -> A::TypedStats<'_> {
2961 self.allocator.typed_stats()
2962 }
2963}
2964
2965impl<'a, T, A: BumpAllocatorTypedScope<'a>> BumpVec<T, A> {
2966 /// Turns this `BumpVec<T>` into a `FixedBumpVec<T>`.
2967 ///
2968 /// This retains the unused capacity unlike <code>[into_](Self::into_slice)([boxed_](Self::into_boxed_slice))[slice](Self::into_slice)</code>.
2969 #[must_use]
2970 #[inline(always)]
2971 pub fn into_fixed_vec(self) -> FixedBumpVec<'a, T> {
2972 self.into_parts().0
2973 }
2974
2975 /// Turns this `BumpVec<T>` into a `BumpBox<[T]>`.
2976 #[must_use]
2977 #[inline(always)]
2978 pub fn into_boxed_slice(mut self) -> BumpBox<'a, [T]> {
2979 self.shrink_to_fit();
2980 self.into_fixed_vec().into_boxed_slice()
2981 }
2982
2983 /// Turns this `BumpVec<T>` into a `&[T]` that is live for this bump scope.
2984 ///
2985 /// This is only available for [`NoDrop`] types so you don't omit dropping a value for which it matters.
2986 ///
2987 /// `!NoDrop` types can still be turned into slices via <code>BumpBox::[leak](BumpBox::leak)(vec.[into_boxed_slice](Self::into_boxed_slice)())</code>.
2988 #[must_use]
2989 #[inline(always)]
2990 pub fn into_slice(self) -> &'a mut [T]
2991 where
2992 [T]: NoDrop,
2993 {
2994 self.into_boxed_slice().into_mut()
2995 }
2996
2997 /// Creates a `BumpVec<T>` from its parts.
2998 ///
2999 /// The provided `bump` does not have to be the one the `fixed_vec` was allocated in.
3000 ///
3001 /// ```
3002 /// # use bump_scope::{Bump, FixedBumpVec, BumpVec};
3003 /// # let bump: Bump = Bump::new();
3004 /// let mut fixed_vec = FixedBumpVec::with_capacity_in(3, &bump);
3005 /// fixed_vec.push(1);
3006 /// fixed_vec.push(2);
3007 /// fixed_vec.push(3);
3008 /// let mut vec = BumpVec::from_parts(fixed_vec, &bump);
3009 /// vec.push(4);
3010 /// vec.push(5);
3011 /// assert_eq!(vec, [1, 2, 3, 4, 5]);
3012 /// ```
3013 #[must_use]
3014 #[inline(always)]
3015 pub fn from_parts(vec: FixedBumpVec<'a, T>, allocator: A) -> Self {
3016 Self {
3017 fixed: unsafe { RawFixedBumpVec::from_cooked(vec) },
3018 allocator,
3019 }
3020 }
3021
3022 /// Turns this `BumpVec<T>` into its parts.
3023 /// ```
3024 /// # use bump_scope::{Bump, BumpVec};
3025 /// # let bump: Bump = Bump::new();
3026 /// let mut vec = BumpVec::new_in(&bump);
3027 /// vec.reserve(10);
3028 /// vec.push(1);
3029 /// let fixed_vec = vec.into_parts().0;
3030 /// assert_eq!(fixed_vec.capacity(), 10);
3031 /// assert_eq!(fixed_vec, [1]);
3032 /// ```
3033 #[must_use]
3034 #[inline(always)]
3035 pub fn into_parts(self) -> (FixedBumpVec<'a, T>, A) {
3036 destructure!(let Self { fixed, allocator } = self);
3037 (unsafe { fixed.cook() }, allocator)
3038 }
3039}
3040
3041impl<T, const N: usize, A: BumpAllocatorTyped> BumpVec<[T; N], A> {
3042 /// Takes a `BumpVec<[T; N]>` and flattens it into a `BumpVec<T>`.
3043 ///
3044 /// # Panics
3045 ///
3046 /// Panics if the length of the resulting vector would overflow a `usize`.
3047 ///
3048 /// This is only possible when flattening a vector of arrays of zero-sized
3049 /// types, and thus tends to be irrelevant in practice. If
3050 /// `size_of::<T>() > 0`, this will never panic.
3051 ///
3052 /// # Examples
3053 ///
3054 /// ```
3055 /// # use bump_scope::{Bump, bump_vec};
3056 /// # let bump: Bump = Bump::new();
3057 /// #
3058 /// let mut vec = bump_vec![in ≎ [1, 2, 3], [4, 5, 6], [7, 8, 9]];
3059 /// assert_eq!(vec.pop(), Some([7, 8, 9]));
3060 ///
3061 /// let mut flattened = vec.into_flattened();
3062 /// assert_eq!(flattened.pop(), Some(6));
3063 /// ```
3064 #[must_use]
3065 pub fn into_flattened(self) -> BumpVec<T, A> {
3066 destructure!(let Self { fixed, allocator } = self);
3067 let fixed = unsafe { RawFixedBumpVec::from_cooked(fixed.cook().into_flattened()) };
3068 BumpVec { fixed, allocator }
3069 }
3070}
3071
3072impl<T: Debug, A: BumpAllocatorTyped> Debug for BumpVec<T, A> {
3073 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3074 Debug::fmt(self.as_slice(), f)
3075 }
3076}
3077
3078impl<T, A: BumpAllocatorTyped, I: SliceIndex<[T]>> Index<I> for BumpVec<T, A> {
3079 type Output = I::Output;
3080
3081 #[inline(always)]
3082 fn index(&self, index: I) -> &Self::Output {
3083 Index::index(self.as_slice(), index)
3084 }
3085}
3086
3087impl<T, A: BumpAllocatorTyped, I: SliceIndex<[T]>> IndexMut<I> for BumpVec<T, A> {
3088 #[inline(always)]
3089 fn index_mut(&mut self, index: I) -> &mut Self::Output {
3090 IndexMut::index_mut(self.as_mut_slice(), index)
3091 }
3092}
3093
3094#[cfg(feature = "panic-on-alloc")]
3095impl<T, A: BumpAllocatorTyped> Extend<T> for BumpVec<T, A> {
3096 #[inline]
3097 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
3098 let iter = iter.into_iter();
3099
3100 self.reserve(iter.size_hint().0);
3101
3102 for value in iter {
3103 self.push(value);
3104 }
3105 }
3106}
3107
3108#[cfg(feature = "panic-on-alloc")]
3109impl<'t, T: Clone + 't, A: BumpAllocatorTyped> Extend<&'t T> for BumpVec<T, A> {
3110 #[inline]
3111 fn extend<I: IntoIterator<Item = &'t T>>(&mut self, iter: I) {
3112 let iter = iter.into_iter();
3113
3114 self.reserve(iter.size_hint().0);
3115
3116 for value in iter {
3117 self.push(value.clone());
3118 }
3119 }
3120}
3121
3122impl<T, A: BumpAllocatorTyped> IntoIterator for BumpVec<T, A> {
3123 type Item = T;
3124 type IntoIter = IntoIter<T, A>;
3125
3126 #[inline]
3127 fn into_iter(self) -> Self::IntoIter {
3128 unsafe {
3129 destructure!(let Self { fixed, allocator } = self);
3130
3131 let (slice, cap) = fixed.into_raw_parts();
3132 let begin = slice.cast::<T>();
3133
3134 let end = if T::IS_ZST {
3135 non_null::wrapping_byte_add(begin, slice.len())
3136 } else {
3137 begin.add(slice.len())
3138 };
3139
3140 IntoIter {
3141 buf: begin,
3142 cap,
3143
3144 ptr: begin,
3145 end,
3146
3147 allocator,
3148 marker: PhantomData,
3149 }
3150 }
3151 }
3152}
3153
3154impl<'c, T, A: BumpAllocatorTyped> IntoIterator for &'c BumpVec<T, A> {
3155 type Item = &'c T;
3156 type IntoIter = slice::Iter<'c, T>;
3157
3158 #[inline(always)]
3159 fn into_iter(self) -> Self::IntoIter {
3160 self.as_slice().iter()
3161 }
3162}
3163
3164impl<'c, T, A: BumpAllocatorTyped> IntoIterator for &'c mut BumpVec<T, A> {
3165 type Item = &'c mut T;
3166 type IntoIter = slice::IterMut<'c, T>;
3167
3168 #[inline(always)]
3169 fn into_iter(self) -> Self::IntoIter {
3170 self.as_mut_slice().iter_mut()
3171 }
3172}
3173
3174impl<T, A: BumpAllocatorTyped> AsRef<[T]> for BumpVec<T, A> {
3175 #[inline(always)]
3176 fn as_ref(&self) -> &[T] {
3177 self
3178 }
3179}
3180
3181impl<T, A: BumpAllocatorTyped> AsMut<[T]> for BumpVec<T, A> {
3182 #[inline(always)]
3183 fn as_mut(&mut self) -> &mut [T] {
3184 self
3185 }
3186}
3187
3188impl<T, A: BumpAllocatorTyped> Borrow<[T]> for BumpVec<T, A> {
3189 #[inline(always)]
3190 fn borrow(&self) -> &[T] {
3191 self
3192 }
3193}
3194
3195impl<T, A: BumpAllocatorTyped> BorrowMut<[T]> for BumpVec<T, A> {
3196 #[inline(always)]
3197 fn borrow_mut(&mut self) -> &mut [T] {
3198 self
3199 }
3200}
3201
3202impl<T: Hash, A: BumpAllocatorTyped> Hash for BumpVec<T, A> {
3203 #[inline(always)]
3204 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
3205 self.as_slice().hash(state);
3206 }
3207}
3208
3209/// Returns [`ErrorKind::OutOfMemory`](std::io::ErrorKind::OutOfMemory) when allocations fail.
3210#[cfg(feature = "std")]
3211impl<A: BumpAllocatorTyped> std::io::Write for BumpVec<u8, A> {
3212 #[inline(always)]
3213 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
3214 if self.try_extend_from_slice_copy(buf).is_err() {
3215 return Err(std::io::ErrorKind::OutOfMemory.into());
3216 }
3217
3218 Ok(buf.len())
3219 }
3220
3221 #[inline(always)]
3222 fn flush(&mut self) -> std::io::Result<()> {
3223 Ok(())
3224 }
3225
3226 #[inline]
3227 fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
3228 let len = bufs.iter().map(|b| b.len()).sum();
3229 self.try_reserve(len).map_err(|_| std::io::ErrorKind::OutOfMemory)?;
3230 for buf in bufs {
3231 self.try_extend_from_slice_copy(buf)
3232 .map_err(|_| std::io::ErrorKind::OutOfMemory)?;
3233 }
3234 Ok(len)
3235 }
3236
3237 #[inline(always)]
3238 fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
3239 if self.try_extend_from_slice_copy(buf).is_err() {
3240 return Err(std::io::ErrorKind::OutOfMemory.into());
3241 }
3242
3243 Ok(())
3244 }
3245}
3246
3247#[cfg(feature = "panic-on-alloc")]
3248impl<T, A: BumpAllocatorTyped + Default> FromIterator<T> for BumpVec<T, A> {
3249 #[inline]
3250 #[track_caller]
3251 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3252 Self::from_iter_in(iter, A::default())
3253 }
3254}