Skip to content

Commit a3c765e

Browse files
CoAlloc: Vec: added new_co()
1 parent df419ce commit a3c765e

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

library/alloc/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#[allow_internal_unstable(rustc_attrs, liballoc_internals)]
4242
macro_rules! vec {
4343
() => (
44-
$crate::__rust_force_expr!($crate::vec::Vec::new())
44+
$crate::__rust_force_expr!($crate::vec::Vec::new_co())
4545
);
4646
($elem:expr; $n:expr) => (
4747
$crate::__rust_force_expr!($crate::vec::from_elem($elem, $n))

library/alloc/src/vec/mod.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,7 @@ pub type DefVec<T, A = Global> =
444444
#[unstable(feature = "global_co_alloc_vec", issue = "none")]
445445
pub type WeVec<T, const WEIGHT: u8> = Vec<T, Global, { WEIGHT > 127 }>;
446446

447-
////////////////////////////////////////////////////////////////////////////////
448-
// Inherent methods
449-
////////////////////////////////////////////////////////////////////////////////
450-
451-
impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED>
452-
where
453-
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
454-
{
447+
impl<T> Vec<T> {
455448
/// Constructs a new, empty `Vec<T>`.
456449
///
457450
/// The vector will not allocate until elements are pushed onto it.
@@ -467,6 +460,25 @@ where
467460
#[stable(feature = "rust1", since = "1.0.0")]
468461
#[must_use]
469462
pub const fn new() -> Self {
463+
#[allow(unused_braces)]
464+
Vec::<T, Global, {DEFAULT_COOP_PREFERRED!()}>::new_co()
465+
}
466+
}
467+
468+
////////////////////////////////////////////////////////////////////////////////
469+
// Inherent methods
470+
////////////////////////////////////////////////////////////////////////////////
471+
472+
impl<T, const COOP_PREFERRED: bool> Vec<T, Global, COOP_PREFERRED>
473+
where
474+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
475+
{
476+
/// Like new(), but it respects COOP_PREFERRED.
477+
#[inline]
478+
#[rustc_const_stable(feature = "const_vec_new_co", since = "1.60.0")] //@FIXME This is `rustc_const_stable`, so that String::new() can be const and can call this.
479+
#[unstable(feature = "vec_new_co", reason = "confirm_or_fix_the_function_name", issue = "none")]
480+
#[must_use]
481+
pub const fn new_co() -> Self {
470482
Vec { buf: RawVec::NEW, len: 0 }
471483
}
472484

@@ -3242,7 +3254,7 @@ where
32423254
///
32433255
/// The vector will not allocate until elements are pushed onto it.
32443256
fn default() -> Vec<T, Global, COOP_PREFERRED> {
3245-
Vec::new()
3257+
Vec::new_co()
32463258
}
32473259
}
32483260

library/alloc/src/vec/spec_from_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ where
6363
}
6464
}
6565

66-
let mut vec = Vec::<T, Global, COOP_PREFERRED>::new();
66+
let mut vec = Vec::<T, Global, COOP_PREFERRED>::new_co();
6767
// must delegate to spec_extend() since extend() itself delegates
6868
// to spec_from for empty Vecs
6969
vec.spec_extend(iterator);

library/alloc/src/vec/spec_from_iter_nested.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ where
2929
// vector being full in the few subsequent loop iterations.
3030
// So we get better branch prediction.
3131
let mut vector = match iterator.next() {
32-
None => return Vec::new(),
32+
None => return Vec::new_co(),
3333
Some(element) => {
3434
let (lower, _) = iterator.size_hint();
3535
let initial_capacity =

0 commit comments

Comments
 (0)