@@ -124,6 +124,23 @@ impl BlobVec {
124
124
}
125
125
}
126
126
127
+ /// Reserves the minimum capacity for at least `additional` more elements to be inserted in the given `BlobVec`.
128
+ #[ inline]
129
+ pub fn reserve ( & mut self , additional : usize ) {
130
+ /// Similar to `reserve_exact`. This method ensures that the capacity will grow at least `self.capacity()` if there is no
131
+ /// enough space to hold `additional` more elements.
132
+ #[ cold]
133
+ fn do_reserve ( slf : & mut BlobVec , additional : usize ) {
134
+ let increment = slf. capacity . max ( additional - ( slf. capacity - slf. len ) ) ;
135
+ let increment = NonZeroUsize :: new ( increment) . unwrap ( ) ;
136
+ slf. grow_exact ( increment) ;
137
+ }
138
+
139
+ if self . capacity - self . len < additional {
140
+ do_reserve ( self , additional) ;
141
+ }
142
+ }
143
+
127
144
/// Grows the capacity by `increment` elements.
128
145
///
129
146
/// # Panics
@@ -241,7 +258,7 @@ impl BlobVec {
241
258
/// The `value` must match the [`layout`](`BlobVec::layout`) of the elements in the [`BlobVec`].
242
259
#[ inline]
243
260
pub unsafe fn push ( & mut self , value : OwningPtr < ' _ > ) {
244
- self . reserve_exact ( 1 ) ;
261
+ self . reserve ( 1 ) ;
245
262
let index = self . len ;
246
263
self . len += 1 ;
247
264
self . initialize_unchecked ( index, value) ;
@@ -530,7 +547,7 @@ mod tests {
530
547
}
531
548
532
549
assert_eq ! ( blob_vec. len( ) , 1_000 ) ;
533
- assert_eq ! ( blob_vec. capacity( ) , 1_000 ) ;
550
+ assert_eq ! ( blob_vec. capacity( ) , 1_024 ) ;
534
551
}
535
552
536
553
#[ derive( Debug , Eq , PartialEq , Clone ) ]
@@ -590,19 +607,19 @@ mod tests {
590
607
591
608
push ( & mut blob_vec, foo3. clone ( ) ) ;
592
609
assert_eq ! ( blob_vec. len( ) , 3 ) ;
593
- assert_eq ! ( blob_vec. capacity( ) , 3 ) ;
610
+ assert_eq ! ( blob_vec. capacity( ) , 4 ) ;
594
611
595
612
let last_index = blob_vec. len ( ) - 1 ;
596
613
let value = swap_remove :: < Foo > ( & mut blob_vec, last_index) ;
597
614
assert_eq ! ( foo3, value) ;
598
615
599
616
assert_eq ! ( blob_vec. len( ) , 2 ) ;
600
- assert_eq ! ( blob_vec. capacity( ) , 3 ) ;
617
+ assert_eq ! ( blob_vec. capacity( ) , 4 ) ;
601
618
602
619
let value = swap_remove :: < Foo > ( & mut blob_vec, 0 ) ;
603
620
assert_eq ! ( foo1, value) ;
604
621
assert_eq ! ( blob_vec. len( ) , 1 ) ;
605
- assert_eq ! ( blob_vec. capacity( ) , 3 ) ;
622
+ assert_eq ! ( blob_vec. capacity( ) , 4 ) ;
606
623
607
624
foo2. a = 8 ;
608
625
assert_eq ! ( get_mut:: <Foo >( & mut blob_vec, 0 ) , & foo2) ;
0 commit comments