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

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

mammothbane
Copy link
Contributor

@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

tgross35 commented Jul 30, 2024

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

edit: wow, this turned out to be a popular comment for some reason 😄

@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
Contributor 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
Contributor 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
Contributor 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.

@rust-log-analyzer

This comment has been minimized.

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

@@ -498,7 +498,7 @@ impl<A: Allocator> RawVecInner<A> {
}
Copy link
Contributor

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.

Copy link
Contributor

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.

library/alloc/src/string.rs Show resolved Hide resolved
library/alloc/src/string.rs Show resolved Hide resolved
library/alloc/src/vec/mod.rs Show resolved Hide resolved
library/alloc/src/vec/mod.rs Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

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

tgross35 commented Oct 7, 2024

@bors r=Amanieu,tgross35 rollup

@bors
Copy link
Contributor

bors commented Oct 7, 2024

📌 Commit d793766 has been approved by Amanieu,tgross35

It is now in the queue for this repository.

@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 Oct 7, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
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
@bors bors merged commit dd4f062 into rust-lang:master Oct 7, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 7, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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