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

liballoc: introduce String, Vec const-slicing #128399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mammothbane
Copy link

@mammothbane mammothbane commented Jul 30, 2024

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.

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.

@rustbot
Copy link
Collaborator

rustbot commented Jul 30, 2024

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 30, 2024
@tgross35
Copy link
Contributor

This shouldn't be implemented as new functions, the existing as_ptr, len etc just get a const keyword and a #[rustc_const_unstable(...)] attribute.

I am not sure that the motivation is that strong but it seems harmless to unstably add const to most of the non-&mut methods. This is an API change though so

r? libs-api

@rustbot rustbot added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jul 30, 2024
@rustbot rustbot assigned Amanieu and unassigned tgross35 Jul 30, 2024
@tgross35 tgross35 removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 30, 2024
@tgross35
Copy link
Contributor

tgross35 commented Jul 30, 2024

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 rustc_const_unstable as mentioned above. Including len, is_empty, as_bytes, and as_slice seems reasonable too.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 30, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@mammothbane
Copy link
Author

Thanks for the review! Made those changes and added capacity on the same logic.

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 31, 2024
@bors
Copy link
Contributor

bors commented Aug 12, 2024

☔ The latest upstream changes (presumably #126793) made this pull request unmergeable. Please resolve the merge conflicts.

@@ -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")]
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #129041

Copy link
Member

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.

Copy link
Contributor

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.

@GrigorenkoPV
Copy link
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 24, 2024
@mammothbane
Copy link
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.

@tgross35
Copy link
Contributor

tgross35 commented Sep 5, 2024

I know you're probably not done yet, but please make sure you squash at some point

@mammothbane
Copy link
Author

Squashed -- all changes made afaik.

@rustbot review

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 5, 2024
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 5, 2024
@bors
Copy link
Contributor

bors commented Sep 14, 2024

☔ The latest upstream changes (presumably #128299) made this pull request unmergeable. Please resolve the merge conflicts.

@Amanieu
Copy link
Member

Amanieu commented Sep 15, 2024

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

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()) }
Copy link
Contributor

@oli-obk oli-obk Sep 15, 2024

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?

@rust-log-analyzer

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
Copy link
Contributor

bors commented Sep 19, 2024

☔ The latest upstream changes (presumably #130572) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants