Skip to content
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

Merged
merged 7 commits into from
Aug 14, 2020
Merged

Conversation

rodrimati1992
Copy link
Contributor

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.

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 4, 2020
@sfackler
Copy link
Member

sfackler commented Aug 5, 2020

LGTM with a tracking issue and the build failure fixed: tidy error: /checkout/library/core/src/str/mod.rs:2364: trailing whitespace

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 }
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor

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

Copy link
Member

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).

Copy link
Contributor

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)

@RalfJung
Copy link
Member

RalfJung commented Aug 7, 2020

@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] {
Copy link
Contributor

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

@oli-obk
Copy link
Contributor

oli-obk commented Aug 14, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Aug 14, 2020

📌 Commit 1837708 has been approved by oli-obk

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2020
@bors
Copy link
Contributor

bors commented Aug 14, 2020

⌛ Testing commit 1837708 with merge 55b9adf...

@bors
Copy link
Contributor

bors commented Aug 14, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: oli-obk
Pushing 55b9adf to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 14, 2020
@bors bors merged commit 55b9adf into rust-lang:master Aug 14, 2020
@cuviper cuviper added this to the 1.47.0 milestone May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants