-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Constified str::from_utf8_unchecked #75157
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
LGTM with a tracking issue and the build failure fixed: |
library/core/src/str/mod.rs
Outdated
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 { StrOrSlice { slice: v }.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.
I think calling transmute
would be preferred over a hacky transmute-union.
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.
But, can't you use the const_raw_ptr_deref
feature gate to keep using the old code?
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.
I'd prefer the transmute version
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.
Why? Usually a ptr cast is preferred over a transmute as it is slightly more restrictive (ensures that these are actually pointer types).
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.
usually I agree, but &[u8]
and &str
are exactly the same thing, the pointer cast and deref is harder to read than a transmute imo. But I'm fine keeping the status quo (and also using it for as_bytes
)
@sfackler btw, PRs like this (that introduce hacks into the code to make it const-compatible) should be getting the "const hack" label. But I think we can avoid the hacks. |
@@ -2350,13 +2357,8 @@ impl str { | |||
#[allow(unused_attributes)] | |||
#[allow_internal_unstable(const_fn_union)] | |||
pub const fn as_bytes(&self) -> &[u8] { |
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.
we could also un-hack this function by using transmute
@bors r+ |
📌 Commit 1837708 has been approved by |
☀️ Test successful - checks-actions, checks-azure |
This would be useful for const code to use an array to construct a string using guaranteed utf8 inputs, and then create a
&str
from it.