Skip to content

Commit bcb7aa8

Browse files
committed
liballoc/{Vec,String}: constify as_mut_* methods
1 parent bcc8544 commit bcb7aa8

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

library/alloc/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
#![feature(const_option)]
115115
#![feature(const_pin)]
116116
#![feature(const_size_of_val)]
117+
#![feature(const_slice_from_raw_parts_mut)]
118+
#![feature(const_str_from_utf8_unchecked_mut)]
117119
#![feature(const_vec_string_slice)]
118120
#![feature(core_intrinsics)]
119121
#![feature(deprecated_suggestion)]

library/alloc/src/string.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,9 @@ impl String {
10981098
#[must_use]
10991099
#[stable(feature = "string_as_str", since = "1.7.0")]
11001100
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")]
1101-
pub fn as_mut_str(&mut self) -> &mut str {
1102-
unsafe { str::from_utf8_unchecked_mut(&mut self.vec) }
1101+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1102+
pub const fn as_mut_str(&mut self) -> &mut str {
1103+
unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) }
11031104
}
11041105

11051106
/// Appends a given string slice onto the end of this `String`.
@@ -1788,7 +1789,8 @@ impl String {
17881789
/// ```
17891790
#[inline]
17901791
#[stable(feature = "rust1", since = "1.0.0")]
1791-
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
1792+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1793+
pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
17921794
&mut self.vec
17931795
}
17941796

library/alloc/src/vec/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,8 @@ impl<T, A: Allocator> Vec<T, A> {
15681568
#[inline]
15691569
#[stable(feature = "vec_as_slice", since = "1.7.0")]
15701570
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_as_mut_slice")]
1571-
pub fn as_mut_slice(&mut self) -> &mut [T] {
1571+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
1572+
pub const fn as_mut_slice(&mut self) -> &mut [T] {
15721573
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
15731574
}
15741575

@@ -1684,9 +1685,10 @@ impl<T, A: Allocator> Vec<T, A> {
16841685
/// [`as_mut_ptr`]: Vec::as_mut_ptr
16851686
/// [`as_ptr`]: Vec::as_ptr
16861687
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1688+
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")]
16871689
#[rustc_never_returns_null_ptr]
16881690
#[inline]
1689-
pub fn as_mut_ptr(&mut self) -> *mut T {
1691+
pub const fn as_mut_ptr(&mut self) -> *mut T {
16901692
// We shadow the slice method of the same name to avoid going through
16911693
// `deref_mut`, which creates an intermediate reference.
16921694
self.buf.ptr()

0 commit comments

Comments
 (0)