Skip to content

Commit 301d090

Browse files
Renamed String::from_utf8_lossy_coop_or_not(..) to from_utf8_lossy_co(..). Added Vec::new_co()
1 parent aa30184 commit 301d090

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

library/alloc/src/ffi/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ impl CStr {
11261126
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
11271127
{
11281128
// false = no need for co-alloc metadata, since it would get lost once converted to the slice.
1129-
String::<COOP_PREFERRED>::from_utf8_lossy_coop_or_not(self.to_bytes())
1129+
String::<COOP_PREFERRED>::from_utf8_lossy_co(self.to_bytes())
11301130
}
11311131

11321132
/// Converts a <code>[Box]<[CStr]></code> into a [`CString`] without copying or allocating.

library/alloc/src/macros.rs

+1-1
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/string.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ where
468468
#[stable(feature = "rust1", since = "1.0.0")]
469469
#[must_use]
470470
pub const fn new() -> String<COOP_PREFERRED> {
471-
String { vec: Vec::new() }
471+
String { vec: Vec::new_co() }
472472
}
473473

474474
/// Creates a new empty `String` with at least the specified capacity.
@@ -595,8 +595,8 @@ where
595595
}
596596

597597
/// Like `from_utf8_lossy`, but this honors COOP_PREFERRED.
598-
#[unstable(feature = "compile_check_struct_with_const_generic_added_associated_function", reason = "confirm_or_fix_the_function_name", issue = "none")]
599-
pub fn from_utf8_lossy_coop_or_not(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> {
598+
#[unstable(feature = "string_from_utf8_lossy_co", reason = "confirm_or_fix_the_function_name", issue = "none")]
599+
pub fn from_utf8_lossy_co(v: &[u8]) -> Cow<'_, str, COOP_PREFERRED> {
600600
let mut iter = Utf8Chunks::new(v);
601601

602602
let first_valid = if let Some(chunk) = iter.next() {
@@ -686,7 +686,7 @@ impl String {
686686
#[stable(feature = "rust1", since = "1.0.0")]
687687
#[allow(unused_braces)]
688688
pub fn from_utf8_lossy(v: &[u8]) -> Cow<'_, str, {DEFAULT_COOP_PREFERRED!()}> {
689-
String::<{DEFAULT_COOP_PREFERRED!()}>::from_utf8_lossy_coop_or_not(v)
689+
String::<{DEFAULT_COOP_PREFERRED!()}>::from_utf8_lossy_co(v)
690690
}
691691
}
692692

library/alloc/src/vec/mod.rs

+21-9
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

+1-1
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

+1-1
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)