diff options
| author | Stuart Cook <Zalathar@users.noreply.github.com> | 2024-10-07 15:37:06 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-07 15:37:06 +1100 |
| commit | dd4f062b0765a8a152b184f03b08645cac20e0e7 (patch) | |
| tree | 2aa89f538c63107b20b85b346930abf32ba393d5 /compiler/rustc_codegen_llvm/src | |
| parent | a964a9227787447016894268152bff79ee76a36b (diff) | |
| parent | d793766a6199d9b7339633ad0296af63c7b4f9b8 (diff) | |
| download | rust-dd4f062b0765a8a152b184f03b08645cac20e0e7.tar.gz rust-dd4f062b0765a8a152b184f03b08645cac20e0e7.zip | |
Rollup merge of #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.
Diffstat (limited to 'compiler/rustc_codegen_llvm/src')
0 files changed, 0 insertions, 0 deletions
