-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
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 edit: wow, this turned out to be a popular comment for some reason 😄 |
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. |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #130572) made this pull request unmergeable. Please resolve the merge conflicts. |
bcb7aa8
to
f07b0f9
Compare
@@ -498,7 +498,7 @@ impl<A: Allocator> RawVecInner<A> { | |||
} |
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). Then fn ptr
can be marked const
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
and non_null
functions.
Anyway, this change plus the safety comments Oli requested and we can merge this.
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.
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
@bors r=Amanieu,tgross35 rollup |
Rollup of 3 pull requests Successful merges: - rust-lang#128399 (liballoc: introduce String, Vec const-slicing) - rust-lang#131308 (enable f16 and f128 on windows-gnullvm targets) - rust-lang#131325 (coverage: Multiple small tweaks to counter creation) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#128399 - mammothbane:master, r=Amanieu,tgross35 liballoc: introduce String, Vec const-slicing 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`. ## 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. ```rust enum StrOrString<'s> { Str(&'s str), String(String), } impl<'s> StrOrString<'s> { const fn as_str(&self) -> &str { match self { // In a const-context, I really only expect to see this variant, but I can't switch the implementation // in some mode like #[cfg(const)] -- there has to be a single body Self::Str(s) => s, // so this is a problem, since it's not `const` Self::String(s) => s.as_str(), } } } ``` Currently `String` and `Vec` don't support this, but can without functional changes. Similar logic applies for `len`, `capacity`, `is_empty`. ## Changes The essential thing enabling this change is that `Unique::as_ptr` is `const`. This lets us convert `RawVec::ptr` -> `Vec::as_ptr` -> `Vec::as_slice` -> `String::as_str`. I had to move the `Deref` implementations into `as_{str,slice}` because `Deref` isn't `#[const_trait]`, but I would expect this change to be invisible up to inlining. I moved the `DerefMut` implementations as well for uniformity.
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.