@@ -511,6 +511,74 @@ impl<T> Vec<T> {
511
511
// Inherent methods
512
512
////////////////////////////////////////////////////////////////////////////////
513
513
514
+ #[ allow( unused_braces) ]
515
+ impl < T , A : Allocator > Vec < T , A >
516
+ where
517
+ [ ( ) ; { meta_num_slots_default ! ( A ) } ] : ,
518
+ {
519
+ /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
520
+ /// with the provided allocator.
521
+ ///
522
+ /// The vector will be able to hold at least `capacity` elements without
523
+ /// reallocating. This method is allowed to allocate for more elements than
524
+ /// `capacity`. If `capacity` is 0, the vector will not allocate.
525
+ ///
526
+ /// It is important to note that although the returned vector has the
527
+ /// minimum *capacity* specified, the vector will have a zero *length*. For
528
+ /// an explanation of the difference between length and capacity, see
529
+ /// *[Capacity and reallocation]*.
530
+ ///
531
+ /// If it is important to know the exact allocated capacity of a `Vec`,
532
+ /// always use the [`capacity`] method after construction.
533
+ ///
534
+ /// For `Vec<T, A>` where `T` is a zero-sized type, there will be no allocation
535
+ /// and the capacity will always be `usize::MAX`.
536
+ ///
537
+ /// [Capacity and reallocation]: #capacity-and-reallocation
538
+ /// [`capacity`]: Vec::capacity
539
+ ///
540
+ /// # Panics
541
+ ///
542
+ /// Panics if the new capacity exceeds `isize::MAX` bytes.
543
+ ///
544
+ /// # Examples
545
+ ///
546
+ /// ```
547
+ /// #![feature(allocator_api)]
548
+ ///
549
+ /// use std::alloc::System;
550
+ ///
551
+ /// let mut vec = Vec::with_capacity_in(10, System);
552
+ ///
553
+ /// // The vector contains no items, even though it has capacity for more
554
+ /// assert_eq!(vec.len(), 0);
555
+ /// assert_eq!(vec.capacity(), 10);
556
+ ///
557
+ /// // These are all done without reallocating...
558
+ /// for i in 0..10 {
559
+ /// vec.push(i);
560
+ /// }
561
+ /// assert_eq!(vec.len(), 10);
562
+ /// assert_eq!(vec.capacity(), 10);
563
+ ///
564
+ /// // ...but this may make the vector reallocate
565
+ /// vec.push(11);
566
+ /// assert_eq!(vec.len(), 11);
567
+ /// assert!(vec.capacity() >= 11);
568
+ ///
569
+ /// // A vector of a zero-sized type will always over-allocate, since no
570
+ /// // allocation is necessary
571
+ /// let vec_units = Vec::<(), System>::with_capacity_in(10, System);
572
+ /// assert_eq!(vec_units.capacity(), usize::MAX);
573
+ /// ```
574
+ #[ cfg( not( no_global_oom_handling) ) ]
575
+ #[ inline]
576
+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
577
+ pub fn with_capacity_in ( capacity : usize , alloc : A ) -> Self {
578
+ Self :: with_capacity_in_co ( capacity, alloc)
579
+ }
580
+ }
581
+
514
582
/**/
515
583
#[ allow( unused_braces) ]
516
584
impl < T , const CO_ALLOC_PREF : CoAllocPref > Vec < T , Global , CO_ALLOC_PREF >
@@ -582,7 +650,7 @@ where
582
650
#[ unstable( feature = "vec_new_co" , reason = "confirm_or_fix_the_function_name" , issue = "none" ) ]
583
651
#[ must_use]
584
652
pub fn with_capacity_co ( capacity : usize ) -> Self {
585
- Self :: with_capacity_in ( capacity, Global )
653
+ Self :: with_capacity_in_co ( capacity, Global )
586
654
}
587
655
588
656
/// Coallocation-aware alternative to `from_row_parts`.
@@ -729,65 +797,11 @@ where
729
797
Vec { buf : RawVec :: new_in ( alloc) , len : 0 }
730
798
}
731
799
732
- /// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
733
- /// with the provided allocator.
734
- ///
735
- /// The vector will be able to hold at least `capacity` elements without
736
- /// reallocating. This method is allowed to allocate for more elements than
737
- /// `capacity`. If `capacity` is 0, the vector will not allocate.
738
- ///
739
- /// It is important to note that although the returned vector has the
740
- /// minimum *capacity* specified, the vector will have a zero *length*. For
741
- /// an explanation of the difference between length and capacity, see
742
- /// *[Capacity and reallocation]*.
743
- ///
744
- /// If it is important to know the exact allocated capacity of a `Vec`,
745
- /// always use the [`capacity`] method after construction.
746
- ///
747
- /// For `Vec<T, A>` where `T` is a zero-sized type, there will be no allocation
748
- /// and the capacity will always be `usize::MAX`.
749
- ///
750
- /// [Capacity and reallocation]: #capacity-and-reallocation
751
- /// [`capacity`]: Vec::capacity
752
- ///
753
- /// # Panics
754
- ///
755
- /// Panics if the new capacity exceeds `isize::MAX` bytes.
756
- ///
757
- /// # Examples
758
- ///
759
- /// ```
760
- /// #![feature(allocator_api)]
761
- ///
762
- /// use std::alloc::System;
763
- ///
764
- /// let mut vec = Vec::with_capacity_in(10, System);
765
- ///
766
- /// // The vector contains no items, even though it has capacity for more
767
- /// assert_eq!(vec.len(), 0);
768
- /// assert_eq!(vec.capacity(), 10);
769
- ///
770
- /// // These are all done without reallocating...
771
- /// for i in 0..10 {
772
- /// vec.push(i);
773
- /// }
774
- /// assert_eq!(vec.len(), 10);
775
- /// assert_eq!(vec.capacity(), 10);
776
- ///
777
- /// // ...but this may make the vector reallocate
778
- /// vec.push(11);
779
- /// assert_eq!(vec.len(), 11);
780
- /// assert!(vec.capacity() >= 11);
781
- ///
782
- /// // A vector of a zero-sized type will always over-allocate, since no
783
- /// // allocation is necessary
784
- /// let vec_units = Vec::<(), System>::with_capacity_in(10, System);
785
- /// assert_eq!(vec_units.capacity(), usize::MAX);
786
- /// ```
800
+ /** Like `with_capacity_in`, but co-allocation-aware. */
787
801
#[ cfg( not( no_global_oom_handling) ) ]
788
802
#[ inline]
789
- #[ unstable( feature = "allocator_api " , issue = "32838 " ) ]
790
- pub fn with_capacity_in ( capacity : usize , alloc : A ) -> Self {
803
+ #[ unstable( feature = "global_co_alloc " , issue = "none " ) ]
804
+ pub fn with_capacity_in_co ( capacity : usize , alloc : A ) -> Self {
791
805
Vec { buf : RawVec :: with_capacity_in ( capacity, alloc) , len : 0 }
792
806
}
793
807
@@ -2260,12 +2274,12 @@ where
2260
2274
// the new vector can take over the original buffer and avoid the copy
2261
2275
return mem:: replace (
2262
2276
self ,
2263
- Vec :: with_capacity_in ( self . capacity ( ) , self . allocator ( ) . clone ( ) ) ,
2277
+ Vec :: with_capacity_in_co ( self . capacity ( ) , self . allocator ( ) . clone ( ) ) ,
2264
2278
) ;
2265
2279
}
2266
2280
2267
2281
let other_len = self . len - at;
2268
- let mut other = Vec :: with_capacity_in ( other_len, self . allocator ( ) . clone ( ) ) ;
2282
+ let mut other = Vec :: with_capacity_in_co ( other_len, self . allocator ( ) . clone ( ) ) ;
2269
2283
2270
2284
// Unsafely `set_len` and copy items to `other`.
2271
2285
unsafe {
0 commit comments