bump_scope/mut_bump_vec_rev.rs
1use core::{
2 borrow::{Borrow, BorrowMut},
3 fmt::Debug,
4 hash::Hash,
5 iter,
6 marker::PhantomData,
7 mem::{ManuallyDrop, MaybeUninit},
8 ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
9 panic::{RefUnwindSafe, UnwindSafe},
10 ptr::{self, NonNull},
11 slice::{self, SliceIndex},
12};
13
14use crate::{
15 BumpBox, ErrorBehavior, MutBumpAllocatorExt, MutBumpAllocatorScopeExt, NoDrop, SetLenOnDrop, SizedTypeProperties,
16 alloc::AllocError,
17 destructure::destructure,
18 min_non_zero_cap,
19 mut_bump_vec::IntoIter,
20 owned_slice::{OwnedSlice, TakeOwnedSlice},
21 polyfill::{self, hint::likely, non_null, pointer},
22};
23
24#[cfg(feature = "panic-on-alloc")]
25use crate::panic_on_error;
26
27/// This is like [`vec!`](alloc_crate::vec!) but allocates inside a bump allocator, returning a [`MutBumpVecRev`].
28///
29/// `$bump` can be any type that implements [`MutBumpAllocatorExt`].
30///
31/// # Panics
32/// If used without `try`, panics on allocation failure.
33///
34/// # Errors
35/// If used with `try`, errors on allocation failure.
36///
37/// # Examples
38///
39/// There are three forms of this macro:
40///
41/// - Create an empty [`MutBumpVecRev`]:
42/// ```
43/// # use bump_scope::{Bump, mut_bump_vec_rev, MutBumpVecRev};
44/// # let mut bump: Bump = Bump::new();
45/// let vec: MutBumpVecRev<i32, _> = mut_bump_vec_rev![in &mut bump];
46/// assert!(vec.is_empty());
47/// ```
48///
49/// - Create a [`MutBumpVecRev`] containing a given list of elements:
50///
51/// ```
52/// # use bump_scope::{Bump, mut_bump_vec_rev};
53/// # let mut bump: Bump = Bump::new();
54/// let vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
55/// assert_eq!(vec[0], 1);
56/// assert_eq!(vec[1], 2);
57/// assert_eq!(vec[2], 3);
58/// ```
59///
60/// - Create a [`MutBumpVecRev`] from a given element and size:
61///
62/// ```
63/// # use bump_scope::{Bump, mut_bump_vec_rev};
64/// # let mut bump: Bump = Bump::new();
65/// let vec = mut_bump_vec_rev![in &mut bump; 1; 3];
66/// assert_eq!(vec, [1, 1, 1]);
67/// ```
68///
69/// Note that unlike array expressions this syntax supports all elements
70/// which implement [`Clone`] and the number of elements doesn't have to be
71/// a constant.
72///
73/// This will use `clone` to duplicate an expression, so one should be careful
74/// using this with types having a nonstandard `Clone` implementation. For
75/// example, `mut_bump_vec_rev![in &mut bump; Rc::new(1); 5]` will create a vector of five references
76/// to the same boxed integer value, not five references pointing to independently
77/// boxed integers.
78///
79/// Also, note that `mut_bump_vec_rev![in &mut bump; expr; 0]` is allowed, and produces an empty vector.
80/// This will still evaluate `expr`, however, and immediately drop the resulting value, so
81/// be mindful of side effects.
82#[macro_export]
83macro_rules! mut_bump_vec_rev {
84 [in $bump:expr] => {
85 $crate::MutBumpVecRev::new_in($bump)
86 };
87 [in $bump:expr; $($values:expr),* $(,)?] => {
88 $crate::__mut_bump_vec_rev_panic_on_alloc![in $bump; $($values),*]
89 };
90 [in $bump:expr; $value:expr; $count:expr] => {
91 $crate::__mut_bump_vec_rev_panic_on_alloc![in $bump; $value; $count]
92 };
93 [try in $bump:expr] => {
94 Ok::<_, $crate::alloc::AllocError>($crate::MutBumpVecRev::new_in($bump))
95 };
96 [try in $bump:expr; $($values:expr),* $(,)?] => {
97 $crate::MutBumpVecRev::try_from_owned_slice_in([$($values),*], $bump)
98 };
99 [try in $bump:expr; $value:expr; $count:expr] => {
100 $crate::MutBumpVecRev::try_from_elem_in($value, $count, $bump)
101 };
102}
103
104#[doc(hidden)]
105#[macro_export]
106#[cfg(feature = "panic-on-alloc")]
107macro_rules! __mut_bump_vec_rev_panic_on_alloc {
108 [in $bump:expr; $($values:expr),* $(,)?] => {
109 $crate::MutBumpVecRev::from_owned_slice_in([$($values),*], $bump)
110 };
111 [in $bump:expr; $value:expr; $count:expr] => {
112 $crate::MutBumpVecRev::from_elem_in($value, $count, $bump)
113 };
114}
115
116#[doc(hidden)]
117#[macro_export]
118#[cfg(not(feature = "panic-on-alloc"))]
119macro_rules! __mut_bump_vec_rev_panic_on_alloc {
120 [in $bump:expr; $($values:expr),* $(,)?] => {
121 compile_error!(
122 concat!("the potentially panicking api of `mut_bump_vec_rev!` is not available\n\
123 help: enable `bump-scope`'s \"panic-on-alloc\" feature or use `try`:\n\
124 `mut_bump_vec_rev![try in ",
125 stringify!($bump),
126 "; ",
127 stringify!($($values),*),
128 "]`"
129 ))
130 };
131 [in $bump:expr; $value:expr; $count:expr] => {
132 compile_error!(
133 concat!("the potentially panicking api of `mut_bump_vec_rev!` is not available\n\
134 help: enable `bump-scope`'s \"panic-on-alloc\" feature or use `try`:\n\
135 `mut_bump_vec_rev![try in ", // TODO: add 4 spaces
136 stringify!($bump),
137 "; ",
138 stringify!($value),
139 "; ",
140 stringify!($count),
141 "]`"
142 ))
143 };
144}
145
146/// A type like [`MutBumpVec`](crate::MutBumpVec) but new elements are pushed to the front.
147///
148/// The point of this vector is to have a more performant <code>[into](Self::into_slice)([_boxed](Self::into_boxed_slice))[_slice](Self::into_slice)</code> for a downwards bumping allocator.
149///
150/// # Examples
151///
152/// This type can be used to allocate a slice, when `alloc_*` methods are too limiting:
153/// ```
154/// # use bump_scope::{Bump, mut_bump_vec_rev};
155/// # let mut bump: Bump = Bump::new();
156/// let mut vec = mut_bump_vec_rev![in &mut bump];
157///
158/// vec.push(1);
159/// vec.push(2);
160/// vec.push(3);
161///
162/// let slice: &[i32] = vec.into_slice();
163///
164/// assert_eq!(slice, [3, 2, 1]);
165/// ```
166///
167/// When extending a `MutBumpVecRev` by a slice, the elements have the same order as in the source slice.
168///
169/// ```
170/// # use bump_scope::{Bump, mut_bump_vec_rev};
171/// # let mut bump: Bump = Bump::new();
172/// let mut vec = mut_bump_vec_rev![in &mut bump; 4, 5, 6];
173///
174/// vec.extend_from_slice_copy(&[1, 2, 3]);
175///
176/// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
177/// ```
178//
179// MutBumpVecRev never actually moves a bump pointer.
180// It may force allocation of a new chunk, but it does not move the pointer within.
181// So we don't need to move the bump pointer when dropping.
182//
183// If we want to reset the bump pointer to a previous chunk, we use a bump scope.
184// We could do it here, by resetting to the last non-empty chunk but that would require a loop.
185// Chunk allocations are supposed to be very rare, so this wouldn't be worth it.
186pub struct MutBumpVecRev<T, A> {
187 /// This points at the end of the slice (`ptr` + `len`).
188 /// When `T` is a ZST this is always `NonNull::<T>::dangling()`.
189 pub(crate) end: NonNull<T>,
190 pub(crate) len: usize,
191
192 /// When `T` is a ZST this is always `usize::MAX`.
193 cap: usize,
194
195 allocator: A,
196
197 /// Marks ownership over T. (<https://doc.rust-lang.org/nomicon/phantom-data.html#generic-parameters-and-drop-checking>)
198 marker: PhantomData<T>,
199}
200
201impl<T: UnwindSafe, A: UnwindSafe> UnwindSafe for MutBumpVecRev<T, A> {}
202impl<T: RefUnwindSafe, A: RefUnwindSafe> RefUnwindSafe for MutBumpVecRev<T, A> {}
203
204impl<T, A> MutBumpVecRev<T, A> {
205 /// Constructs a new empty `MutBumpVecRev<T>`.
206 ///
207 /// The vector will not allocate until elements are pushed onto it.
208 ///
209 /// # Examples
210 /// ```
211 /// # use bump_scope::{Bump, MutBumpVecRev};
212 /// # let mut bump: Bump = Bump::new();
213 /// let vec = MutBumpVecRev::<i32, _>::new_in(&mut bump);
214 /// assert_eq!(vec.len(), 0);
215 /// assert_eq!(vec.capacity(), 0);
216 /// ```
217 #[inline]
218 pub fn new_in(allocator: A) -> Self {
219 Self {
220 end: NonNull::dangling(),
221 len: 0,
222 cap: if T::IS_ZST { usize::MAX } else { 0 },
223 allocator,
224 marker: PhantomData,
225 }
226 }
227
228 /// Returns the total number of elements the vector can hold without
229 /// reallocating.
230 ///
231 /// # Examples
232 ///
233 /// ```
234 /// # use bump_scope::{Bump, MutBumpVecRev};
235 /// # let mut bump: Bump = Bump::new();
236 /// let vec = MutBumpVecRev::<i32, _>::with_capacity_in(2048, &mut bump);
237 /// assert!(vec.capacity() >= 2048);
238 /// ```
239 #[must_use]
240 #[inline(always)]
241 pub const fn capacity(&self) -> usize {
242 self.cap
243 }
244
245 /// Returns the number of elements in the vector, also referred to
246 /// as its 'length'.
247 ///
248 /// # Examples
249 /// ```
250 /// # use bump_scope::{Bump, mut_bump_vec_rev};
251 /// # let mut bump: Bump = Bump::new();
252 /// let a = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
253 /// assert_eq!(a.len(), 3);
254 /// ```
255 #[must_use]
256 #[inline(always)]
257 pub const fn len(&self) -> usize {
258 self.len
259 }
260
261 /// Returns `true` if the vector contains no elements.
262 ///
263 /// # Examples
264 /// ```
265 /// # use bump_scope::{Bump, MutBumpVecRev};
266 /// # let mut bump: Bump = Bump::new();
267 /// let mut v = MutBumpVecRev::new_in(&mut bump);
268 /// assert!(v.is_empty());
269 ///
270 /// v.push(1);
271 /// assert!(!v.is_empty());
272 /// ```
273 #[must_use]
274 #[inline(always)]
275 pub const fn is_empty(&self) -> bool {
276 self.len == 0
277 }
278
279 /// Appends an element to the back of the collection.
280 ///
281 /// # Safety
282 /// Vector must not be full.
283 #[inline(always)]
284 pub unsafe fn push_unchecked(&mut self, value: T) {
285 unsafe { self.push_with_unchecked(|| value) };
286 }
287
288 /// Appends an element to the back of the collection.
289 ///
290 /// # Safety
291 /// Vector must not be full.
292 #[inline(always)]
293 pub unsafe fn push_with_unchecked(&mut self, f: impl FnOnce() -> T) {
294 debug_assert!(self.len < self.cap);
295
296 unsafe {
297 let ptr = self.end.sub(self.len + 1);
298 non_null::write_with(ptr, f);
299 }
300
301 // We set the len here so when `f` panics, `self.len` doesn't change.
302 self.len += 1;
303 }
304
305 /// Removes the first element from a vector and returns it, or [`None`] if it
306 /// is empty.
307 ///
308 /// # Examples
309 /// ```
310 /// # use bump_scope::{Bump, mut_bump_vec_rev};
311 /// # let mut bump: Bump = Bump::new();
312 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
313 /// assert_eq!(vec.pop(), Some(1));
314 /// assert_eq!(vec, [2, 3]);
315 /// ```
316 ///
317 /// # Time complexity
318 /// Takes *O*(1) time.
319 #[inline(always)]
320 pub fn pop(&mut self) -> Option<T> {
321 if self.len == 0 {
322 None
323 } else {
324 unsafe {
325 let ptr = self.as_ptr();
326 self.len -= 1;
327 Some(ptr.read())
328 }
329 }
330 }
331
332 /// Removes and returns the last element from a vector if the predicate
333 /// returns `true`, or [`None`] if the predicate returns false or the vector
334 /// is empty (the predicate will not be called in that case).
335 ///
336 /// # Examples
337 ///
338 /// ```
339 /// # use bump_scope::{Bump, mut_bump_vec_rev};
340 /// # let mut bump: Bump = Bump::new();
341 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3, 4];
342 /// let pred = |x: &mut i32| *x % 2 != 0;
343 ///
344 /// assert_eq!(vec.pop_if(pred), Some(1));
345 /// assert_eq!(vec, [2, 3, 4]);
346 /// assert_eq!(vec.pop_if(pred), None);
347 /// ```
348 pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option<T> {
349 let last = self.first_mut()?;
350 if predicate(last) { self.pop() } else { None }
351 }
352
353 /// Clears the vector, removing all values.
354 ///
355 /// Note that this method has no effect on the allocated capacity
356 /// of the vector.
357 ///
358 /// # Examples
359 ///
360 /// ```
361 /// # use bump_scope::{Bump, mut_bump_vec_rev};
362 /// # let mut bump: Bump = Bump::new();
363 /// let mut v = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
364 ///
365 /// v.clear();
366 ///
367 /// assert!(v.is_empty());
368 /// ```
369 #[inline(always)]
370 pub fn clear(&mut self) {
371 let elems: *mut [T] = self.as_mut_slice();
372
373 // SAFETY:
374 // - `elems` comes directly from `as_mut_slice` and is therefore valid.
375 // - Setting `self.len` before calling `drop_in_place` means that,
376 // if an element's `Drop` impl panics, the vector's `Drop` impl will
377 // do nothing (leaking the rest of the elements) instead of dropping
378 // some twice.
379 unsafe {
380 self.len = 0;
381 ptr::drop_in_place(elems);
382 }
383 }
384
385 /// Extracts a slice containing the entire vector.
386 ///
387 /// Equivalent to `&s[..]`.
388 #[must_use]
389 #[inline(always)]
390 pub fn as_slice(&self) -> &[T] {
391 unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
392 }
393
394 /// Extracts a mutable slice of the entire vector.
395 ///
396 /// Equivalent to `&mut s[..]`.
397 #[must_use]
398 #[inline(always)]
399 pub fn as_mut_slice(&mut self) -> &mut [T] {
400 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
401 }
402
403 /// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
404 /// valid for zero sized reads if the vector didn't allocate.
405 ///
406 /// The caller must ensure that the vector outlives the pointer this
407 /// function returns, or else it will end up pointing to garbage.
408 /// Modifying the vector may cause its buffer to be reallocated,
409 /// which would also make any pointers to it invalid.
410 ///
411 /// The caller must also ensure that the memory the pointer (non-transitively) points to
412 /// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
413 /// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
414 ///
415 /// # Examples
416 ///
417 /// ```
418 /// # use bump_scope::{Bump, mut_bump_vec_rev};
419 /// # let mut bump: Bump = Bump::new();
420 /// #
421 /// let x = mut_bump_vec_rev![in &mut bump; 1, 2, 4];
422 /// let x_ptr = x.as_ptr();
423 ///
424 /// unsafe {
425 /// for i in 0..x.len() {
426 /// assert_eq!(*x_ptr.add(i), 1 << i);
427 /// }
428 /// }
429 /// ```
430 ///
431 /// [`as_mut_ptr`]: Self::as_mut_ptr
432 #[inline]
433 #[must_use]
434 pub fn as_ptr(&self) -> *const T {
435 // We shadow the slice method of the same name to avoid going through
436 // `deref`, which creates an intermediate reference.
437 self.as_non_null().as_ptr()
438 }
439
440 /// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
441 /// raw pointer valid for zero sized reads if the vector didn't allocate.
442 ///
443 /// The caller must ensure that the vector outlives the pointer this
444 /// function returns, or else it will end up pointing to garbage.
445 /// Modifying the vector may cause its buffer to be reallocated,
446 /// which would also make any pointers to it invalid.
447 ///
448 /// # Examples
449 ///
450 /// ```
451 /// # use bump_scope::{Bump, MutBumpVecRev};
452 /// # let mut bump: Bump = Bump::new();
453 /// // Allocate vector big enough for 4 elements.
454 /// let size = 4;
455 /// let mut x = MutBumpVecRev::<i32, _>::with_capacity_in(size, &mut bump);
456 /// let x_ptr = unsafe { x.as_mut_ptr().sub(size) };
457 ///
458 /// // Initialize elements via raw pointer writes, then set length.
459 /// unsafe {
460 /// for i in 0..size {
461 /// *x_ptr.add(i) = i as i32;
462 /// }
463 /// x.set_len(size);
464 /// }
465 ///
466 /// assert_eq!(&*x, &[0, 1, 2, 3]);
467 /// ```
468 #[inline]
469 pub fn as_mut_ptr(&mut self) -> *mut T {
470 // We shadow the slice method of the same name to avoid going through
471 // `deref_mut`, which creates an intermediate reference.
472 self.as_non_null().as_ptr()
473 }
474
475 /// Returns a `NonNull` pointer to the vector's buffer, or a dangling
476 /// `NonNull` pointer valid for zero sized reads if the vector didn't allocate.
477 ///
478 /// The caller must ensure that the vector outlives the pointer this
479 /// function returns, or else it will end up dangling.
480 /// Modifying the vector may cause its buffer to be reallocated,
481 /// which would also make any pointers to it invalid.
482 ///
483 /// This method guarantees that for the purpose of the aliasing model, this method
484 /// does not materialize a reference to the underlying slice, and thus the returned pointer
485 /// will remain valid when mixed with other calls to [`as_ptr`], [`as_mut_ptr`],
486 /// and [`as_non_null`].
487 /// Note that calling other methods that materialize references to the slice,
488 /// or references to specific elements you are planning on accessing through this pointer,
489 /// may still invalidate this pointer.
490 /// See the second example below for how this guarantee can be used.
491 ///
492 /// # Examples
493 ///
494 /// ```
495 /// # use bump_scope::{Bump, MutBumpVecRev};
496 /// # let mut bump: Bump = Bump::new();
497 /// // Allocate vector big enough for 4 elements.
498 /// let size = 4;
499 /// let mut x: MutBumpVecRev<i32, _> = MutBumpVecRev::with_capacity_in(size, &mut bump);
500 /// let x_ptr = x.as_non_null();
501 ///
502 /// // Initialize elements via raw pointer writes, then set length.
503 /// unsafe {
504 /// for i in 0..size {
505 /// x_ptr.sub(i + 1).write(i as i32);
506 /// }
507 /// x.set_len(size);
508 /// }
509 /// assert_eq!(&*x, &[3, 2, 1, 0]);
510 /// ```
511 ///
512 /// Due to the aliasing guarantee, the following code is legal:
513 ///
514 /// ```
515 /// # use bump_scope::{Bump, mut_bump_vec_rev};
516 /// # let mut bump: Bump = Bump::new();
517 /// unsafe {
518 /// let v = mut_bump_vec_rev![in &mut bump; 0];
519 /// let ptr1 = v.as_non_null();
520 /// ptr1.write(1);
521 /// let ptr2 = v.as_non_null();
522 /// ptr2.write(2);
523 /// // Notably, the write to `ptr2` did *not* invalidate `ptr1`:
524 /// ptr1.write(3);
525 /// }
526 /// ```
527 ///
528 /// [`as_mut_ptr`]: Self::as_mut_ptr
529 /// [`as_ptr`]: Self::as_ptr
530 /// [`as_non_null`]: Self::as_non_null
531 #[must_use]
532 #[inline(always)]
533 pub const fn as_non_null(&self) -> NonNull<T> {
534 // SAFETY: The start pointer is never null.
535 unsafe { self.end.sub(self.len) }
536 }
537
538 /// Returns a `NonNull` pointer to the vector's buffer, or a dangling
539 /// `NonNull` pointer valid for zero sized reads if the vector didn't allocate.
540 #[doc(hidden)]
541 #[deprecated = "renamed to `as_non_null`"]
542 #[must_use]
543 #[inline(always)]
544 pub fn as_non_null_ptr(&self) -> NonNull<T> {
545 unsafe { self.end.sub(self.len) }
546 }
547
548 /// Returns a `NonNull` pointer to the vector's buffer, or a dangling
549 /// `NonNull` pointer valid for zero sized reads if the vector didn't allocate.
550 #[doc(hidden)]
551 #[deprecated = "too niche; compute this yourself if needed"]
552 #[must_use]
553 #[inline(always)]
554 pub fn as_non_null_slice(&self) -> NonNull<[T]> {
555 NonNull::slice_from_raw_parts(self.as_non_null(), self.len)
556 }
557
558 /// Shortens the vector, keeping the first `len` elements and dropping
559 /// the rest.
560 ///
561 /// If `len` is greater than the vector's current length, this has no
562 /// effect.
563 ///
564 /// Note that this method has no effect on the allocated capacity
565 /// of the vector.
566 ///
567 /// # Examples
568 ///
569 /// Truncating a five element vector to two elements:
570 ///
571 /// ```
572 /// # use bump_scope::{Bump, mut_bump_vec_rev};
573 /// # let mut bump: Bump = Bump::new();
574 /// #
575 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3, 4, 5];
576 /// vec.truncate(2);
577 /// assert_eq!(vec, [4, 5]);
578 /// ```
579 ///
580 /// No truncation occurs when `len` is greater than the vector's current
581 /// length:
582 ///
583 /// ```
584 /// # use bump_scope::{Bump, mut_bump_vec_rev};
585 /// # let mut bump: Bump = Bump::new();
586 /// #
587 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
588 /// vec.truncate(8);
589 /// assert_eq!(vec, [1, 2, 3]);
590 /// ```
591 ///
592 /// Truncating when `len == 0` is equivalent to calling the [`clear`]
593 /// method.
594 ///
595 /// ```
596 /// # use bump_scope::{Bump, mut_bump_vec_rev};
597 /// # let mut bump: Bump = Bump::new();
598 /// #
599 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
600 /// vec.truncate(0);
601 /// assert_eq!(vec, []);
602 /// ```
603 ///
604 /// [`clear`]: Self::clear
605 pub fn truncate(&mut self, len: usize) {
606 // This is safe because:
607 //
608 // * the slice passed to `drop_in_place` is valid; the `len > self.len`
609 // case avoids creating an invalid slice, and
610 // * the `len` of the vector is shrunk before calling `drop_in_place`,
611 // such that no value will be dropped twice in case `drop_in_place`
612 // were to panic once (if it panics twice, the program aborts).
613 unsafe {
614 // Unlike std this is `>=`. Std uses `>` because when a call is inlined with `len` of `0` that optimizes better.
615 // But this was likely only motivated because `clear` used to be implemented as `truncate(0)`.
616 // See <https://github.com/rust-lang/rust/issues/76089#issuecomment-1889416842>.
617 if len >= self.len {
618 return;
619 }
620
621 let remaining_len = self.len - len;
622
623 let ptr = self.as_mut_ptr();
624 let slice = ptr::slice_from_raw_parts_mut(ptr, remaining_len);
625
626 self.len = len;
627 slice.drop_in_place();
628 }
629 }
630
631 /// Forces the length of the vector to `new_len`.
632 ///
633 /// This is a low-level operation that maintains none of the normal
634 /// invariants of the type. Normally changing the length of a vector
635 /// is done using one of the safe operations instead, such as
636 /// [`resize`], [`truncate`], [`extend`], or [`clear`].
637 ///
638 /// [`truncate`]: Self::truncate
639 /// [`resize`]: Self::resize
640 /// [`extend`]: Self::extend
641 /// [`clear`]: Self::clear
642 ///
643 /// # Safety
644 ///
645 /// - `new_len` must be less than or equal to [`capacity`].
646 /// - The elements at `old_len..new_len` must be initialized.
647 ///
648 /// [`capacity`]: Self::capacity
649 #[inline]
650 pub unsafe fn set_len(&mut self, new_len: usize) {
651 debug_assert!(new_len <= self.cap);
652 self.len = new_len;
653 }
654
655 #[inline]
656 pub(crate) unsafe fn inc_len(&mut self, amount: usize) {
657 self.len += amount;
658 }
659
660 #[inline(always)]
661 fn into_raw_parts(self) -> (NonNull<T>, usize, usize, A) {
662 destructure!(let Self { end, len, cap, allocator } = self);
663 (end, len, cap, allocator)
664 }
665
666 #[inline(always)]
667 unsafe fn from_raw_parts(end: NonNull<T>, len: usize, cap: usize, allocator: A) -> Self {
668 Self {
669 end,
670 len,
671 cap,
672 allocator,
673 marker: PhantomData,
674 }
675 }
676}
677
678impl<T, A: MutBumpAllocatorExt> MutBumpVecRev<T, A> {
679 /// Constructs a new empty vector with at least the specified capacity
680 /// in the provided bump allocator.
681 ///
682 /// The vector will be able to hold `capacity` elements without
683 /// reallocating. If `capacity` is 0, the vector will not allocate.
684 ///
685 /// It is important to note that although the returned vector has the
686 /// minimum *capacity* specified, the vector will have a zero *length*. For
687 /// an explanation of the difference between length and capacity, see
688 /// *[Capacity and reallocation]*.
689 ///
690 /// When `T` is a zero-sized type, there will be no allocation
691 /// and the capacity will always be `usize::MAX`.
692 ///
693 /// [Capacity and reallocation]: alloc_crate::vec::Vec#capacity-and-reallocation
694 ///
695 /// # Panics
696 /// Panics if the allocation fails.
697 ///
698 /// # Examples
699 /// ```
700 /// # use bump_scope::{Bump, MutBumpVecRev};
701 /// # let mut bump: Bump = Bump::new();
702 /// let mut vec = MutBumpVecRev::<i32, _>::with_capacity_in(10, &mut bump);
703 ///
704 /// // The vector contains no items, even though it has capacity for more
705 /// assert_eq!(vec.len(), 0);
706 /// assert!(vec.capacity() >= 10);
707 ///
708 /// // These are all done without reallocating...
709 /// for i in 0..10 {
710 /// vec.push(i);
711 /// }
712 /// assert_eq!(vec.len(), 10);
713 /// assert!(vec.capacity() >= 10);
714 ///
715 /// // ...but this may make the vector reallocate
716 /// vec.push(11);
717 /// assert_eq!(vec.len(), 11);
718 /// assert!(vec.capacity() >= 11);
719 ///
720 /// drop(vec);
721 ///
722 /// // A vector of a zero-sized type will always over-allocate, since no
723 /// // allocation is necessary
724 /// let vec_units = MutBumpVecRev::<(), _>::with_capacity_in(10, &mut bump);
725 /// assert_eq!(vec_units.capacity(), usize::MAX);
726 /// ```
727 #[must_use]
728 #[inline(always)]
729 #[cfg(feature = "panic-on-alloc")]
730 pub fn with_capacity_in(capacity: usize, allocator: A) -> Self {
731 panic_on_error(Self::generic_with_capacity_in(capacity, allocator))
732 }
733
734 /// Constructs a new empty vector with at least the specified capacity
735 /// in the provided bump allocator.
736 ///
737 /// The vector will be able to hold `capacity` elements without
738 /// reallocating. If `capacity` is 0, the vector will not allocate.
739 ///
740 /// It is important to note that although the returned vector has the
741 /// minimum *capacity* specified, the vector will have a zero *length*. For
742 /// an explanation of the difference between length and capacity, see
743 /// *[Capacity and reallocation]*.
744 ///
745 /// When `T` is a zero-sized type, there will be no allocation
746 /// and the capacity will always be `usize::MAX`.
747 ///
748 /// [Capacity and reallocation]: alloc_crate::vec::Vec#capacity-and-reallocation
749 ///
750 /// # Errors
751 /// Errors if the allocation fails.
752 ///
753 /// # Examples
754 /// ```
755 /// # use bump_scope::{Bump, MutBumpVecRev};
756 /// # let mut bump: Bump = Bump::try_new()?;
757 /// let mut vec = MutBumpVecRev::<i32, _>::try_with_capacity_in(10, &mut bump)?;
758 ///
759 /// // The vector contains no items, even though it has capacity for more
760 /// assert_eq!(vec.len(), 0);
761 /// assert!(vec.capacity() >= 10);
762 ///
763 /// // These are all done without reallocating...
764 /// for i in 0..10 {
765 /// vec.push(i);
766 /// }
767 /// assert_eq!(vec.len(), 10);
768 /// assert!(vec.capacity() >= 10);
769 ///
770 /// // ...but this may make the vector reallocate
771 /// vec.push(11);
772 /// assert_eq!(vec.len(), 11);
773 /// assert!(vec.capacity() >= 11);
774 ///
775 /// drop(vec);
776 ///
777 /// // A vector of a zero-sized type will always over-allocate, since no
778 /// // allocation is necessary
779 /// let vec_units = MutBumpVecRev::<(), _>::try_with_capacity_in(10, &mut bump)?;
780 /// assert_eq!(vec_units.capacity(), usize::MAX);
781 /// # Ok::<(), bump_scope::alloc::AllocError>(())
782 /// ```
783 #[inline(always)]
784 pub fn try_with_capacity_in(capacity: usize, allocator: A) -> Result<Self, AllocError> {
785 Self::generic_with_capacity_in(capacity, allocator)
786 }
787
788 #[inline]
789 pub(crate) fn generic_with_capacity_in<E: ErrorBehavior>(capacity: usize, allocator: A) -> Result<Self, E> {
790 let mut allocator = allocator;
791
792 if T::IS_ZST {
793 return Ok(Self {
794 end: NonNull::dangling(),
795 len: 0,
796 cap: usize::MAX,
797 allocator,
798 marker: PhantomData,
799 });
800 }
801
802 if capacity == 0 {
803 return Ok(Self {
804 end: NonNull::dangling(),
805 len: 0,
806 cap: 0,
807 allocator,
808 marker: PhantomData,
809 });
810 }
811
812 let slice = unsafe { E::prepare_slice_allocation::<T>(&mut allocator, capacity)? };
813 let cap = slice.len();
814 let end = unsafe { non_null::as_non_null_ptr(slice).add(cap) };
815
816 Ok(Self {
817 end,
818 len: 0,
819 cap,
820 allocator,
821 marker: PhantomData,
822 })
823 }
824
825 /// Constructs a new `MutBumpVecRev<T>` and pushes `value` `count` times.
826 ///
827 /// # Panics
828 /// Panics if the allocation fails.
829 ///
830 /// # Examples
831 /// ```
832 /// # use bump_scope::{Bump, MutBumpVecRev};
833 /// # let mut bump: Bump = Bump::new();
834 /// let vec = MutBumpVecRev::from_elem_in("ho", 3, &mut bump);
835 /// assert_eq!(vec, ["ho", "ho", "ho"]);
836 /// ```
837 #[must_use]
838 #[inline(always)]
839 #[cfg(feature = "panic-on-alloc")]
840 pub fn from_elem_in(value: T, count: usize, allocator: A) -> Self
841 where
842 T: Clone,
843 {
844 panic_on_error(Self::generic_from_elem_in(value, count, allocator))
845 }
846
847 /// Constructs a new `MutBumpVecRev<T>` and pushes `value` `count` times.
848 ///
849 /// # Errors
850 /// Errors if the allocation fails.
851 ///
852 /// # Examples
853 /// ```
854 /// # use bump_scope::{Bump, MutBumpVecRev};
855 /// # let mut bump: Bump = Bump::try_new()?;
856 /// let vec = MutBumpVecRev::try_from_elem_in("ho", 3, &mut bump)?;
857 /// assert_eq!(vec, ["ho", "ho", "ho"]);
858 /// # Ok::<(), bump_scope::alloc::AllocError>(())
859 /// ```
860 #[inline(always)]
861 pub fn try_from_elem_in(value: T, count: usize, allocator: A) -> Result<Self, AllocError>
862 where
863 T: Clone,
864 {
865 Self::generic_from_elem_in(value, count, allocator)
866 }
867
868 #[inline]
869 pub(crate) fn generic_from_elem_in<E: ErrorBehavior>(value: T, count: usize, allocator: A) -> Result<Self, E>
870 where
871 T: Clone,
872 {
873 let mut vec = Self::generic_with_capacity_in(count, allocator)?;
874
875 unsafe {
876 if count != 0 {
877 for _ in 0..(count - 1) {
878 vec.push_with_unchecked(|| value.clone());
879 }
880
881 vec.push_with_unchecked(|| value);
882 }
883 }
884
885 Ok(vec)
886 }
887
888 /// Constructs a new `MutBumpVecRev<T>` from an [`OwnedSlice`].
889 ///
890 /// # Panics
891 /// Panics if the allocation fails.
892 ///
893 /// # Examples
894 /// ```
895 /// # use bump_scope::{Bump, MutBumpVecRev};
896 /// # let bump: Bump = Bump::new();
897 /// # let mut bump_a: Bump = Bump::new();
898 /// # let mut bump_b: Bump = Bump::new();
899 /// # let mut bump_c: Bump = Bump::new();
900 /// # let mut bump_d: Bump = Bump::new();
901 /// // by value
902 /// let a = MutBumpVecRev::from_owned_slice_in([1, 2], &mut bump_a);
903 /// let b = MutBumpVecRev::from_owned_slice_in(vec![3, 4], &mut bump_b);
904 /// let c = MutBumpVecRev::from_owned_slice_in(bump.alloc_iter(5..=6), &mut bump_c);
905 ///
906 /// // by mutable reference
907 /// let mut other = vec![7, 8];
908 /// let d = MutBumpVecRev::from_owned_slice_in(&mut other, &mut bump_d);
909 /// assert!(other.is_empty());
910 ///
911 /// assert_eq!(a, [1, 2]);
912 /// assert_eq!(b, [3, 4]);
913 /// assert_eq!(c, [5, 6]);
914 /// assert_eq!(d, [7, 8]);
915 /// ```
916 #[must_use]
917 #[inline(always)]
918 #[cfg(feature = "panic-on-alloc")]
919 pub fn from_owned_slice_in(owned_slice: impl OwnedSlice<Item = T>, allocator: A) -> Self {
920 panic_on_error(Self::generic_from_owned_slice_in(owned_slice, allocator))
921 }
922
923 /// Constructs a new `MutBumpVecRev<T>` from an [`OwnedSlice`].
924 ///
925 /// # Errors
926 /// Errors if the allocation fails.
927 ///
928 /// # Examples
929 /// ```
930 /// # use bump_scope::{Bump, MutBumpVecRev};
931 /// # let bump: Bump = Bump::new();
932 /// # let mut bump_a: Bump = Bump::new();
933 /// # let mut bump_b: Bump = Bump::new();
934 /// # let mut bump_c: Bump = Bump::new();
935 /// # let mut bump_d: Bump = Bump::new();
936 /// // by value
937 /// let a = MutBumpVecRev::try_from_owned_slice_in([1, 2], &mut bump_a)?;
938 /// let b = MutBumpVecRev::try_from_owned_slice_in(vec![3, 4], &mut bump_b)?;
939 /// let c = MutBumpVecRev::try_from_owned_slice_in(bump.alloc_iter(5..=6), &mut bump_c)?;
940 ///
941 /// // by mutable reference
942 /// let mut other = vec![7, 8];
943 /// let d = MutBumpVecRev::try_from_owned_slice_in(&mut other, &mut bump_d)?;
944 /// assert!(other.is_empty());
945 ///
946 /// assert_eq!(a, [1, 2]);
947 /// assert_eq!(b, [3, 4]);
948 /// assert_eq!(c, [5, 6]);
949 /// assert_eq!(d, [7, 8]);
950 /// # Ok::<(), bump_scope::alloc::AllocError>(())
951 /// ```
952 #[inline(always)]
953 pub fn try_from_owned_slice_in(owned_slice: impl OwnedSlice<Item = T>, allocator: A) -> Result<Self, AllocError> {
954 Self::generic_from_owned_slice_in(owned_slice, allocator)
955 }
956
957 #[inline]
958 pub(crate) fn generic_from_owned_slice_in<E: ErrorBehavior>(
959 owned_slice: impl OwnedSlice<Item = T>,
960 allocator: A,
961 ) -> Result<Self, E> {
962 let owned_slice = owned_slice.into_take_owned_slice();
963 let mut this = Self::generic_with_capacity_in(owned_slice.owned_slice_ref().len(), allocator)?;
964 this.generic_append(owned_slice)?;
965 Ok(this)
966 }
967
968 /// Constructs a new `MutBumpVecRev<T>` from a `[T; N]`.
969 ///
970 /// # Panics
971 /// Panics if the allocation fails.
972 #[doc(hidden)]
973 #[deprecated = "use `from_owned_slice_in` instead"]
974 #[must_use]
975 #[inline(always)]
976 #[cfg(feature = "panic-on-alloc")]
977 pub fn from_array_in<const N: usize>(array: [T; N], allocator: A) -> Self {
978 panic_on_error(Self::generic_from_array_in(array, allocator))
979 }
980
981 /// Constructs a new `MutBumpVecRev<T>` from a `[T; N]`.
982 ///
983 /// # Errors
984 /// Errors if the allocation fails.
985 #[doc(hidden)]
986 #[deprecated = "use `try_from_owned_slice_in` instead"]
987 #[inline(always)]
988 pub fn try_from_array_in<const N: usize>(array: [T; N], allocator: A) -> Result<Self, AllocError> {
989 Self::generic_from_array_in(array, allocator)
990 }
991
992 #[inline]
993 pub(crate) fn generic_from_array_in<E: ErrorBehavior, const N: usize>(array: [T; N], allocator: A) -> Result<Self, E> {
994 let array = ManuallyDrop::new(array);
995 let mut allocator = allocator;
996
997 if T::IS_ZST {
998 return Ok(Self {
999 end: NonNull::dangling(),
1000 len: N,
1001 cap: usize::MAX,
1002 allocator,
1003 marker: PhantomData,
1004 });
1005 }
1006
1007 if N == 0 {
1008 return Ok(Self {
1009 end: NonNull::dangling(),
1010 len: 0,
1011 cap: 0,
1012 allocator,
1013 marker: PhantomData,
1014 });
1015 }
1016
1017 let slice = unsafe { E::prepare_slice_allocation::<T>(&mut allocator, N)? };
1018 let cap = slice.len();
1019 let end = unsafe { non_null::as_non_null_ptr(slice).add(cap) };
1020
1021 let src = array.as_ptr();
1022
1023 unsafe {
1024 let dst = end.as_ptr().sub(N);
1025 ptr::copy_nonoverlapping(src, dst, N);
1026 };
1027
1028 Ok(Self {
1029 end,
1030 len: N,
1031 cap,
1032 allocator,
1033 marker: PhantomData,
1034 })
1035 }
1036
1037 /// Create a new [`MutBumpVecRev`] whose elements are taken from an iterator and allocated in the given `bump`.
1038 ///
1039 /// This is behaviorally identical to [`FromIterator::from_iter`].
1040 ///
1041 /// If you have an `impl ExactSizeIterator` then you can use [`from_iter_exact_in`](Self::from_iter_exact_in) instead for better performance.
1042 ///
1043 /// # Panics
1044 /// Panics if the allocation fails.
1045 ///
1046 /// # Examples
1047 /// ```
1048 /// # use bump_scope::{Bump, MutBumpVecRev};
1049 /// # let mut bump: Bump = Bump::new();
1050 /// let vec = MutBumpVecRev::from_iter_in([1, 2, 3], &mut bump);
1051 /// assert_eq!(vec, [3, 2, 1]);
1052 /// ```
1053 #[must_use]
1054 #[inline(always)]
1055 #[cfg(feature = "panic-on-alloc")]
1056 pub fn from_iter_in<I>(iter: I, allocator: A) -> Self
1057 where
1058 I: IntoIterator<Item = T>,
1059 {
1060 panic_on_error(Self::generic_from_iter_in(iter, allocator))
1061 }
1062
1063 /// Create a new [`MutBumpVecRev`] whose elements are taken from an iterator and allocated in the given `bump`.
1064 ///
1065 /// This is behaviorally identical to [`FromIterator::from_iter`].
1066 ///
1067 /// If you have an `impl ExactSizeIterator` then you can use [`try_from_iter_exact_in`](Self::try_from_iter_exact_in) instead for better performance.
1068 ///
1069 /// # Errors
1070 /// Errors if the allocation fails.
1071 ///
1072 /// # Examples
1073 /// ```
1074 /// # use bump_scope::{Bump, MutBumpVecRev};
1075 /// # let mut bump: Bump = Bump::try_new()?;
1076 /// let vec = MutBumpVecRev::try_from_iter_in([1, 2, 3], &mut bump)?;
1077 /// assert_eq!(vec, [3, 2, 1]);
1078 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1079 /// ```
1080 #[inline(always)]
1081 pub fn try_from_iter_in<I>(iter: I, allocator: A) -> Result<Self, AllocError>
1082 where
1083 I: IntoIterator<Item = T>,
1084 {
1085 Self::generic_from_iter_in(iter, allocator)
1086 }
1087
1088 #[inline]
1089 pub(crate) fn generic_from_iter_in<E: ErrorBehavior, I>(iter: I, allocator: A) -> Result<Self, E>
1090 where
1091 I: IntoIterator<Item = T>,
1092 {
1093 let iter = iter.into_iter();
1094 let capacity = iter.size_hint().0;
1095
1096 let mut vec = Self::generic_with_capacity_in(capacity, allocator)?;
1097
1098 for value in iter {
1099 vec.generic_push(value)?;
1100 }
1101
1102 Ok(vec)
1103 }
1104
1105 /// Create a new [`MutBumpVecRev`] whose elements are taken from an iterator and allocated in the given `bump`.
1106 ///
1107 /// This is just like [`from_iter_in`](Self::from_iter_in) but optimized for an [`ExactSizeIterator`].
1108 ///
1109 /// # Panics
1110 /// Panics if the allocation fails.
1111 ///
1112 /// # Examples
1113 /// ```
1114 /// # use bump_scope::{Bump, MutBumpVecRev};
1115 /// # let mut bump: Bump = Bump::new();
1116 /// let vec = MutBumpVecRev::from_iter_exact_in([1, 2, 3], &mut bump);
1117 /// assert_eq!(vec, [3, 2, 1]);
1118 /// ```
1119 #[must_use]
1120 #[inline(always)]
1121 #[cfg(feature = "panic-on-alloc")]
1122 pub fn from_iter_exact_in<I>(iter: I, allocator: A) -> Self
1123 where
1124 I: IntoIterator<Item = T>,
1125 I::IntoIter: ExactSizeIterator,
1126 {
1127 panic_on_error(Self::generic_from_iter_exact_in(iter, allocator))
1128 }
1129
1130 /// Create a new [`MutBumpVecRev`] whose elements are taken from an iterator and allocated in the given `bump`.
1131 ///
1132 /// This is just like [`from_iter_in`](Self::from_iter_in) but optimized for an [`ExactSizeIterator`].
1133 ///
1134 /// # Errors
1135 /// Errors if the allocation fails.
1136 ///
1137 /// # Examples
1138 /// ```
1139 /// # use bump_scope::{Bump, MutBumpVecRev};
1140 /// # let mut bump: Bump = Bump::try_new()?;
1141 /// let vec = MutBumpVecRev::try_from_iter_exact_in([1, 2, 3], &mut bump)?;
1142 /// assert_eq!(vec, [3, 2, 1]);
1143 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1144 /// ```
1145 #[inline(always)]
1146 pub fn try_from_iter_exact_in<I>(iter: I, allocator: A) -> Result<Self, AllocError>
1147 where
1148 I: IntoIterator<Item = T>,
1149 I::IntoIter: ExactSizeIterator,
1150 {
1151 Self::generic_from_iter_exact_in(iter, allocator)
1152 }
1153
1154 #[inline]
1155 pub(crate) fn generic_from_iter_exact_in<E: ErrorBehavior, I>(iter: I, allocator: A) -> Result<Self, E>
1156 where
1157 I: IntoIterator<Item = T>,
1158 I::IntoIter: ExactSizeIterator,
1159 {
1160 let mut iter = iter.into_iter();
1161 let len = iter.len();
1162
1163 let mut vec = Self::generic_with_capacity_in(len, allocator)?;
1164
1165 while vec.len() != vec.capacity() {
1166 match iter.next() {
1167 // SAFETY: we checked above that `len != capacity`, so there is space
1168 Some(value) => unsafe { vec.push_unchecked(value) },
1169 None => break,
1170 }
1171 }
1172
1173 Ok(vec)
1174 }
1175
1176 /// Appends an element to the front of a collection.
1177 ///
1178 /// # Panics
1179 /// Panics if the allocation fails.
1180 ///
1181 /// # Examples
1182 /// ```
1183 /// # use bump_scope::{mut_bump_vec_rev, Bump};
1184 /// # let mut bump: Bump = Bump::new();
1185 /// let mut vec = mut_bump_vec_rev![in &mut bump; 2, 1];
1186 /// vec.push(3);
1187 /// assert_eq!(vec, [3, 2, 1]);
1188 /// # let _ = vec;
1189 /// ```
1190 #[inline(always)]
1191 #[cfg(feature = "panic-on-alloc")]
1192 pub fn push(&mut self, value: T) {
1193 panic_on_error(self.generic_push(value));
1194 }
1195
1196 /// Appends an element to the front of a collection.
1197 ///
1198 /// # Errors
1199 /// Errors if the allocation fails.
1200 ///
1201 /// # Examples
1202 /// ```
1203 /// # use bump_scope::{mut_bump_vec_rev, Bump};
1204 /// # let mut bump: Bump = Bump::try_new()?;
1205 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 2, 1]?;
1206 /// vec.try_push(3)?;
1207 /// assert_eq!(vec, [3, 2, 1]);
1208 /// # let _ = vec;
1209 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1210 /// ```
1211 #[inline(always)]
1212 pub fn try_push(&mut self, value: T) -> Result<(), AllocError> {
1213 self.generic_push(value)
1214 }
1215
1216 #[inline]
1217 pub(crate) fn generic_push<E: ErrorBehavior>(&mut self, value: T) -> Result<(), E> {
1218 self.generic_push_with(|| value)
1219 }
1220
1221 /// Reserves space for one more element, then calls `f`
1222 /// to produce the value that is appended.
1223 ///
1224 /// In some cases this could be more performant than `push(f())` because it
1225 /// permits the compiler to directly place `T` in the vector instead of
1226 /// constructing it on the stack and copying it over.
1227 ///
1228 /// # Panics
1229 /// Panics if the allocation fails.
1230 ///
1231 /// # Examples
1232 /// ```
1233 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1234 /// # let mut bump: Bump = Bump::new();
1235 /// let mut vec = mut_bump_vec_rev![in &mut bump; 2, 3];
1236 /// vec.push_with(|| 1);
1237 /// assert_eq!(vec, [1, 2, 3]);
1238 /// ```
1239 #[inline(always)]
1240 #[cfg(feature = "panic-on-alloc")]
1241 pub fn push_with(&mut self, f: impl FnOnce() -> T) {
1242 panic_on_error(self.generic_push_with(f));
1243 }
1244
1245 /// Reserves space for one more element, then calls `f`
1246 /// to produce the value that is appended.
1247 ///
1248 /// In some cases this could be more performant than `push(f())` because it
1249 /// permits the compiler to directly place `T` in the vector instead of
1250 /// constructing it on the stack and copying it over.
1251 ///
1252 /// # Errors
1253 /// Errors if the allocation fails.
1254 ///
1255 /// # Examples
1256 /// ```
1257 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1258 /// # let mut bump: Bump = Bump::new();
1259 /// let mut vec = mut_bump_vec_rev![in &mut bump; 2, 3];
1260 /// vec.try_push_with(|| 1)?;
1261 /// assert_eq!(vec, [1, 2, 3]);
1262 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1263 /// ```
1264 #[inline(always)]
1265 pub fn try_push_with(&mut self, f: impl FnOnce() -> T) -> Result<(), AllocError> {
1266 self.generic_push_with(f)
1267 }
1268
1269 #[inline]
1270 pub(crate) fn generic_push_with<E: ErrorBehavior>(&mut self, f: impl FnOnce() -> T) -> Result<(), E> {
1271 self.generic_reserve_one()?;
1272 unsafe {
1273 self.push_with_unchecked(f);
1274 }
1275 Ok(())
1276 }
1277
1278 /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1279 ///
1280 /// # Panics
1281 /// Panics if the allocation fails.
1282 ///
1283 /// Panics if `index > len`.
1284 ///
1285 /// # Examples
1286 /// ```
1287 /// # use bump_scope::{mut_bump_vec_rev, Bump};
1288 /// # let mut bump: Bump = Bump::new();
1289 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
1290 /// vec.insert(1, 4);
1291 /// assert_eq!(vec, [1, 4, 2, 3]);
1292 /// vec.insert(4, 5);
1293 /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1294 /// ```
1295 #[inline(always)]
1296 #[cfg(feature = "panic-on-alloc")]
1297 pub fn insert(&mut self, index: usize, element: T) {
1298 panic_on_error(self.generic_insert(index, element));
1299 }
1300
1301 /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
1302 ///
1303 /// # Panics
1304 /// Panics if `index > len`.
1305 ///
1306 /// # Errors
1307 /// Errors if the allocation fails.
1308 ///
1309 /// # Examples
1310 /// ```
1311 /// # use bump_scope::{mut_bump_vec_rev, Bump};
1312 /// # let mut bump: Bump = Bump::try_new()?;
1313 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3]?;
1314 /// vec.try_insert(1, 4)?;
1315 /// assert_eq!(vec, [1, 4, 2, 3]);
1316 /// vec.try_insert(4, 5)?;
1317 /// assert_eq!(vec, [1, 4, 2, 3, 5]);
1318 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1319 /// ```
1320 #[inline(always)]
1321 pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), AllocError> {
1322 self.generic_insert(index, element)
1323 }
1324
1325 #[inline]
1326 pub(crate) fn generic_insert<E: ErrorBehavior>(&mut self, index: usize, element: T) -> Result<(), E> {
1327 #[cold]
1328 #[track_caller]
1329 #[inline(never)]
1330 fn assert_failed(index: usize, len: usize) -> ! {
1331 panic!("insertion index (is {index}) should be <= len (is {len})");
1332 }
1333
1334 if index > self.len {
1335 assert_failed(index, self.len);
1336 }
1337
1338 self.generic_reserve_one()?;
1339
1340 unsafe {
1341 if index == 0 {
1342 self.len += 1;
1343 self.as_mut_ptr().write(element);
1344 } else {
1345 let start = self.as_mut_ptr();
1346 let start_sub = start.sub(1);
1347 ptr::copy(start, start_sub, index);
1348 self.len += 1;
1349 start_sub.add(index).write(element);
1350 }
1351 }
1352
1353 Ok(())
1354 }
1355
1356 /// Copies and appends all elements in a slice to the `MutBumpVecRev`.
1357 ///
1358 /// Iterates over the `slice`, copies each element, and then appends
1359 /// it to this `MutBumpVecRev`. The `slice` is traversed in-order.
1360 ///
1361 /// Note that this function is same as [`extend`] except that it is
1362 /// specialized to work with copyable slices instead.
1363 ///
1364 /// [`extend`]: Self::extend
1365 ///
1366 /// # Panics
1367 /// Panics if the allocation fails.
1368 ///
1369 /// # Examples
1370 /// ```
1371 /// # use bump_scope::{ Bump, mut_bump_vec_rev };
1372 /// # let mut bump: Bump = Bump::new();
1373 /// let mut vec = mut_bump_vec_rev![in &mut bump; 4];
1374 /// vec.extend_from_slice_copy(&[1, 2, 3]);
1375 /// assert_eq!(vec, [1, 2, 3, 4]);
1376 /// ```
1377 #[inline(always)]
1378 #[cfg(feature = "panic-on-alloc")]
1379 pub fn extend_from_slice_copy(&mut self, slice: &[T])
1380 where
1381 T: Copy,
1382 {
1383 panic_on_error(self.generic_extend_from_slice_copy(slice));
1384 }
1385
1386 /// Copies and appends all elements in a slice to the `MutBumpVecRev`.
1387 ///
1388 /// Iterates over the `slice`, copies each element, and then appends
1389 /// it to this `MutBumpVecRev`. The `slice` is traversed in-order.
1390 ///
1391 /// Note that this function is same as [`extend`] except that it is
1392 /// specialized to work with copyable slices instead.
1393 ///
1394 /// [`extend`]: Self::extend
1395 ///
1396 /// # Errors
1397 /// Errors if the allocation fails.
1398 ///
1399 /// # Examples
1400 /// ```
1401 /// # use bump_scope::{ Bump, mut_bump_vec_rev };
1402 /// # let mut bump: Bump = Bump::new();
1403 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 4]?;
1404 /// vec.try_extend_from_slice_copy(&[1, 2, 3])?;
1405 /// assert_eq!(vec, [1, 2, 3, 4]);
1406 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1407 /// ```
1408 #[inline(always)]
1409 pub fn try_extend_from_slice_copy(&mut self, slice: &[T]) -> Result<(), AllocError>
1410 where
1411 T: Copy,
1412 {
1413 self.generic_extend_from_slice_copy(slice)
1414 }
1415
1416 #[inline]
1417 pub(crate) fn generic_extend_from_slice_copy<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1418 where
1419 T: Copy,
1420 {
1421 unsafe { self.extend_by_copy_nonoverlapping(slice) }
1422 }
1423
1424 /// Clones and appends all elements in a slice to the `MutBumpVecRev`.
1425 ///
1426 /// Iterates over the `slice`, clones each element, and then appends
1427 /// it to this `MutBumpVecRev`. The `slice` is traversed in-order.
1428 ///
1429 /// Note that this function is same as [`extend`] except that it is
1430 /// specialized to work with slices instead.
1431 ///
1432 /// [`extend`]: Self::extend
1433 ///
1434 /// # Panics
1435 /// Panics if the allocation fails.
1436 ///
1437 /// # Examples
1438 /// ```
1439 /// # use std::string::String;
1440 /// # use bump_scope::{ Bump, mut_bump_vec_rev };
1441 /// # let mut bump: Bump = Bump::new();
1442 /// let mut vec = mut_bump_vec_rev![in &mut bump; String::from("c")];
1443 /// vec.extend_from_slice_clone(&[String::from("a"), String::from("b")]);
1444 /// assert_eq!(vec, ["a", "b", "c"]);
1445 /// ```
1446 #[inline(always)]
1447 #[cfg(feature = "panic-on-alloc")]
1448 pub fn extend_from_slice_clone(&mut self, slice: &[T])
1449 where
1450 T: Clone,
1451 {
1452 panic_on_error(self.generic_extend_from_slice_clone(slice));
1453 }
1454
1455 /// Clones and appends all elements in a slice to the `MutBumpVecRev`.
1456 ///
1457 /// Iterates over the `slice`, clones each element, and then appends
1458 /// it to this `MutBumpVecRev`. The `slice` is traversed in-order.
1459 ///
1460 /// Note that this function is same as [`extend`] except that it is
1461 /// specialized to work with slices instead.
1462 ///
1463 /// [`extend`]: Self::extend
1464 ///
1465 /// # Errors
1466 /// Errors if the allocation fails.
1467 ///
1468 /// # Examples
1469 /// ```
1470 /// # use std::string::String;
1471 /// # use bump_scope::{ Bump, mut_bump_vec_rev };
1472 /// # let mut bump: Bump = Bump::new();
1473 /// let mut vec = mut_bump_vec_rev![try in &mut bump; String::from("c")]?;
1474 /// vec.try_extend_from_slice_clone(&[String::from("a"), String::from("b")])?;
1475 /// assert_eq!(vec, ["a", "b", "c"]);
1476 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1477 /// ```
1478 #[inline(always)]
1479 pub fn try_extend_from_slice_clone(&mut self, slice: &[T]) -> Result<(), AllocError>
1480 where
1481 T: Clone,
1482 {
1483 self.generic_extend_from_slice_clone(slice)
1484 }
1485
1486 #[inline]
1487 pub(crate) fn generic_extend_from_slice_clone<E: ErrorBehavior>(&mut self, slice: &[T]) -> Result<(), E>
1488 where
1489 T: Clone,
1490 {
1491 self.generic_reserve(slice.len())?;
1492
1493 unsafe {
1494 let src = slice.as_ptr().add(slice.len());
1495 let mut pos = 0usize;
1496
1497 while likely(pos != slice.len()) {
1498 let elem = &*src.sub(pos + 1);
1499 self.push_unchecked(elem.clone());
1500 pos += 1;
1501 }
1502 }
1503
1504 Ok(())
1505 }
1506
1507 /// Copies elements from `src` range to the start of the vector.
1508 ///
1509 /// # Panics
1510 /// Panics if the allocation fails.
1511 ///
1512 /// Panics if the starting point is greater than the end point or if
1513 /// the end point is greater than the length of the vector.
1514 ///
1515 /// # Examples
1516 /// ```
1517 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1518 /// # let mut bump: Bump = Bump::new();
1519 /// let mut vec = mut_bump_vec_rev![in &mut bump; 0, 1, 2, 3, 4];
1520 ///
1521 /// vec.extend_from_within_copy(2..);
1522 /// assert_eq!(vec, [2, 3, 4, 0, 1, 2, 3, 4]);
1523 ///
1524 /// vec.extend_from_within_copy(..2);
1525 /// assert_eq!(vec, [2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1526 ///
1527 /// vec.extend_from_within_copy(4..8);
1528 /// assert_eq!(vec, [4, 0, 1, 2, 2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1529 /// ```
1530 #[inline(always)]
1531 #[cfg(feature = "panic-on-alloc")]
1532 pub fn extend_from_within_copy<R>(&mut self, src: R)
1533 where
1534 T: Copy,
1535 R: RangeBounds<usize>,
1536 {
1537 panic_on_error(self.generic_extend_from_within_copy(src));
1538 }
1539
1540 /// Copies elements from `src` range to the start of the vector.
1541 ///
1542 /// # Panics
1543 /// Panics if the starting point is greater than the end point or if
1544 /// the end point is greater than the length of the vector.
1545 ///
1546 /// # Errors
1547 /// Errors if the allocation fails.
1548 ///
1549 /// # Examples
1550 /// ```
1551 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1552 /// # let mut bump: Bump = Bump::try_new()?;
1553 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 0, 1, 2, 3, 4]?;
1554 ///
1555 /// vec.try_extend_from_within_copy(2..)?;
1556 /// assert_eq!(vec, [2, 3, 4, 0, 1, 2, 3, 4]);
1557 ///
1558 /// vec.try_extend_from_within_copy(..2)?;
1559 /// assert_eq!(vec, [2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1560 ///
1561 /// vec.try_extend_from_within_copy(4..8)?;
1562 /// assert_eq!(vec, [4, 0, 1, 2, 2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1563 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1564 /// ```
1565 #[inline(always)]
1566 pub fn try_extend_from_within_copy<R>(&mut self, src: R) -> Result<(), AllocError>
1567 where
1568 T: Copy,
1569 R: RangeBounds<usize>,
1570 {
1571 self.generic_extend_from_within_copy(src)
1572 }
1573
1574 #[inline]
1575 pub(crate) fn generic_extend_from_within_copy<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1576 where
1577 T: Copy,
1578 R: RangeBounds<usize>,
1579 {
1580 let range = polyfill::slice::range(src, ..self.len());
1581 let count = range.len();
1582
1583 self.generic_reserve(count)?;
1584
1585 // SAFETY:
1586 // - `slice::range` guarantees that the given range is valid for indexing self
1587 unsafe {
1588 let ptr = self.as_mut_ptr();
1589
1590 let src = ptr.add(range.start);
1591 let dst = ptr.sub(count);
1592 ptr::copy_nonoverlapping(src, dst, count);
1593 }
1594
1595 self.len += count;
1596 Ok(())
1597 }
1598
1599 /// Clones elements from `src` range to the end of the vector.
1600 ///
1601 /// # Panics
1602 /// Panics if the allocation fails.
1603 ///
1604 /// Panics if the starting point is greater than the end point or if
1605 /// the end point is greater than the length of the vector.
1606 ///
1607 /// # Examples
1608 /// ```
1609 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1610 /// # let mut bump: Bump = Bump::new();
1611 /// let mut vec = mut_bump_vec_rev![in &mut bump; 0, 1, 2, 3, 4];
1612 ///
1613 /// vec.extend_from_within_clone(2..);
1614 /// assert_eq!(vec, [2, 3, 4, 0, 1, 2, 3, 4]);
1615 ///
1616 /// vec.extend_from_within_clone(..2);
1617 /// assert_eq!(vec, [2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1618 ///
1619 /// vec.extend_from_within_clone(4..8);
1620 /// assert_eq!(vec, [4, 0, 1, 2, 2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1621 /// ```
1622 #[inline(always)]
1623 #[cfg(feature = "panic-on-alloc")]
1624 pub fn extend_from_within_clone<R>(&mut self, src: R)
1625 where
1626 T: Clone,
1627 R: RangeBounds<usize>,
1628 {
1629 panic_on_error(self.generic_extend_from_within_clone(src));
1630 }
1631
1632 /// Clones elements from `src` range to the end of the vector.
1633 ///
1634 /// # Panics
1635 /// Panics if the starting point is greater than the end point or if
1636 /// the end point is greater than the length of the vector.
1637 ///
1638 /// # Errors
1639 /// Errors if the allocation fails.
1640 ///
1641 /// # Examples
1642 /// ```
1643 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1644 /// # let mut bump: Bump = Bump::try_new()?;
1645 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 0, 1, 2, 3, 4]?;
1646 ///
1647 /// vec.try_extend_from_within_clone(2..)?;
1648 /// assert_eq!(vec, [2, 3, 4, 0, 1, 2, 3, 4]);
1649 ///
1650 /// vec.try_extend_from_within_clone(..2)?;
1651 /// assert_eq!(vec, [2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1652 ///
1653 /// vec.try_extend_from_within_clone(4..8)?;
1654 /// assert_eq!(vec, [4, 0, 1, 2, 2, 3, 2, 3, 4, 0, 1, 2, 3, 4]);
1655 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1656 /// ```
1657 #[inline(always)]
1658 pub fn try_extend_from_within_clone<R>(&mut self, src: R) -> Result<(), AllocError>
1659 where
1660 T: Clone,
1661 R: RangeBounds<usize>,
1662 {
1663 self.generic_extend_from_within_clone(src)
1664 }
1665
1666 #[inline]
1667 pub(crate) fn generic_extend_from_within_clone<E: ErrorBehavior, R>(&mut self, src: R) -> Result<(), E>
1668 where
1669 T: Clone,
1670 R: RangeBounds<usize>,
1671 {
1672 let range = polyfill::slice::range(src, ..self.len());
1673 let count = range.len();
1674
1675 self.generic_reserve(count)?;
1676
1677 if T::IS_ZST {
1678 unsafe {
1679 // We can materialize ZST's from nothing.
1680 #[expect(clippy::uninit_assumed_init)]
1681 let fake = ManuallyDrop::new(MaybeUninit::<T>::uninit().assume_init());
1682
1683 for _ in 0..count {
1684 self.push_unchecked((*fake).clone());
1685 }
1686
1687 return Ok(());
1688 }
1689 }
1690
1691 // SAFETY:
1692 // - `slice::range` guarantees that the given range is valid for indexing self
1693 unsafe {
1694 let ptr = self.as_mut_ptr();
1695
1696 let mut src = ptr.add(range.end);
1697 let mut dst = ptr;
1698
1699 let src_end = src.sub(count);
1700
1701 while src != src_end {
1702 src = src.sub(1);
1703 dst = dst.sub(1);
1704
1705 dst.write((*src).clone());
1706
1707 self.len += 1;
1708 }
1709 }
1710
1711 Ok(())
1712 }
1713
1714 /// Reserves capacity for at least `additional` more elements to be inserted
1715 /// in the given `MutBumpVecRev<T>`. The collection may reserve more space to
1716 /// speculatively avoid frequent reallocations. After calling `reserve`,
1717 /// capacity will be greater than or equal to `self.len() + additional`.
1718 /// Does nothing if capacity is already sufficient.
1719 ///
1720 /// # Panics
1721 /// Panics if the allocation fails.
1722 ///
1723 /// # Examples
1724 /// ```
1725 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1726 /// # let mut bump: Bump = Bump::new();
1727 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1];
1728 /// vec.reserve(10);
1729 /// assert!(vec.capacity() >= 11);
1730 /// ```
1731 #[inline(always)]
1732 #[cfg(feature = "panic-on-alloc")]
1733 pub fn reserve(&mut self, additional: usize) {
1734 panic_on_error(self.generic_reserve(additional));
1735 }
1736
1737 /// Reserves capacity for at least `additional` more elements to be inserted
1738 /// in the given `MutBumpVecRev<T>`. The collection may reserve more space to
1739 /// speculatively avoid frequent reallocations. After calling `reserve`,
1740 /// capacity will be greater than or equal to `self.len() + additional`.
1741 /// Does nothing if capacity is already sufficient.
1742 ///
1743 /// # Errors
1744 /// Errors if the allocation fails.
1745 ///
1746 /// # Examples
1747 /// ```
1748 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1749 /// # let mut bump: Bump = Bump::try_new()?;
1750 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1]?;
1751 /// vec.try_reserve(10)?;
1752 /// assert!(vec.capacity() >= 11);
1753 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1754 /// ```
1755 #[inline(always)]
1756 pub fn try_reserve(&mut self, additional: usize) -> Result<(), AllocError> {
1757 self.generic_reserve(additional)
1758 }
1759
1760 #[inline]
1761 pub(crate) fn generic_reserve<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1762 if additional > (self.capacity() - self.len()) {
1763 self.generic_grow_amortized(additional)?;
1764 }
1765
1766 Ok(())
1767 }
1768
1769 /// Reserves the minimum capacity for at least `additional` more elements to
1770 /// be inserted in the given `MutBumpVecRev<T>`. Unlike [`reserve`], this will not
1771 /// deliberately over-allocate to speculatively avoid frequent allocations.
1772 /// After calling `reserve_exact`, capacity will be greater than or equal to
1773 /// `self.len() + additional`. Does nothing if the capacity is already
1774 /// sufficient.
1775 ///
1776 /// Note that the allocator may give the collection more space than it
1777 /// requests. Therefore, capacity cannot be relied upon to be precisely
1778 /// minimal. Prefer [`reserve`] if future insertions are expected.
1779 ///
1780 /// [`reserve`]: Self::reserve
1781 ///
1782 /// # Panics
1783 /// Panics if the allocation fails.
1784 ///
1785 /// # Examples
1786 /// ```
1787 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1788 /// # let mut bump: Bump = Bump::new();
1789 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1];
1790 /// vec.reserve_exact(10);
1791 /// assert!(vec.capacity() >= 11);
1792 /// ```
1793 #[inline(always)]
1794 #[cfg(feature = "panic-on-alloc")]
1795 pub fn reserve_exact(&mut self, additional: usize) {
1796 panic_on_error(self.generic_reserve_exact(additional));
1797 }
1798
1799 /// Reserves the minimum capacity for at least `additional` more elements to
1800 /// be inserted in the given `MutBumpVecRev<T>`. Unlike [`reserve`], this will not
1801 /// deliberately over-allocate to speculatively avoid frequent allocations.
1802 /// After calling `reserve_exact`, capacity will be greater than or equal to
1803 /// `self.len() + additional`. Does nothing if the capacity is already
1804 /// sufficient.
1805 ///
1806 /// Note that the allocator may give the collection more space than it
1807 /// requests. Therefore, capacity cannot be relied upon to be precisely
1808 /// minimal. Prefer [`reserve`] if future insertions are expected.
1809 ///
1810 /// [`reserve`]: Self::reserve
1811 ///
1812 /// # Errors
1813 /// Errors if the allocation fails.
1814 ///
1815 /// # Examples
1816 /// ```
1817 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1818 /// # let mut bump: Bump = Bump::try_new()?;
1819 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1]?;
1820 /// vec.try_reserve_exact(10)?;
1821 /// assert!(vec.capacity() >= 11);
1822 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1823 /// ```
1824 #[inline(always)]
1825 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), AllocError> {
1826 self.generic_reserve_exact(additional)
1827 }
1828
1829 #[inline]
1830 pub(crate) fn generic_reserve_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
1831 if additional > (self.capacity() - self.len()) {
1832 self.generic_grow_exact(additional)?;
1833 }
1834
1835 Ok(())
1836 }
1837
1838 /// Resizes the `MutBumpVecRev` in-place so that `len` is equal to `new_len`.
1839 ///
1840 /// If `new_len` is greater than `len`, the `MutBumpVecRev` is extended by the
1841 /// difference, with each additional slot filled with `value`.
1842 /// If `new_len` is less than `len`, the `MutBumpVecRev` is simply truncated.
1843 ///
1844 /// This method requires `T` to implement [`Clone`],
1845 /// in order to be able to clone the passed value.
1846 /// If you need more flexibility (or want to rely on [`Default`] instead of
1847 /// [`Clone`]), use [`resize_with`].
1848 /// If you only need to resize to a smaller size, use [`truncate`].
1849 ///
1850 /// [`resize_with`]: Self::resize_with
1851 /// [`truncate`]: Self::truncate
1852 ///
1853 /// # Panics
1854 /// Panics if the allocation fails.
1855 ///
1856 /// # Examples
1857 /// ```
1858 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1859 /// # let mut bump: Bump = Bump::new();
1860 /// let mut vec = mut_bump_vec_rev![in &mut bump; "hello"];
1861 /// vec.resize(3, "world");
1862 /// assert_eq!(vec, ["world", "world", "hello"]);
1863 /// drop(vec);
1864 ///
1865 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3, 4];
1866 /// vec.resize(2, 0);
1867 /// assert_eq!(vec, [3, 4]);
1868 /// ```
1869 #[inline(always)]
1870 #[cfg(feature = "panic-on-alloc")]
1871 pub fn resize(&mut self, new_len: usize, value: T)
1872 where
1873 T: Clone,
1874 {
1875 panic_on_error(self.generic_resize(new_len, value));
1876 }
1877
1878 /// Resizes the `MutBumpVecRev` in-place so that `len` is equal to `new_len`.
1879 ///
1880 /// If `new_len` is greater than `len`, the `MutBumpVecRev` is extended by the
1881 /// difference, with each additional slot filled with `value`.
1882 /// If `new_len` is less than `len`, the `MutBumpVecRev` is simply truncated.
1883 ///
1884 /// This method requires `T` to implement [`Clone`],
1885 /// in order to be able to clone the passed value.
1886 /// If you need more flexibility (or want to rely on [`Default`] instead of
1887 /// [`Clone`]), use [`resize_with`].
1888 /// If you only need to resize to a smaller size, use [`truncate`].
1889 ///
1890 /// [`resize_with`]: Self::resize_with
1891 /// [`truncate`]: Self::truncate
1892 ///
1893 /// # Errors
1894 /// Errors if the allocation fails.
1895 ///
1896 /// # Examples
1897 /// ```
1898 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1899 /// # let mut bump: Bump = Bump::try_new()?;
1900 /// let mut vec = mut_bump_vec_rev![try in &mut bump; "hello"]?;
1901 /// vec.try_resize(3, "world")?;
1902 /// assert_eq!(vec, ["world", "world", "hello"]);
1903 /// drop(vec);
1904 ///
1905 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3, 4]?;
1906 /// vec.try_resize(2, 0)?;
1907 /// assert_eq!(vec, [3, 4]);
1908 /// # Ok::<(), bump_scope::alloc::AllocError>(())
1909 /// ```
1910 #[inline(always)]
1911 pub fn try_resize(&mut self, new_len: usize, value: T) -> Result<(), AllocError>
1912 where
1913 T: Clone,
1914 {
1915 self.generic_resize(new_len, value)
1916 }
1917
1918 #[inline]
1919 pub(crate) fn generic_resize<E: ErrorBehavior>(&mut self, new_len: usize, value: T) -> Result<(), E>
1920 where
1921 T: Clone,
1922 {
1923 let len = self.len();
1924
1925 if new_len > len {
1926 self.extend_with(new_len - len, value)
1927 } else {
1928 self.truncate(new_len);
1929 Ok(())
1930 }
1931 }
1932
1933 /// Resizes the `MutBumpVecRev` in-place so that `len` is equal to `new_len`.
1934 ///
1935 /// If `new_len` is greater than `len`, the `MutBumpVecRev` is extended by the
1936 /// difference, with each additional slot filled with the result of
1937 /// calling the closure `f`. The return values from `f` will end up
1938 /// in the `MutBumpVecRev` in the order they have been generated.
1939 ///
1940 /// If `new_len` is less than `len`, the `MutBumpVecRev` is simply truncated.
1941 ///
1942 /// This method uses a closure to create new values on every push. If
1943 /// you'd rather [`Clone`] a given value, use [`MutBumpVecRev::resize`]. If you
1944 /// want to use the [`Default`] trait to generate values, you can
1945 /// pass [`Default::default`] as the second argument.
1946 ///
1947 /// # Panics
1948 /// Panics if the allocation fails.
1949 ///
1950 /// # Examples
1951 /// ```
1952 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1953 /// # let mut bump: Bump = Bump::new();
1954 /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
1955 /// vec.resize_with(5, Default::default);
1956 /// assert_eq!(vec, [0, 0, 1, 2, 3]);
1957 /// drop(vec);
1958 ///
1959 /// let mut vec = mut_bump_vec_rev![in &mut bump];
1960 /// let mut p = 1;
1961 /// vec.resize_with(4, || { p *= 2; p });
1962 /// assert_eq!(vec, [16, 8, 4, 2]);
1963 /// ```
1964 #[inline(always)]
1965 #[cfg(feature = "panic-on-alloc")]
1966 pub fn resize_with<F>(&mut self, new_len: usize, f: F)
1967 where
1968 F: FnMut() -> T,
1969 {
1970 panic_on_error(self.generic_resize_with(new_len, f));
1971 }
1972
1973 /// Resizes the `MutBumpVecRev` in-place so that `len` is equal to `new_len`.
1974 ///
1975 /// If `new_len` is greater than `len`, the `MutBumpVecRev` is extended by the
1976 /// difference, with each additional slot filled with the result of
1977 /// calling the closure `f`. The return values from `f` will end up
1978 /// in the `MutBumpVecRev` in the order they have been generated.
1979 ///
1980 /// If `new_len` is less than `len`, the `MutBumpVecRev` is simply truncated.
1981 ///
1982 /// This method uses a closure to create new values on every push. If
1983 /// you'd rather [`Clone`] a given value, use [`MutBumpVecRev::resize`]. If you
1984 /// want to use the [`Default`] trait to generate values, you can
1985 /// pass [`Default::default`] as the second argument.
1986 ///
1987 /// # Errors
1988 /// Errors if the allocation fails.
1989 ///
1990 /// # Examples
1991 /// ```
1992 /// # use bump_scope::{Bump, mut_bump_vec_rev};
1993 /// # let mut bump: Bump = Bump::try_new()?;
1994 /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3]?;
1995 /// vec.try_resize_with(5, Default::default)?;
1996 /// assert_eq!(vec, [0, 0, 1, 2, 3]);
1997 /// drop(vec);
1998 ///
1999 /// let mut vec = mut_bump_vec_rev![try in &mut bump]?;
2000 /// let mut p = 1;
2001 /// vec.try_resize_with(4, || { p *= 2; p })?;
2002 /// assert_eq!(vec, [16, 8, 4, 2]);
2003 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2004 /// ```
2005 #[inline(always)]
2006 pub fn try_resize_with<F>(&mut self, new_len: usize, f: F) -> Result<(), AllocError>
2007 where
2008 F: FnMut() -> T,
2009 {
2010 self.generic_resize_with(new_len, f)
2011 }
2012
2013 #[inline]
2014 pub(crate) fn generic_resize_with<E: ErrorBehavior, F>(&mut self, new_len: usize, f: F) -> Result<(), E>
2015 where
2016 F: FnMut() -> T,
2017 {
2018 let len = self.len();
2019 if new_len > len {
2020 unsafe { self.extend_trusted(iter::repeat_with(f).take(new_len - len)) }
2021 } else {
2022 self.truncate(new_len);
2023 Ok(())
2024 }
2025 }
2026
2027 /// Moves all the elements of `other` into `self`, leaving `other` empty.
2028 ///
2029 /// # Panics
2030 /// Panics if the allocation fails.
2031 ///
2032 /// # Examples
2033 /// ```
2034 /// # use bump_scope::{Bump, MutBumpVecRev};
2035 /// # let mut bump: Bump = Bump::new();
2036 /// # let bump2: Bump = Bump::new();
2037 /// let mut vec = MutBumpVecRev::new_in(&mut bump);
2038 ///
2039 /// // append by value
2040 /// vec.append([1, 2]);
2041 /// vec.append(vec![3, 4]);
2042 /// vec.append(bump2.alloc_iter(5..=6));
2043 ///
2044 /// // append by mutable reference
2045 /// let mut other = vec![7, 8];
2046 /// vec.append(&mut other);
2047 ///
2048 /// assert_eq!(other, []);
2049 /// assert_eq!(vec, [7, 8, 5, 6, 3, 4, 1, 2]);
2050 /// ```
2051 #[inline(always)]
2052 #[cfg(feature = "panic-on-alloc")]
2053 pub fn append(&mut self, other: impl OwnedSlice<Item = T>) {
2054 panic_on_error(self.generic_append(other));
2055 }
2056
2057 /// Moves all the elements of `other` into `self`, leaving `other` empty.
2058 ///
2059 /// # Errors
2060 /// Errors if the allocation fails.
2061 ///
2062 /// # Examples
2063 /// ```
2064 /// # use bump_scope::{Bump, MutBumpVecRev};
2065 /// # let mut bump: Bump = Bump::new();
2066 /// # let bump2: Bump = Bump::new();
2067 /// let mut vec = MutBumpVecRev::new_in(&mut bump);
2068 ///
2069 /// // append by value
2070 /// vec.try_append([1, 2])?;
2071 /// vec.try_append(vec![3, 4])?;
2072 /// vec.try_append(bump2.alloc_iter(5..=6))?;
2073 ///
2074 /// // append by mutable reference
2075 /// let mut other = vec![7, 8];
2076 /// vec.try_append(&mut other)?;
2077 ///
2078 /// assert_eq!(other, []);
2079 /// assert_eq!(vec, [7, 8, 5, 6, 3, 4, 1, 2]);
2080 /// # Ok::<(), bump_scope::alloc::AllocError>(())
2081 /// ```
2082 #[inline(always)]
2083 pub fn try_append(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), AllocError> {
2084 self.generic_append(other)
2085 }
2086
2087 #[inline]
2088 pub(crate) fn generic_append<E: ErrorBehavior>(&mut self, other: impl OwnedSlice<Item = T>) -> Result<(), E> {
2089 unsafe {
2090 let mut owned_slice = other.into_take_owned_slice();
2091
2092 let slice = NonNull::from(owned_slice.owned_slice_ref());
2093 self.generic_reserve(slice.len())?;
2094
2095 let src = slice.cast::<T>().as_ptr();
2096 let dst = self.end.as_ptr().sub(self.len + slice.len());
2097 ptr::copy_nonoverlapping(src, dst, slice.len());
2098
2099 owned_slice.take_owned_slice();
2100 self.inc_len(slice.len());
2101 Ok(())
2102 }
2103 }
2104
2105 /// Extend the vector by `n` clones of value.
2106 fn extend_with<B: ErrorBehavior>(&mut self, n: usize, value: T) -> Result<(), B>
2107 where
2108 T: Clone,
2109 {
2110 self.generic_reserve(n)?;
2111
2112 unsafe {
2113 let mut ptr = self.as_mut_ptr().sub(1);
2114
2115 // Use SetLenOnDrop to work around bug where compiler
2116 // might not realize the store through `ptr` through self.set_len()
2117 // don't alias.
2118 let mut local_len = SetLenOnDrop::new(&mut self.len);
2119
2120 // Write all elements except the last one
2121 for _ in 1..n {
2122 pointer::write_with(ptr, || value.clone());
2123 ptr = ptr.sub(1);
2124
2125 // Increment the length in every step in case clone() panics
2126 local_len.increment_len(1);
2127 }
2128
2129 if n > 0 {
2130 // We can write the last element directly without cloning needlessly
2131 ptr.write(value);
2132 local_len.increment_len(1);
2133 }
2134
2135 Ok(())
2136 // len set by scope guard
2137 }
2138 }
2139
2140 #[inline(always)]
2141 unsafe fn extend_by_copy_nonoverlapping<E: ErrorBehavior>(&mut self, other: *const [T]) -> Result<(), E> {
2142 unsafe {
2143 let len = other.len();
2144 self.generic_reserve(len)?;
2145
2146 let src = other.cast::<T>();
2147 self.len += len;
2148 let dst = self.as_mut_ptr();
2149
2150 ptr::copy_nonoverlapping(src, dst, len);
2151
2152 Ok(())
2153 }
2154 }
2155
2156 #[inline]
2157 fn generic_reserve_one<E: ErrorBehavior>(&mut self) -> Result<(), E> {
2158 if self.cap == self.len {
2159 self.generic_grow_amortized::<E>(1)?;
2160 }
2161
2162 Ok(())
2163 }
2164
2165 #[cold]
2166 #[inline(never)]
2167 fn generic_grow_amortized<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2168 if T::IS_ZST {
2169 // This function is only called after we checked that the current capacity is not
2170 // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2171 return Err(E::capacity_overflow());
2172 }
2173
2174 let Some(required_cap) = self.len().checked_add(additional) else {
2175 return Err(E::capacity_overflow())?;
2176 };
2177
2178 // This guarantees exponential growth. The doubling cannot overflow
2179 // because `capacity <= isize::MAX` and the type of `capacity` is usize;
2180 let new_cap = (self.capacity() * 2).max(required_cap).max(min_non_zero_cap(T::SIZE));
2181
2182 unsafe { self.generic_grow_to(new_cap) }
2183 }
2184
2185 #[cold]
2186 #[inline(never)]
2187 fn generic_grow_exact<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
2188 if T::IS_ZST {
2189 // This function is only called after we checked that the current capacity is not
2190 // sufficient. When `T::IS_ZST` the capacity is `usize::MAX`, so it can't grow.
2191 return Err(E::capacity_overflow());
2192 }
2193
2194 let Some(required_cap) = self.len().checked_add(additional) else {
2195 return Err(E::capacity_overflow());
2196 };
2197
2198 unsafe { self.generic_grow_to(required_cap) }
2199 }
2200
2201 /// # Safety
2202 ///
2203 /// `new_capacity` must be greater than the current capacity.
2204 unsafe fn generic_grow_to<E: ErrorBehavior>(&mut self, new_capacity: usize) -> Result<(), E> {
2205 unsafe {
2206 let slice = E::prepare_slice_allocation::<T>(&mut self.allocator, new_capacity)?;
2207 let cap = slice.len();
2208 let end = non_null::as_non_null_ptr(slice).add(cap);
2209
2210 let src = self.as_mut_ptr();
2211 let dst = end.as_ptr().sub(self.len);
2212 ptr::copy_nonoverlapping(src, dst, self.len);
2213
2214 self.end = end;
2215 self.cap = cap;
2216
2217 Ok(())
2218 }
2219 }
2220
2221 /// Removes and returns the element at position `index` within the vector,
2222 /// shifting all elements after it to the right.
2223 ///
2224 /// Note: Because this shifts over the remaining elements, it has a
2225 /// worst-case performance of *O*(*n*). If you don't need the order of elements
2226 /// to be preserved, use [`swap_remove`] instead.
2227 ///
2228 /// # Panics
2229 /// Panics if `index` is out of bounds.
2230 ///
2231 /// [`swap_remove`]: Self::swap_remove
2232 ///
2233 /// # Examples
2234 ///
2235 /// ```
2236 /// # use bump_scope::{Bump, mut_bump_vec_rev};
2237 /// # let mut bump: Bump = Bump::new();
2238 /// let mut v = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
2239 /// assert_eq!(v.remove(1), 2);
2240 /// assert_eq!(v, [1, 3]);
2241 /// ```
2242 #[track_caller]
2243 pub fn remove(&mut self, index: usize) -> T {
2244 #[cold]
2245 #[track_caller]
2246 fn assert_failed(index: usize, len: usize) -> ! {
2247 panic!("removal index (is {index}) should be < len (is {len})");
2248 }
2249
2250 if index >= self.len {
2251 assert_failed(index, self.len);
2252 }
2253
2254 unsafe {
2255 let start = self.as_mut_ptr();
2256 let value_ptr = start.add(index);
2257
2258 // copy it out, unsafely having a copy of the value on
2259 // the stack and in the vector at the same time
2260 let value = value_ptr.read();
2261
2262 // shift everything to fill in that spot
2263 if index != 0 {
2264 start.copy_to(start.add(1), index);
2265 }
2266
2267 self.len -= 1;
2268 value
2269 }
2270 }
2271
2272 /// Removes an element from the vector and returns it.
2273 ///
2274 /// The removed element is replaced by the first element of the vector.
2275 ///
2276 /// This does not preserve ordering, but is *O*(1).
2277 /// If you need to preserve the element order, use [`remove`] instead.
2278 ///
2279 /// # Panics
2280 /// Panics if `index` is out of bounds.
2281 ///
2282 /// [`remove`]: Self::remove
2283 ///
2284 /// # Examples
2285 ///
2286 /// ```
2287 /// # use bump_scope::{Bump, mut_bump_vec_rev};
2288 /// # let mut bump: Bump = Bump::new();
2289 /// #
2290 /// let mut v = mut_bump_vec_rev![in &mut bump; "foo", "bar", "baz", "qux"];
2291 ///
2292 /// assert_eq!(v.swap_remove(1), "bar");
2293 /// assert_eq!(v, ["foo", "baz", "qux"]);
2294 ///
2295 /// assert_eq!(v.swap_remove(0), "foo");
2296 /// assert_eq!(v, ["baz", "qux"]);
2297 /// ```
2298 #[inline]
2299 pub fn swap_remove(&mut self, index: usize) -> T {
2300 #[cold]
2301 #[inline(never)]
2302 #[track_caller]
2303 fn assert_failed(index: usize, len: usize) -> ! {
2304 panic!("swap_remove index (is {index}) should be < len (is {len})");
2305 }
2306
2307 if index >= self.len {
2308 assert_failed(index, self.len);
2309 }
2310
2311 unsafe {
2312 // We replace self[index] with the first element. Note that if the
2313 // bounds check above succeeds there must be a first element (which
2314 // can be self[index] itself).
2315
2316 let start = self.as_mut_ptr();
2317 let value_ptr = start.add(index);
2318 let value = value_ptr.read();
2319 self.len -= 1;
2320
2321 start.copy_to(value_ptr, 1);
2322 value
2323 }
2324 }
2325
2326 #[must_use]
2327 #[inline]
2328 fn into_slice_ptr(self) -> NonNull<[T]> {
2329 let this = ManuallyDrop::new(self);
2330
2331 if T::IS_ZST {
2332 return NonNull::slice_from_raw_parts(NonNull::dangling(), this.len());
2333 }
2334
2335 if this.cap == 0 {
2336 // We didn't touch the bump, so no need to do anything.
2337 debug_assert_eq!(this.end, NonNull::<T>::dangling());
2338 return NonNull::slice_from_raw_parts(NonNull::<T>::dangling(), 0);
2339 }
2340
2341 let end = this.end;
2342 let len = this.len;
2343 let cap = this.cap;
2344 unsafe { this.allocator.allocate_prepared_slice_rev(end, len, cap) }
2345 }
2346
2347 /// # Safety
2348 ///
2349 /// `iterator` must satisfy the invariants of nightly's `TrustedLen`.
2350 // specific extend for `TrustedLen` iterators, called both by the specializations
2351 // and internal places where resolving specialization makes compilation slower
2352 unsafe fn extend_trusted<B: ErrorBehavior>(&mut self, iterator: impl Iterator<Item = T>) -> Result<(), B> {
2353 unsafe {
2354 let (low, high) = iterator.size_hint();
2355 if let Some(additional) = high {
2356 debug_assert_eq!(
2357 low,
2358 additional,
2359 "TrustedLen iterator's size hint is not exact: {:?}",
2360 (low, high)
2361 );
2362
2363 self.generic_reserve(additional)?;
2364
2365 let ptr = self.end.as_ptr();
2366 let mut local_len = SetLenOnDrop::new(&mut self.len);
2367
2368 iterator.for_each(move |element| {
2369 let dst = ptr.sub(local_len.current_len() + 1);
2370
2371 ptr::write(dst, element);
2372 // Since the loop executes user code which can panic we have to update
2373 // the length every step to correctly drop what we've written.
2374 // NB can't overflow since we would have had to alloc the address space
2375 local_len.increment_len(1);
2376 });
2377
2378 Ok(())
2379 } else {
2380 // Per TrustedLen contract a `None` upper bound means that the iterator length
2381 // truly exceeds usize::MAX, which would eventually lead to a capacity overflow anyway.
2382 // Since the other branch already panics eagerly (via `reserve()`) we do the same here.
2383 // This avoids additional codegen for a fallback code path which would eventually
2384 // panic anyway.
2385 Err(B::capacity_overflow())
2386 }
2387 }
2388 }
2389
2390 /// Returns the remaining spare capacity of the vector as a slice of
2391 /// `MaybeUninit<T>`.
2392 ///
2393 /// The returned slice can be used to fill the vector with data (e.g. by
2394 /// reading from a file) before marking the data as initialized using the
2395 /// [`set_len`] method.
2396 ///
2397 /// [`set_len`]: Self::set_len
2398 ///
2399 /// # Examples
2400 ///
2401 /// ```
2402 /// # use bump_scope::{Bump, MutBumpVecRev};
2403 /// # let mut bump: Bump = Bump::new();
2404 /// // Allocate vector big enough for 10 elements.
2405 /// let mut v = MutBumpVecRev::with_capacity_in(10, &mut bump);
2406 ///
2407 /// // Fill in the first 3 elements.
2408 /// let uninit = v.spare_capacity_mut();
2409 /// let len = uninit.len();
2410 /// uninit[len - 3].write(0);
2411 /// uninit[len - 2].write(1);
2412 /// uninit[len - 1].write(2);
2413 ///
2414 /// // Mark the first 3 elements of the vector as being initialized.
2415 /// unsafe {
2416 /// v.set_len(3);
2417 /// }
2418 ///
2419 /// assert_eq!(&v, &[0, 1, 2]);
2420 /// ```
2421 #[inline]
2422 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
2423 // Note:
2424 // This method is not implemented in terms of `split_at_spare_mut`,
2425 // to prevent invalidation of pointers to the buffer.
2426 unsafe {
2427 slice::from_raw_parts_mut(
2428 self.end.as_ptr().sub(self.capacity()).cast::<MaybeUninit<T>>(),
2429 self.capacity() - self.len(),
2430 )
2431 }
2432 }
2433
2434 /// Returns vector content as a slice of `T`, along with the remaining spare
2435 /// capacity of the vector as a slice of `MaybeUninit<T>`.
2436 ///
2437 /// The returned spare capacity slice can be used to fill the vector with data
2438 /// (e.g. by reading from a file) before marking the data as initialized using
2439 /// the [`set_len`] method.
2440 ///
2441 /// [`set_len`]: Self::set_len
2442 ///
2443 /// Note that this is a low-level API, which should be used with care for
2444 /// optimization purposes. If you need to append data to a `MutBumpVecRev`
2445 /// you can use [`push`], [`extend`], `extend_from_slice`[`_copy`](MutBumpVecRev::extend_from_slice_copy)`/`[`_clone`](MutBumpVecRev::extend_from_within_clone),
2446 /// `extend_from_within`[`_copy`](MutBumpVecRev::extend_from_within_copy)`/`[`_clone`](MutBumpVecRev::extend_from_within_clone), [`insert`], [`resize`] or
2447 /// [`resize_with`], depending on your exact needs.
2448 ///
2449 /// [`push`]: Self::push
2450 /// [`extend`]: Self::extend
2451 /// [`insert`]: Self::insert
2452 /// [`append`]: Self::append
2453 /// [`resize`]: Self::resize
2454 /// [`resize_with`]: Self::resize_with
2455 #[inline]
2456 pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
2457 // SAFETY:
2458 // - len is ignored and so never changed
2459 let (init, spare, _) = unsafe { self.split_at_spare_mut_with_len() };
2460 (init, spare)
2461 }
2462
2463 /// Safety: changing returned .2 (&mut usize) is considered the same as calling `.set_len(_)`.
2464 ///
2465 /// This method provides unique access to all vec parts at once in `extend_from_within_clone`.
2466 unsafe fn split_at_spare_mut_with_len(&mut self) -> (&mut [T], &mut [MaybeUninit<T>], &mut usize) {
2467 unsafe {
2468 let end = self.end.as_ptr();
2469
2470 let spare_ptr = end.sub(self.cap);
2471 let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
2472 let spare_len = self.cap - self.len;
2473
2474 let initialized = slice::from_raw_parts_mut(self.as_mut_ptr(), self.len);
2475 let spare = slice::from_raw_parts_mut(spare_ptr, spare_len);
2476
2477 (initialized, spare, &mut self.len)
2478 }
2479 }
2480
2481 /// Returns a type which provides statistics about the memory usage of the bump allocator.
2482 ///
2483 /// This collection does not update the bump pointer, so it also doesn't contribute to the `remaining` and `allocated` stats.
2484 #[must_use]
2485 #[inline(always)]
2486 pub fn allocator_stats(&self) -> A::Stats<'_> {
2487 self.allocator.stats()
2488 }
2489}
2490
2491impl<'a, T, A: MutBumpAllocatorScopeExt<'a>> MutBumpVecRev<T, A> {
2492 /// Turns this `MutBumpVecRev<T>` into a `BumpBox<[T]>`.
2493 ///
2494 /// Unused capacity does not take up space.<br/>
2495 /// When [bumping upwards](crate#bumping-upwards-or-downwards) this needs to shift all elements to the other end of the chunk.
2496 #[must_use]
2497 #[inline(always)]
2498 pub fn into_boxed_slice(self) -> BumpBox<'a, [T]> {
2499 unsafe { BumpBox::from_raw(self.into_slice_ptr()) }
2500 }
2501
2502 /// Turns this `MutBumpVecRev<T>` into a `&[T]` that is live for this bump scope.
2503 ///
2504 /// Unused capacity does not take up space.<br/>
2505 /// When [bumping upwards](crate#bumping-upwards-or-downwards) this needs to shift all elements to the other end of the chunk.
2506 ///
2507 /// This is only available for [`NoDrop`] types so you don't omit dropping a value for which it matters.
2508 ///
2509 /// `!NoDrop` types can still be turned into slices via <code>BumpBox::[leak](BumpBox::leak)(vec.[into_boxed_slice](Self::into_boxed_slice)())</code>.
2510 #[must_use]
2511 #[inline(always)]
2512 pub fn into_slice(self) -> &'a mut [T]
2513 where
2514 [T]: NoDrop,
2515 {
2516 self.into_boxed_slice().into_mut()
2517 }
2518}
2519
2520impl<T, A, const N: usize> MutBumpVecRev<[T; N], A> {
2521 /// Takes a `MutBumpVecRev<[T; N]>` and flattens it into a `MutBumpVecRev<T>`.
2522 ///
2523 /// # Panics
2524 ///
2525 /// Panics if the length of the resulting vector would overflow a `usize`.
2526 ///
2527 /// This is only possible when flattening a vector of arrays of zero-sized
2528 /// types, and thus tends to be irrelevant in practice. If
2529 /// `size_of::<T>() > 0`, this will never panic.
2530 ///
2531 /// # Examples
2532 ///
2533 /// ```
2534 /// # use bump_scope::{Bump, mut_bump_vec_rev};
2535 /// # let mut bump: Bump = Bump::new();
2536 /// #
2537 /// let mut vec = mut_bump_vec_rev![in &mut bump; [1, 2, 3], [4, 5, 6], [7, 8, 9]];
2538 /// assert_eq!(vec.pop(), Some([1, 2, 3]));
2539 ///
2540 /// let mut flattened = vec.into_flattened();
2541 /// assert_eq!(flattened.pop(), Some(4));
2542 /// ```
2543 #[must_use]
2544 pub fn into_flattened(self) -> MutBumpVecRev<T, A> {
2545 let (end, len, cap, allocator) = self.into_raw_parts();
2546
2547 let (new_len, new_cap) = if T::IS_ZST {
2548 (len.checked_mul(N).expect("vec len overflow"), usize::MAX)
2549 } else {
2550 // SAFETY:
2551 // - `cap * N` cannot overflow because the allocation is already in
2552 // the address space.
2553 // - Each `[T; N]` has `N` valid elements, so there are `len * N`
2554 // valid elements in the allocation.
2555 unsafe { (polyfill::usize::unchecked_mul(len, N), polyfill::usize::unchecked_mul(cap, N)) }
2556 };
2557
2558 unsafe { MutBumpVecRev::from_raw_parts(end.cast(), new_len, new_cap, allocator) }
2559 }
2560}
2561
2562impl<T: Debug, A> Debug for MutBumpVecRev<T, A> {
2563 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2564 Debug::fmt(self.as_slice(), f)
2565 }
2566}
2567
2568impl<T, A> Deref for MutBumpVecRev<T, A> {
2569 type Target = [T];
2570
2571 #[inline(always)]
2572 fn deref(&self) -> &Self::Target {
2573 self.as_slice()
2574 }
2575}
2576
2577impl<T, A> DerefMut for MutBumpVecRev<T, A> {
2578 #[inline(always)]
2579 fn deref_mut(&mut self) -> &mut Self::Target {
2580 self.as_mut_slice()
2581 }
2582}
2583
2584impl<T, A: Default> Default for MutBumpVecRev<T, A> {
2585 fn default() -> Self {
2586 Self::new_in(A::default())
2587 }
2588}
2589
2590impl<T, A, I: SliceIndex<[T]>> Index<I> for MutBumpVecRev<T, A> {
2591 type Output = I::Output;
2592
2593 #[inline(always)]
2594 fn index(&self, index: I) -> &Self::Output {
2595 Index::index(self.as_slice(), index)
2596 }
2597}
2598
2599impl<T, A, I: SliceIndex<[T]>> IndexMut<I> for MutBumpVecRev<T, A> {
2600 #[inline(always)]
2601 fn index_mut(&mut self, index: I) -> &mut Self::Output {
2602 IndexMut::index_mut(self.as_mut_slice(), index)
2603 }
2604}
2605
2606#[cfg(feature = "panic-on-alloc")]
2607impl<U, A: MutBumpAllocatorExt> Extend<U> for MutBumpVecRev<U, A> {
2608 #[inline]
2609 fn extend<T: IntoIterator<Item = U>>(&mut self, iter: T) {
2610 let iter = iter.into_iter();
2611
2612 self.reserve(iter.size_hint().0);
2613
2614 for value in iter {
2615 self.push(value);
2616 }
2617 }
2618}
2619
2620impl<T, A> MutBumpVecRev<T, A> {
2621 /// # Safety
2622 ///
2623 /// Must only be called from the drop implementation and a call to this function
2624 /// must be the only thing in that drop implementation.
2625 #[inline]
2626 unsafe fn drop_inner(&mut self) {
2627 // MutBumpVecRev never actually moves a bump pointer.
2628 // It may force allocation of a new chunk, but it does not move the pointer within.
2629 // So we don't need to move the bump pointer when dropping.
2630
2631 // If we want to reset the bump pointer to a previous chunk, we use a bump scope.
2632 // We could do it here, by resetting to the last non-empty chunk but that would require a loop.
2633 // Chunk allocations are supposed to be very rare, so this wouldn't be worth it.
2634
2635 unsafe {
2636 let to_drop = NonNull::slice_from_raw_parts(self.as_non_null(), self.len);
2637 to_drop.as_ptr().drop_in_place();
2638 }
2639 }
2640}
2641
2642#[cfg(feature = "nightly-dropck-eyepatch")]
2643unsafe impl<#[may_dangle] T, A> Drop for MutBumpVecRev<T, A> {
2644 #[inline(always)]
2645 fn drop(&mut self) {
2646 unsafe { self.drop_inner() }
2647 }
2648}
2649
2650#[cfg(not(feature = "nightly-dropck-eyepatch"))]
2651impl<T, A> Drop for MutBumpVecRev<T, A> {
2652 #[inline(always)]
2653 fn drop(&mut self) {
2654 unsafe { self.drop_inner() }
2655 }
2656}
2657
2658#[cfg(feature = "panic-on-alloc")]
2659impl<'t, T: Clone + 't, A: MutBumpAllocatorExt> Extend<&'t T> for MutBumpVecRev<T, A> {
2660 #[inline]
2661 fn extend<I: IntoIterator<Item = &'t T>>(&mut self, iter: I) {
2662 let iter = iter.into_iter();
2663
2664 self.reserve(iter.size_hint().0);
2665
2666 for value in iter {
2667 self.push(value.clone());
2668 }
2669 }
2670}
2671
2672impl<T, A> IntoIterator for MutBumpVecRev<T, A> {
2673 type Item = T;
2674 type IntoIter = IntoIter<T, A>;
2675
2676 /// If you need to use the allocator while iterating you can first turn it to a slice with [`into_slice`] or [`into_boxed_slice`].
2677 ///
2678 /// [`into_slice`]: Self::into_slice
2679 /// [`into_boxed_slice`]: Self::into_boxed_slice
2680 #[inline(always)]
2681 fn into_iter(self) -> Self::IntoIter {
2682 let (end, len, _cap, allocator) = self.into_raw_parts();
2683 let start = unsafe { end.sub(len) };
2684 let slice = NonNull::slice_from_raw_parts(start, len);
2685 unsafe { IntoIter::new(slice, allocator) }
2686 }
2687}
2688
2689impl<'c, T, A> IntoIterator for &'c MutBumpVecRev<T, A> {
2690 type Item = &'c T;
2691 type IntoIter = slice::Iter<'c, T>;
2692
2693 #[inline(always)]
2694 fn into_iter(self) -> Self::IntoIter {
2695 self.as_slice().iter()
2696 }
2697}
2698
2699impl<'c, T, A> IntoIterator for &'c mut MutBumpVecRev<T, A> {
2700 type Item = &'c mut T;
2701 type IntoIter = slice::IterMut<'c, T>;
2702
2703 #[inline(always)]
2704 fn into_iter(self) -> Self::IntoIter {
2705 self.as_mut_slice().iter_mut()
2706 }
2707}
2708
2709impl<T, A> AsRef<Self> for MutBumpVecRev<T, A> {
2710 #[inline(always)]
2711 fn as_ref(&self) -> &Self {
2712 self
2713 }
2714}
2715
2716impl<T, A> AsMut<Self> for MutBumpVecRev<T, A> {
2717 #[inline(always)]
2718 fn as_mut(&mut self) -> &mut Self {
2719 self
2720 }
2721}
2722
2723impl<T, A> AsRef<[T]> for MutBumpVecRev<T, A> {
2724 #[inline(always)]
2725 fn as_ref(&self) -> &[T] {
2726 self
2727 }
2728}
2729
2730impl<T, A> AsMut<[T]> for MutBumpVecRev<T, A> {
2731 #[inline(always)]
2732 fn as_mut(&mut self) -> &mut [T] {
2733 self
2734 }
2735}
2736
2737impl<T, A> Borrow<[T]> for MutBumpVecRev<T, A> {
2738 #[inline(always)]
2739 fn borrow(&self) -> &[T] {
2740 self
2741 }
2742}
2743
2744impl<T, A> BorrowMut<[T]> for MutBumpVecRev<T, A> {
2745 #[inline(always)]
2746 fn borrow_mut(&mut self) -> &mut [T] {
2747 self
2748 }
2749}
2750
2751impl<T: Hash, A> Hash for MutBumpVecRev<T, A> {
2752 #[inline(always)]
2753 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
2754 self.as_slice().hash(state);
2755 }
2756}
2757
2758#[cfg(feature = "panic-on-alloc")]
2759impl<T, A: MutBumpAllocatorExt + Default> FromIterator<T> for MutBumpVecRev<T, A> {
2760 #[inline]
2761 #[track_caller]
2762 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2763 Self::from_iter_in(iter, A::default())
2764 }
2765}