-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add inherent constructors on str
#131118
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
Add inherent constructors on str
#131118
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
//! Ways to create a `str` from bytes slice. | ||
|
||
use super::Utf8Error; | ||
use super::validations::run_utf8_validation; | ||
use crate::{mem, ptr}; | ||
use crate::ptr; | ||
|
||
/// Converts a slice of bytes to a string slice. | ||
/// | ||
|
@@ -85,14 +84,7 @@ use crate::{mem, ptr}; | |
#[rustc_const_stable(feature = "const_str_from_utf8_shared", since = "1.63.0")] | ||
#[rustc_diagnostic_item = "str_from_utf8"] | ||
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. Don't move the existing diagnostic items. This feature will be unstable for a while, we don't want to break everything that relies on these in the meantime. 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. restored |
||
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> { | ||
// FIXME(const-hack): This should use `?` again, once it's `const` | ||
match run_utf8_validation(v) { | ||
Ok(_) => { | ||
// SAFETY: validation succeeded. | ||
Ok(unsafe { from_utf8_unchecked(v) }) | ||
} | ||
Err(err) => Err(err), | ||
} | ||
str::from_utf8(v) | ||
} | ||
|
||
/// Converts a mutable slice of bytes to a mutable string slice. | ||
|
@@ -129,14 +121,7 @@ pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> { | |
#[rustc_const_unstable(feature = "const_str_from_utf8", issue = "91006")] | ||
#[rustc_diagnostic_item = "str_from_utf8_mut"] | ||
pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> { | ||
// FIXME(const-hack): This should use `?` again, once it's `const` | ||
match run_utf8_validation(v) { | ||
Ok(_) => { | ||
// SAFETY: validation succeeded. | ||
Ok(unsafe { from_utf8_unchecked_mut(v) }) | ||
} | ||
Err(err) => Err(err), | ||
} | ||
str::from_utf8_mut(v) | ||
} | ||
|
||
/// Converts a slice of bytes to a string slice without checking | ||
|
@@ -168,11 +153,10 @@ pub const fn from_utf8_mut(v: &mut [u8]) -> Result<&mut str, Utf8Error> { | |
#[must_use] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")] | ||
#[rustc_diagnostic_item = "str_from_utf8_unchecked"] | ||
#[rustc_diagnostic_item = "from_utf8_unchecked"] | ||
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str { | ||
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8. | ||
// Also relies on `&str` and `&[u8]` having the same layout. | ||
unsafe { mem::transmute(v) } | ||
// SAFETY: same requirements | ||
unsafe { str::from_utf8_unchecked(v) } | ||
} | ||
|
||
/// Converts a slice of bytes to a string slice without checking | ||
|
@@ -196,13 +180,10 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str { | |
#[must_use] | ||
#[stable(feature = "str_mut_extras", since = "1.20.0")] | ||
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked_mut", since = "1.83.0")] | ||
#[rustc_diagnostic_item = "str_from_utf8_unchecked_mut"] | ||
#[rustc_diagnostic_item = "from_utf8_unchecked_mut"] | ||
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str { | ||
// SAFETY: the caller must guarantee that the bytes `v` | ||
// are valid UTF-8, thus the cast to `*mut str` is safe. | ||
// Also, the pointer dereference is safe because that pointer | ||
// comes from a reference which is guaranteed to be valid for writes. | ||
unsafe { &mut *(v as *mut [u8] as *mut str) } | ||
// SAFETY: same requirements | ||
unsafe { str::from_utf8_unchecked_mut(v) } | ||
} | ||
|
||
/// Creates a `&str` from a pointer and a length. | ||
|
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.
There are a lot of places in this PR that change from
std:str::*
to the new inherent methods. Is there a reason for this? It would be much cleaner to add the inherent methods now, but don't replace any existing uses until a follow up PR (and once the feature is closer to stable).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.
because I've tried to future-deprecation-warning them, so I already cleaned up all usages
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.
Don't do that in the same PR. Add the unstable API first, we can consider encouraging the newer APIs somehow once this is closer to stabilization - but certainly not while it is unstable.
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.
So that's basically undoing the whole PR.