bump_scope/features/bytemuck/
vec_ext.rs

1use ::bytemuck::Zeroable;
2
3use crate::{
4    BumpAllocatorExt, BumpVec, ErrorBehavior, FixedBumpVec, MutBumpAllocatorExt, MutBumpVec, MutBumpVecRev,
5    alloc::AllocError,
6};
7
8#[cfg(feature = "panic-on-alloc")]
9use crate::panic_on_error;
10
11mod private {
12    use super::*;
13
14    pub trait Sealed {}
15
16    impl<T> Sealed for FixedBumpVec<'_, T> {}
17    impl<T, A: BumpAllocatorExt> Sealed for BumpVec<T, A> {}
18    impl<T, A: MutBumpAllocatorExt> Sealed for MutBumpVec<T, A> {}
19    impl<T, A: MutBumpAllocatorExt> Sealed for MutBumpVecRev<T, A> {}
20}
21
22/// Extension trait for this crate's vector types.
23pub trait VecExt: private::Sealed {
24    /// The element type of this vector.
25    type T;
26
27    /// Extends this vector by pushing `additional` new items onto the end.
28    /// The new items are initialized with zeroes.
29    ///
30    /// # Panics
31    /// Panics if the allocation fails.
32    ///
33    /// # Examples
34    /// ```
35    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
36    /// let bump: Bump = Bump::new();
37    ///
38    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
39    /// vec.extend_zeroed(2);
40    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
41    /// ```
42    #[cfg(feature = "panic-on-alloc")]
43    fn extend_zeroed(&mut self, additional: usize)
44    where
45        Self::T: Zeroable;
46
47    /// Extends this vector by pushing `additional` new items onto the end.
48    /// The new items are initialized with zeroes.
49    ///
50    /// # Errors
51    /// Errors if the allocation fails.
52    ///
53    /// # Examples
54    /// ```
55    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
56    /// let bump: Bump = Bump::try_new()?;
57    ///
58    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
59    /// vec.try_extend_zeroed(2)?;
60    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
61    /// # Ok::<(), bump_scope::alloc::AllocError>(())
62    /// ```
63    fn try_extend_zeroed(&mut self, additional: usize) -> Result<(), AllocError>
64    where
65        Self::T: Zeroable;
66
67    /// Resizes this vector in-place so that `len` is equal to `new_len`.
68    ///
69    /// If `new_len` is greater than `len`, the vector is extended by the
70    /// difference, with each additional slot filled with `value`.
71    /// If `new_len` is less than `len`, the vector is simply truncated.
72    ///
73    /// # Panics
74    /// Panics if the allocation fails.
75    ///
76    /// # Examples
77    /// ```
78    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
79    /// let bump: Bump = Bump::new();
80    ///
81    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
82    /// vec.resize_zeroed(5);
83    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
84    ///
85    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
86    /// vec.resize_zeroed(2);
87    /// assert_eq!(vec, [1, 2]);
88    /// ```
89    #[cfg(feature = "panic-on-alloc")]
90    fn resize_zeroed(&mut self, new_len: usize)
91    where
92        Self::T: Zeroable;
93
94    /// Resizes this vector in-place so that `len` is equal to `new_len`.
95    ///
96    /// If `new_len` is greater than `len`, the vector is extended by the
97    /// difference, with each additional slot filled with `value`.
98    /// If `new_len` is less than `len`, the vector is simply truncated.
99    ///
100    /// # Errors
101    /// Errors if the allocation fails.
102    ///
103    /// # Examples
104    /// ```
105    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
106    /// let bump: Bump = Bump::try_new()?;
107    ///
108    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
109    /// vec.try_resize_zeroed(5)?;
110    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
111    ///
112    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
113    /// vec.try_resize_zeroed(2)?;
114    /// assert_eq!(vec, [1, 2]);
115    /// # Ok::<(), bump_scope::alloc::AllocError>(())
116    /// ```
117    fn try_resize_zeroed(&mut self, new_len: usize) -> Result<(), AllocError>
118    where
119        Self::T: Zeroable;
120}
121
122impl<T> VecExt for FixedBumpVec<'_, T> {
123    type T = T;
124
125    /// Extends this vector by pushing `additional` new items onto the end.
126    /// The new items are initialized with zeroes.
127    ///
128    /// # Panics
129    /// Panics if the vector does not have enough capacity.
130    ///
131    /// # Examples
132    /// ```
133    /// use bump_scope::{Bump, FixedBumpVec, bytemuck::VecExt};
134    /// let bump: Bump = Bump::new();
135    ///
136    /// let mut vec = FixedBumpVec::with_capacity_in(5, &bump);
137    /// vec.extend_from_slice_copy(&[1, 2, 3]);
138    /// vec.extend_zeroed(2);
139    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
140    /// ```
141    #[inline(always)]
142    #[cfg(feature = "panic-on-alloc")]
143    fn extend_zeroed(&mut self, additional: usize)
144    where
145        Self::T: Zeroable,
146    {
147        panic_on_error(self.generic_extend_zeroed(additional));
148    }
149
150    /// Extends this vector by pushing `additional` new items onto the end.
151    /// The new items are initialized with zeroes.
152    ///
153    /// # Errors
154    /// Errors if the vector does not have enough capacity.
155    ///
156    /// # Examples
157    /// ```
158    /// use bump_scope::{Bump, FixedBumpVec, bytemuck::VecExt};
159    /// let bump: Bump = Bump::try_new()?;
160    ///
161    /// let mut vec = FixedBumpVec::try_with_capacity_in(5, &bump)?;
162    /// vec.try_extend_from_slice_copy(&[1, 2, 3])?;
163    /// vec.try_extend_zeroed(2)?;
164    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
165    /// # Ok::<(), bump_scope::alloc::AllocError>(())
166    /// ```
167    #[inline(always)]
168    fn try_extend_zeroed(&mut self, additional: usize) -> Result<(), AllocError>
169    where
170        Self::T: Zeroable,
171    {
172        self.generic_extend_zeroed(additional)
173    }
174
175    /// Resizes this vector in-place so that `len` is equal to `new_len`.
176    ///
177    /// If `new_len` is greater than `len`, the vector is extended by the
178    /// difference, with each additional slot filled with `value`.
179    /// If `new_len` is less than `len`, the vector is simply truncated.
180    ///
181    /// # Panics
182    /// Panics if the vector does not have enough capacity.
183    ///
184    /// # Examples
185    /// ```
186    /// use bump_scope::{Bump, FixedBumpVec, bytemuck::VecExt};
187    /// let bump: Bump = Bump::new();
188    ///
189    /// let mut vec = FixedBumpVec::with_capacity_in(5, &bump);
190    /// vec.extend_from_slice_copy(&[1, 2, 3]);
191    /// vec.resize_zeroed(5);
192    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
193    ///
194    /// let mut vec = FixedBumpVec::with_capacity_in(5, &bump);
195    /// vec.extend_from_slice_copy(&[1, 2, 3]);
196    /// vec.resize_zeroed(2);
197    /// assert_eq!(vec, [1, 2]);
198    /// ```
199    #[inline(always)]
200    #[cfg(feature = "panic-on-alloc")]
201    fn resize_zeroed(&mut self, new_len: usize)
202    where
203        T: Zeroable,
204    {
205        panic_on_error(self.generic_resize_zeroed(new_len));
206    }
207
208    /// Resizes this vector in-place so that `len` is equal to `new_len`.
209    ///
210    /// If `new_len` is greater than `len`, the vector is extended by the
211    /// difference, with each additional slot filled with `value`.
212    /// If `new_len` is less than `len`, the vector is simply truncated.
213    ///
214    /// # Errors
215    /// Errors if the vector does not have enough capacity.
216    ///
217    /// # Examples
218    /// ```
219    /// use bump_scope::{Bump, FixedBumpVec, bytemuck::VecExt};
220    /// let bump: Bump = Bump::try_new()?;
221    ///
222    /// let mut vec = FixedBumpVec::try_with_capacity_in(5, &bump)?;
223    /// vec.try_extend_from_slice_copy(&[1, 2, 3])?;
224    /// vec.try_resize_zeroed(5)?;
225    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
226    ///
227    /// let mut vec = FixedBumpVec::try_with_capacity_in(5, &bump)?;
228    /// vec.try_extend_from_slice_copy(&[1, 2, 3])?;
229    /// vec.try_resize_zeroed(2)?;
230    /// assert_eq!(vec, [1, 2]);
231    /// # Ok::<(), bump_scope::alloc::AllocError>(())
232    /// ```
233    #[inline(always)]
234    fn try_resize_zeroed(&mut self, new_len: usize) -> Result<(), AllocError>
235    where
236        Self::T: Zeroable,
237    {
238        self.generic_resize_zeroed(new_len)
239    }
240}
241
242impl<T, A: BumpAllocatorExt> VecExt for BumpVec<T, A> {
243    type T = T;
244
245    /// Extends this vector by pushing `additional` new items onto the end.
246    /// The new items are initialized with zeroes.
247    ///
248    /// # Panics
249    /// Panics if the allocation fails.
250    ///
251    /// # Examples
252    /// ```
253    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
254    /// let bump: Bump = Bump::new();
255    ///
256    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
257    /// vec.extend_zeroed(2);
258    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
259    /// ```
260    #[inline(always)]
261    #[cfg(feature = "panic-on-alloc")]
262    fn extend_zeroed(&mut self, additional: usize)
263    where
264        Self::T: Zeroable,
265    {
266        panic_on_error(self.generic_extend_zeroed(additional));
267    }
268
269    /// Extends this vector by pushing `additional` new items onto the end.
270    /// The new items are initialized with zeroes.
271    ///
272    /// # Errors
273    /// Errors if the allocation fails.
274    ///
275    /// # Examples
276    /// ```
277    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
278    /// let bump: Bump = Bump::try_new()?;
279    ///
280    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
281    /// vec.try_extend_zeroed(2)?;
282    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
283    /// # Ok::<(), bump_scope::alloc::AllocError>(())
284    /// ```
285    #[inline(always)]
286    fn try_extend_zeroed(&mut self, additional: usize) -> Result<(), AllocError>
287    where
288        Self::T: Zeroable,
289    {
290        self.generic_extend_zeroed(additional)
291    }
292
293    /// Resizes this vector in-place so that `len` is equal to `new_len`.
294    ///
295    /// If `new_len` is greater than `len`, the vector is extended by the
296    /// difference, with each additional slot filled with `value`.
297    /// If `new_len` is less than `len`, the vector is simply truncated.
298    ///
299    /// # Panics
300    /// Panics if the allocation fails.
301    ///
302    /// # Examples
303    /// ```
304    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
305    /// let bump: Bump = Bump::new();
306    ///
307    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
308    /// vec.resize_zeroed(5);
309    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
310    ///
311    /// let mut vec = bump_vec![in &bump; 1, 2, 3];
312    /// vec.resize_zeroed(2);
313    /// assert_eq!(vec, [1, 2]);
314    /// ```
315    #[inline(always)]
316    #[cfg(feature = "panic-on-alloc")]
317    fn resize_zeroed(&mut self, new_len: usize)
318    where
319        Self::T: Zeroable,
320    {
321        panic_on_error(self.generic_resize_zeroed(new_len));
322    }
323
324    /// Resizes this vector in-place so that `len` is equal to `new_len`.
325    ///
326    /// If `new_len` is greater than `len`, the vector is extended by the
327    /// difference, with each additional slot filled with `value`.
328    /// If `new_len` is less than `len`, the vector is simply truncated.
329    ///
330    /// # Errors
331    /// Errors if the allocation fails.
332    ///
333    /// # Examples
334    /// ```
335    /// use bump_scope::{Bump, bump_vec, bytemuck::VecExt};
336    /// let bump: Bump = Bump::try_new()?;
337    ///
338    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
339    /// vec.try_resize_zeroed(5)?;
340    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
341    ///
342    /// let mut vec = bump_vec![try in &bump; 1, 2, 3]?;
343    /// vec.try_resize_zeroed(2)?;
344    /// assert_eq!(vec, [1, 2]);
345    /// # Ok::<(), bump_scope::alloc::AllocError>(())
346    /// ```
347    #[inline(always)]
348    fn try_resize_zeroed(&mut self, new_len: usize) -> Result<(), AllocError>
349    where
350        T: Zeroable,
351    {
352        self.generic_resize_zeroed(new_len)
353    }
354}
355
356impl<T, A: MutBumpAllocatorExt> VecExt for MutBumpVec<T, A> {
357    type T = T;
358
359    /// Extends this vector by pushing `additional` new items onto the end.
360    /// The new items are initialized with zeroes.
361    ///
362    /// # Panics
363    /// Panics if the allocation fails.
364    ///
365    /// # Examples
366    /// ```
367    /// use bump_scope::{Bump, mut_bump_vec, bytemuck::VecExt};
368    /// let mut bump: Bump = Bump::new();
369    ///
370    /// let mut vec = mut_bump_vec![in &mut bump; 1, 2, 3];
371    /// vec.extend_zeroed(2);
372    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
373    /// ```
374    #[inline(always)]
375    #[cfg(feature = "panic-on-alloc")]
376    fn extend_zeroed(&mut self, additional: usize)
377    where
378        Self::T: Zeroable,
379    {
380        panic_on_error(self.generic_extend_zeroed(additional));
381    }
382
383    /// Extends this vector by pushing `additional` new items onto the end.
384    /// The new items are initialized with zeroes.
385    ///
386    /// # Errors
387    /// Errors if the allocation fails.
388    ///
389    /// # Examples
390    /// ```
391    /// use bump_scope::{Bump, mut_bump_vec, bytemuck::VecExt};
392    /// let mut bump: Bump = Bump::try_new()?;
393    ///
394    /// let mut vec = mut_bump_vec![try in &mut bump; 1, 2, 3]?;
395    /// vec.try_extend_zeroed(2)?;
396    /// assert_eq!(vec, [1, 2, 3, 0, 0]);
397    /// # Ok::<(), bump_scope::alloc::AllocError>(())
398    /// ```
399    #[inline(always)]
400    fn try_extend_zeroed(&mut self, additional: usize) -> Result<(), AllocError>
401    where
402        Self::T: Zeroable,
403    {
404        self.generic_extend_zeroed(additional)
405    }
406
407    /// Resizes this vector in-place so that `len` is equal to `new_len`.
408    ///
409    /// If `new_len` is greater than `len`, the vector is extended by the
410    /// difference, with each additional slot filled with `value`.
411    /// If `new_len` is less than `len`, the vector is simply truncated.
412    ///
413    /// # Panics
414    /// Panics if the allocation fails.
415    ///
416    /// # Examples
417    /// ```
418    /// use bump_scope::{Bump, mut_bump_vec, bytemuck::VecExt};
419    /// let mut bump: Bump = Bump::new();
420    ///
421    /// {
422    ///     let mut vec = mut_bump_vec![in &mut bump; 1, 2, 3];
423    ///     vec.resize_zeroed(5);
424    ///     assert_eq!(vec, [1, 2, 3, 0, 0]);
425    /// }
426    ///
427    /// {
428    ///    let mut vec = mut_bump_vec![in &mut bump; 1, 2, 3];
429    ///    vec.resize_zeroed(2);
430    ///    assert_eq!(vec, [1, 2]);
431    /// }
432    /// ```
433    #[inline(always)]
434    #[cfg(feature = "panic-on-alloc")]
435    fn resize_zeroed(&mut self, new_len: usize)
436    where
437        Self::T: Zeroable,
438    {
439        panic_on_error(self.generic_resize_zeroed(new_len));
440    }
441
442    /// Resizes this vector in-place so that `len` is equal to `new_len`.
443    ///
444    /// If `new_len` is greater than `len`, the vector is extended by the
445    /// difference, with each additional slot filled with `value`.
446    /// If `new_len` is less than `len`, the vector is simply truncated.
447    ///
448    /// # Errors
449    /// Errors if the allocation fails.
450    ///
451    /// # Examples
452    /// ```
453    /// use bump_scope::{Bump, mut_bump_vec, bytemuck::VecExt};
454    /// let mut bump: Bump = Bump::try_new()?;
455    ///
456    /// {
457    ///     let mut vec = mut_bump_vec![try in &mut bump; 1, 2, 3]?;
458    ///     vec.try_resize_zeroed(5)?;
459    ///     assert_eq!(vec, [1, 2, 3, 0, 0]);
460    /// }
461    ///
462    /// {
463    ///    let mut vec = mut_bump_vec![try in &mut bump; 1, 2, 3]?;
464    ///    vec.try_resize_zeroed(2)?;
465    ///    assert_eq!(vec, [1, 2]);
466    /// }
467    /// # Ok::<(), bump_scope::alloc::AllocError>(())
468    /// ```
469    #[inline(always)]
470    fn try_resize_zeroed(&mut self, new_len: usize) -> Result<(), AllocError>
471    where
472        Self::T: Zeroable,
473    {
474        self.generic_resize_zeroed(new_len)
475    }
476}
477
478impl<T, A: MutBumpAllocatorExt> VecExt for MutBumpVecRev<T, A> {
479    type T = T;
480
481    /// Extends this vector by pushing `additional` new items onto the end.
482    /// The new items are initialized with zeroes.
483    ///
484    /// # Panics
485    /// Panics if the allocation fails.
486    ///
487    /// # Examples
488    /// ```
489    /// use bump_scope::{Bump, mut_bump_vec_rev, bytemuck::VecExt};
490    /// let mut bump: Bump = Bump::new();
491    ///
492    /// let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
493    /// vec.extend_zeroed(2);
494    /// assert_eq!(vec, [0, 0, 1, 2, 3]);
495    /// ```
496    #[inline(always)]
497    #[cfg(feature = "panic-on-alloc")]
498    fn extend_zeroed(&mut self, additional: usize)
499    where
500        T: Zeroable,
501    {
502        panic_on_error(self.generic_extend_zeroed(additional));
503    }
504
505    /// Extends this vector by pushing `additional` new items onto the end.
506    /// The new items are initialized with zeroes.
507    ///
508    /// # Errors
509    /// Errors if the allocation fails.
510    ///
511    /// # Examples
512    /// ```
513    /// use bump_scope::{Bump, mut_bump_vec_rev, bytemuck::VecExt};
514    /// let mut bump: Bump = Bump::try_new()?;
515    ///
516    /// let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3]?;
517    /// vec.try_extend_zeroed(2)?;
518    /// assert_eq!(vec, [0, 0, 1, 2, 3]);
519    /// # Ok::<(), bump_scope::alloc::AllocError>(())
520    /// ```
521    #[inline(always)]
522    fn try_extend_zeroed(&mut self, additional: usize) -> Result<(), AllocError>
523    where
524        T: Zeroable,
525    {
526        self.generic_extend_zeroed(additional)
527    }
528
529    /// Resizes this vector in-place so that `len` is equal to `new_len`.
530    ///
531    /// If `new_len` is greater than `len`, the vector is extended by the
532    /// difference, with each additional slot filled with `value`.
533    /// If `new_len` is less than `len`, the vector is simply truncated.
534    ///
535    /// # Panics
536    /// Panics if the allocation fails.
537    ///
538    /// # Examples
539    /// ```
540    /// use bump_scope::{Bump, mut_bump_vec_rev, bytemuck::VecExt};
541    /// let mut bump: Bump = Bump::new();
542    ///
543    /// {
544    ///     let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
545    ///     vec.resize_zeroed(5);
546    ///     assert_eq!(vec, [0, 0, 1, 2, 3]);
547    /// }
548    ///
549    /// {
550    ///     let mut vec = mut_bump_vec_rev![in &mut bump; 1, 2, 3];
551    ///     vec.resize_zeroed(2);
552    ///     assert_eq!(vec, [2, 3]);
553    /// }
554    /// ```
555    #[inline(always)]
556    #[cfg(feature = "panic-on-alloc")]
557    fn resize_zeroed(&mut self, new_len: usize)
558    where
559        T: Zeroable,
560    {
561        panic_on_error(self.generic_resize_zeroed(new_len));
562    }
563
564    /// Resizes this vector in-place so that `len` is equal to `new_len`.
565    ///
566    /// If `new_len` is greater than `len`, the vector is extended by the
567    /// difference, with each additional slot filled with `value`.
568    /// If `new_len` is less than `len`, the vector is simply truncated.
569    ///
570    /// # Errors
571    /// Errors if the allocation fails.
572    ///
573    /// # Examples
574    /// ```
575    /// use bump_scope::{Bump, mut_bump_vec_rev, bytemuck::VecExt};
576    /// let mut bump: Bump = Bump::try_new()?;
577    ///
578    /// {
579    ///     let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3]?;
580    ///     vec.try_resize_zeroed(5)?;
581    ///     assert_eq!(vec, [0, 0, 1, 2, 3]);
582    /// }
583    ///
584    /// {
585    ///     let mut vec = mut_bump_vec_rev![try in &mut bump; 1, 2, 3]?;
586    ///     vec.try_resize_zeroed(2)?;
587    ///     assert_eq!(vec, [2, 3]);
588    /// }
589    /// # Ok::<(), bump_scope::alloc::AllocError>(())
590    /// ```
591    #[inline(always)]
592    fn try_resize_zeroed(&mut self, new_len: usize) -> Result<(), AllocError>
593    where
594        T: Zeroable,
595    {
596        self.generic_resize_zeroed(new_len)
597    }
598}
599
600trait PrivateVecExt {
601    fn generic_extend_zeroed<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E>;
602    fn generic_resize_zeroed<E: ErrorBehavior>(&mut self, new_len: usize) -> Result<(), E>;
603}
604
605impl<T: Zeroable> PrivateVecExt for FixedBumpVec<'_, T> {
606    #[inline]
607    fn generic_extend_zeroed<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
608        self.generic_reserve(additional)?;
609
610        unsafe {
611            let ptr = self.as_mut_ptr();
612            let len = self.len();
613
614            ptr.add(len).write_bytes(0, additional);
615            self.set_len(len + additional);
616        }
617
618        Ok(())
619    }
620
621    #[inline]
622    fn generic_resize_zeroed<E: ErrorBehavior>(&mut self, new_len: usize) -> Result<(), E> {
623        let len = self.len();
624
625        if new_len > len {
626            self.generic_extend_zeroed(new_len - len)
627        } else {
628            self.truncate(new_len);
629            Ok(())
630        }
631    }
632}
633
634impl<T: Zeroable, A: BumpAllocatorExt> PrivateVecExt for BumpVec<T, A> {
635    #[inline]
636    fn generic_extend_zeroed<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
637        self.generic_reserve(additional)?;
638
639        unsafe {
640            let ptr = self.as_mut_ptr();
641            let len = self.len();
642
643            ptr.add(len).write_bytes(0, additional);
644            self.set_len(len + additional);
645        }
646
647        Ok(())
648    }
649
650    #[inline]
651    fn generic_resize_zeroed<E: ErrorBehavior>(&mut self, new_len: usize) -> Result<(), E> {
652        let len = self.len();
653
654        if new_len > len {
655            self.generic_extend_zeroed(new_len - len)
656        } else {
657            self.truncate(new_len);
658            Ok(())
659        }
660    }
661}
662
663impl<T: Zeroable, A: MutBumpAllocatorExt> PrivateVecExt for MutBumpVec<T, A> {
664    #[inline]
665    fn generic_extend_zeroed<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E>
666    where
667        T: Zeroable,
668    {
669        self.generic_reserve(additional)?;
670
671        unsafe {
672            let ptr = self.as_mut_ptr();
673            let len = self.len();
674
675            ptr.add(len).write_bytes(0, additional);
676            self.set_len(len + additional);
677        }
678
679        Ok(())
680    }
681
682    #[inline]
683    fn generic_resize_zeroed<E: ErrorBehavior>(&mut self, new_len: usize) -> Result<(), E>
684    where
685        T: Zeroable,
686    {
687        let len = self.len();
688
689        if new_len > len {
690            self.generic_extend_zeroed(new_len - len)
691        } else {
692            self.truncate(new_len);
693            Ok(())
694        }
695    }
696}
697
698impl<T: Zeroable, A: MutBumpAllocatorExt> PrivateVecExt for MutBumpVecRev<T, A> {
699    #[inline]
700    fn generic_extend_zeroed<E: ErrorBehavior>(&mut self, additional: usize) -> Result<(), E> {
701        self.generic_reserve(additional)?;
702
703        unsafe {
704            let new_len = self.len() + additional;
705            self.end.sub(new_len).as_ptr().write_bytes(0, additional);
706            self.set_len(new_len);
707        }
708
709        Ok(())
710    }
711
712    #[inline]
713    fn generic_resize_zeroed<E: ErrorBehavior>(&mut self, new_len: usize) -> Result<(), E> {
714        let len = self.len();
715
716        if new_len > len {
717            self.generic_extend_zeroed(new_len - len)
718        } else {
719            self.truncate(new_len);
720            Ok(())
721        }
722    }
723}