rust/library/alloc/src
Stuart Cook dd4f062b07
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.
2024-10-07 15:37:06 +11:00
..
alloc Reformat use declarations. 2024-07-29 08:26:52 +10:00
boxed Use &raw in the standard library 2024-09-25 17:03:20 -07:00
collections Avoid emptiness check in PeekMut::pop 2024-10-03 22:15:52 +08:00
ffi Mark some more types as having insignificant dtor 2024-09-27 11:59:39 -04:00
raw_vec Polymorphize RawVec 2024-08-09 20:06:26 -04:00
rc Add str.as_str() for easy dereferencing of Box<str> 2024-09-19 16:25:56 +01:00
slice Reformat use declarations. 2024-07-29 08:26:52 +10:00
sync Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
testing Reformat use declarations. 2024-07-29 08:26:52 +10:00
vec Rollup merge of #128399 - mammothbane:master, r=Amanieu,tgross35 2024-10-07 15:37:06 +11:00
alloc.rs read_volatile __rust_no_alloc_shim_is_unstable in alloc_zeroed 2024-09-17 22:26:21 -04:00
borrow.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
boxed.rs Mark some more smart pointers as insignificant 2024-09-27 11:59:39 -04:00
fmt.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Rollup merge of #128399 - mammothbane:master, r=Amanieu,tgross35 2024-10-07 15:37:06 +11:00
macros.rs Mark format! with must_use hint 2024-07-06 14:24:20 +02:00
raw_vec.rs liballoc: introduce String, Vec const-slicing 2024-10-06 19:58:35 -04:00
rc.rs Use &raw in the standard library 2024-09-25 17:03:20 -07:00
slice.rs Rollup merge of #130416 - BatmanAoD:130122-sort-by-docs, r=Mark-Simulacrum 2024-09-29 12:37:51 +02:00
str.rs Improve autovectorization of to_lowercase / to_uppercase functions 2024-09-23 11:31:29 +02:00
string.rs Rollup merge of #128399 - mammothbane:master, r=Amanieu,tgross35 2024-10-07 15:37:06 +11:00
sync.rs Mark some more smart pointers as insignificant 2024-09-27 11:59:39 -04:00
task.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
tests.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00