-
Notifications
You must be signed in to change notification settings - Fork 13.3k
liballoc: introduce String, Vec const-slicing #128399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1059,7 +1059,8 @@ impl String { | |
#[inline] | ||
#[must_use = "`self` will be dropped if the result is not used"] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn into_bytes(self) -> Vec<u8> { | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn into_bytes(self) -> Vec<u8> { | ||
self.vec | ||
} | ||
|
||
|
@@ -1076,8 +1077,11 @@ impl String { | |
#[must_use] | ||
#[stable(feature = "string_as_str", since = "1.7.0")] | ||
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_str")] | ||
pub fn as_str(&self) -> &str { | ||
self | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn as_str(&self) -> &str { | ||
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error | ||
// at construction. | ||
unsafe { str::from_utf8_unchecked(self.vec.as_slice()) } | ||
tgross35 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it correct that the That should always be noted with a comment in the code. We even have a standard form for that, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not specifically — this used to live in I did invert the dependency (moved the primary impl here rather than in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, fair -- thanks. |
||
} | ||
|
||
/// Converts a `String` into a mutable string slice. | ||
|
@@ -1096,8 +1100,11 @@ impl String { | |
#[must_use] | ||
#[stable(feature = "string_as_str", since = "1.7.0")] | ||
#[cfg_attr(not(test), rustc_diagnostic_item = "string_as_mut_str")] | ||
pub fn as_mut_str(&mut self) -> &mut str { | ||
self | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something odd happened here, the function was made There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the tracking issue and renominated for libs-api. |
||
pub const fn as_mut_str(&mut self) -> &mut str { | ||
// SAFETY: String contents are stipulated to be valid UTF-8, invalid contents are an error | ||
// at construction. | ||
unsafe { str::from_utf8_unchecked_mut(self.vec.as_mut_slice()) } | ||
tgross35 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Appends a given string slice onto the end of this `String`. | ||
|
@@ -1168,7 +1175,8 @@ impl String { | |
#[inline] | ||
#[must_use] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn capacity(&self) -> usize { | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn capacity(&self) -> usize { | ||
self.vec.capacity() | ||
} | ||
|
||
|
@@ -1431,8 +1439,9 @@ impl String { | |
#[inline] | ||
#[must_use] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn as_bytes(&self) -> &[u8] { | ||
&self.vec | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn as_bytes(&self) -> &[u8] { | ||
self.vec.as_slice() | ||
} | ||
|
||
/// Shortens this `String` to the specified length. | ||
|
@@ -1784,7 +1793,8 @@ impl String { | |
/// ``` | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> { | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> { | ||
&mut self.vec | ||
} | ||
|
||
|
@@ -1805,8 +1815,9 @@ impl String { | |
#[inline] | ||
#[must_use] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
#[rustc_confusables("length", "size")] | ||
pub fn len(&self) -> usize { | ||
pub const fn len(&self) -> usize { | ||
self.vec.len() | ||
} | ||
|
||
|
@@ -1824,7 +1835,8 @@ impl String { | |
#[inline] | ||
#[must_use] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn is_empty(&self) -> bool { | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
|
@@ -2565,7 +2577,7 @@ impl ops::Deref for String { | |
|
||
#[inline] | ||
fn deref(&self) -> &str { | ||
unsafe { str::from_utf8_unchecked(&self.vec) } | ||
self.as_str() | ||
} | ||
} | ||
|
||
|
@@ -2576,7 +2588,7 @@ unsafe impl ops::DerefPure for String {} | |
impl ops::DerefMut for String { | ||
#[inline] | ||
fn deref_mut(&mut self) -> &mut str { | ||
unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) } | ||
self.as_mut_str() | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mark
fn non_null
const
, just replace.into()
with.as_non_null_ptr()
(source). Thenfn ptr
can be markedconst
without changing its implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, not sure why GH put this comment on an empty line but it's meant to go with the
ptr
andnon_null
functions.Anyway, this change plus the safety comments Oli requested and we can merge this.