-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
liballoc: introduce String, Vec const-slicing #128399
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @tgross35 (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
This shouldn't be implemented as new functions, the existing I am not sure that the motivation is that strong but it seems harmless to unstably add r? libs-api |
You may as well change the implementation before libs-api takes a look since it's pretty easy, and much of this PR as-is won't be needed. Please do the updates to @rustbot author |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for the review! Made those changes and added @rustbot review |
☔ The latest upstream changes (presumably #126793) made this pull request unmergeable. Please resolve the merge conflicts. |
library/alloc/src/string.rs
Outdated
@@ -1006,7 +1006,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 = "none")] |
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.
This needs a tracking issue so that we can eventually const-stabilize it.
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've created #129041
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.
Can you update all the rustc_const_unstable
to point to that tracking issue? Then it should be ready to merge.
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 am not the author of the PR, nor am I a member of the org, so I do not have permissions to do so. Ping @mammothbane, as this PR needs a rebase anyways.
@rustbot author |
Sorry for the delays here — my dev env broke and it's taken me a while to get around to fixing it and verifying my rebase. Working on it now. |
I know you're probably not done yet, but please make sure you squash at some point |
Squashed -- all changes made afaik. @rustbot review |
☔ The latest upstream changes (presumably #128299) made this pull request unmergeable. Please resolve the merge conflicts. |
@rust-lang/wg-const-eval I don't think this introduces any const-eval issues, but just double-checking with you. r=me once conflicts are resolved. |
@@ -1042,7 +1044,7 @@ impl String { | |||
#[must_use] | |||
#[stable(feature = "string_as_str", since = "1.7.0")] | |||
pub fn as_mut_str(&mut self) -> &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.
FWIW it should be fine to also do this with the mut
methods
self | ||
#[rustc_const_unstable(feature = "const_vec_string_slice", issue = "129041")] | ||
pub const fn as_str(&self) -> &str { | ||
unsafe { str::from_utf8_unchecked(self.vec.as_slice()) } |
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 doesn't tidy require a SAFETY
comment here?
This comment has been minimized.
This comment has been minimized.
This change `const`-qualifies many methods on Vec and String, notably `as_slice`, `as_str`, `len`. These changes are made behind the unstable feature flag `const_vec_string_slice` with the following tracking issue: rust-lang#129041
☔ The latest upstream changes (presumably #130572) made this pull request unmergeable. Please resolve the merge conflicts. |
This change
const
-qualifies many methods onVec
andString
, notablyas_slice
,as_str
,len
. These changes are made behind the unstable feature flagconst_vec_string_slice
.Motivation
This is to support simultaneous variance over ownership and constness. I have an enum type that may contain either
String
or&str
, and I want to produce a&str
from it in a possibly-const
context.Currently
String
andVec
don't support this, but can without functional changes. Similar logic applies forlen
,capacity
,is_empty
.Changes
The essential thing enabling this change is that
Unique::as_ptr
isconst
. This lets us convertRawVec::ptr
->Vec::as_ptr
->Vec::as_slice
->String::as_str
.I had to move the
Deref
implementations intoas_{str,slice}
becauseDeref
isn't#[const_trait]
, but I would expect this change to be invisible up to inlining. I moved theDerefMut
implementations as well for uniformity.