@@ -444,6 +444,10 @@ pub type DefVec<T, A = Global> = Vec<T, A, { DEFAULT_COOP_PREFERRED!() }>;
444
444
pub type WeVec < T , const WEIGHT : u8 > = Vec < T , Global , { WEIGHT > 127 } > ;
445
445
446
446
impl < T > Vec < T > {
447
+ /*impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED>
448
+ where
449
+ [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
450
+ {*/
447
451
/// Constructs a new, empty `Vec<T>`.
448
452
///
449
453
/// The vector will not allocate until elements are pushed onto it.
@@ -461,13 +465,73 @@ impl<T> Vec<T> {
461
465
pub const fn new ( ) -> Self {
462
466
#[ allow( unused_braces) ]
463
467
Vec :: < T , Global , { DEFAULT_COOP_PREFERRED ! ( ) } > :: new_co ( )
468
+ //Self::new_co()
469
+ }
470
+
471
+ /// Constructs a new, empty `Vec<T>` with at least the specified capacity.
472
+ ///
473
+ /// The vector will be able to hold at least `capacity` elements without
474
+ /// reallocating. This method is allowed to allocate for more elements than
475
+ /// `capacity`. If `capacity` is 0, the vector will not allocate.
476
+ ///
477
+ /// It is important to note that although the returned vector has the
478
+ /// minimum *capacity* specified, the vector will have a zero *length*. For
479
+ /// an explanation of the difference between length and capacity, see
480
+ /// *[Capacity and reallocation]*.
481
+ ///
482
+ /// If it is important to know the exact allocated capacity of a `Vec`,
483
+ /// always use the [`capacity`] method after construction.
484
+ ///
485
+ /// For `Vec<T>` where `T` is a zero-sized type, there will be no allocation
486
+ /// and the capacity will always be `usize::MAX`.
487
+ ///
488
+ /// [Capacity and reallocation]: #capacity-and-reallocation
489
+ /// [`capacity`]: Vec::capacity
490
+ ///
491
+ /// # Panics
492
+ ///
493
+ /// Panics if the new capacity exceeds `isize::MAX` bytes.
494
+ ///
495
+ /// # Examples
496
+ ///
497
+ /// ```
498
+ /// let mut vec = Vec::with_capacity(10);
499
+ ///
500
+ /// // The vector contains no items, even though it has capacity for more
501
+ /// assert_eq!(vec.len(), 0);
502
+ /// assert!(vec.capacity() >= 10);
503
+ ///
504
+ /// // These are all done without reallocating...
505
+ /// for i in 0..10 {
506
+ /// vec.push(i);
507
+ /// }
508
+ /// assert_eq!(vec.len(), 10);
509
+ /// assert!(vec.capacity() >= 10);
510
+ ///
511
+ /// // ...but this may make the vector reallocate
512
+ /// vec.push(11);
513
+ /// assert_eq!(vec.len(), 11);
514
+ /// assert!(vec.capacity() >= 11);
515
+ ///
516
+ /// // A vector of a zero-sized type will always over-allocate, since no
517
+ /// // allocation is necessary
518
+ /// let vec_units = Vec::<()>::with_capacity(10);
519
+ /// assert_eq!(vec_units.capacity(), usize::MAX);
520
+ /// ``` #[cfg(not(no_global_oom_handling))]
521
+ #[ inline]
522
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
523
+ #[ must_use]
524
+ pub fn with_capacity ( capacity : usize ) -> Self {
525
+ Self :: with_capacity_in ( capacity, Global )
464
526
}
527
+
465
528
}
466
529
467
530
////////////////////////////////////////////////////////////////////////////////
468
531
// Inherent methods
469
532
////////////////////////////////////////////////////////////////////////////////
470
533
534
+ /**/
471
535
impl < T , const COOP_PREFERRED : bool > Vec < T , Global , COOP_PREFERRED >
472
536
where
473
537
[ ( ) ; core:: alloc:: co_alloc_metadata_num_slots_with_preference :: < Global > ( COOP_PREFERRED ) ] : ,
@@ -481,6 +545,7 @@ where
481
545
Vec { buf : RawVec :: NEW , len : 0 }
482
546
}
483
547
548
+ // TODO @FIXME document co-allocation
484
549
/// Constructs a new, empty `Vec<T>` with at least the specified capacity.
485
550
///
486
551
/// The vector will be able to hold at least `capacity` elements without
@@ -533,9 +598,9 @@ where
533
598
/// ```
534
599
#[ cfg( not( no_global_oom_handling) ) ]
535
600
#[ inline]
536
- #[ stable ( feature = "rust1 " , since = "1.0.0 " ) ]
601
+ #[ unstable ( feature = "vec_new_co " , reason = "confirm_or_fix_the_function_name" , issue = "none ") ]
537
602
#[ must_use]
538
- pub fn with_capacity ( capacity : usize ) -> Self {
603
+ pub fn with_capacity_co ( capacity : usize ) -> Self {
539
604
Self :: with_capacity_in ( capacity, Global )
540
605
}
541
606
0 commit comments