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
..
2024-10-05 12:19:20 +03:00
2024-07-18 00:19:27 +00:00
2024-05-04 11:30:38 +02:00
2024-07-22 22:51:53 +00:00
2024-09-13 14:10:56 +03:00
2024-10-06 19:00:09 +02:00
2024-09-25 13:26:48 +02:00
2024-08-24 18:25:41 -04:00
2024-09-25 13:26:48 +02:00
2024-10-04 23:38:41 +00:00
2024-09-26 22:20:57 -07:00
2024-10-05 06:19:35 +00:00
2024-10-02 23:19:26 -05:00
2024-09-11 17:57:04 -04:00
2024-05-20 19:55:59 -07:00
2024-07-30 08:16:47 +02:00
2024-09-13 14:10:56 +03:00
2024-09-22 13:55:06 -04:00
2024-07-18 00:00:04 +00:00
2024-09-24 22:20:46 +02:00
2024-04-24 22:21:15 +00:00
2024-07-26 14:41:56 -04:00
2024-07-14 13:50:09 +09:00
2024-10-04 14:06:48 +02:00
2024-10-06 17:00:02 +00:00
2024-10-04 20:47:28 +02:00
2024-10-05 18:36:47 -04:00
2024-09-21 13:05:23 +02:00
2024-09-30 12:18:02 -04:00
2024-08-10 12:07:17 +02:00
2024-09-26 22:26:29 -04:00
2024-09-27 21:35:08 +02:00
2024-10-04 15:34:31 +02:00
2024-07-06 21:00:30 +08:00
2024-08-11 09:58:11 +01:00
2024-08-23 14:40:08 +10:00
2024-05-28 11:55:20 +00:00
2024-10-04 23:38:41 +00:00
2024-08-01 14:25:19 +02:00
2024-10-07 15:37:06 +11:00
2024-09-25 08:45:40 +00:00
2024-10-03 21:12:24 +10:00
2024-06-04 22:50:35 +02:00
2024-09-20 10:02:14 -07:00
2024-10-04 23:38:41 +00:00
2024-06-22 17:06:47 +00:00
2024-06-14 20:25:17 +02:00
2024-04-21 15:43:43 -03:00
2024-08-05 05:40:19 +02:00
2024-08-29 01:39:52 +08:00
2024-08-09 22:02:23 -04:00
2024-04-25 10:48:11 +03:00
2024-09-27 21:35:08 +02:00
2024-09-30 22:21:45 +08:00
2024-09-24 22:20:46 +02:00
2024-08-08 17:35:40 +00:00
2024-09-18 19:36:44 +02:00
2024-09-25 13:26:48 +02:00
2024-09-24 22:17:55 +02:00
2024-06-13 21:47:43 -04:00
2024-08-28 22:55:57 +09:00
2024-07-18 18:20:35 +00:00
2024-08-10 12:07:17 +02:00
2024-10-04 14:06:48 +02:00
2024-04-27 10:54:31 +03:00
2024-09-29 03:04:05 +00:00
2024-07-07 18:16:38 +02:00
2024-07-18 18:20:35 +00:00
2024-09-20 10:02:14 -07:00
2024-06-11 09:14:34 +01:00
2024-10-06 18:12:25 +02:00
2024-05-20 19:55:59 -07:00
2024-10-05 10:13:18 +02:00
2024-08-28 23:32:40 +01:00
2024-08-05 09:55:14 -04:00
2024-05-20 19:55:59 -07:00
2024-05-16 21:08:42 -04:00
2024-09-13 14:10:56 +03:00
2024-08-16 14:10:06 -04:00
2024-05-20 19:55:59 -07:00
2024-09-13 14:10:56 +03:00
2024-10-02 08:28:45 +03:00
2024-09-29 23:40:43 -05:00
2024-08-31 15:35:42 +03:00
2024-10-06 01:44:59 +00:00
2024-09-25 13:26:48 +02:00
2024-08-02 11:34:54 +00:00
2024-05-24 11:20:33 -04:00
2024-10-04 23:38:41 +00:00
2024-09-05 06:37:38 -04:00
2024-08-20 20:34:13 +08:00
2024-03-29 18:22:44 -07:00
2024-06-21 19:00:18 -04:00
2024-06-01 09:40:46 +08:00
2024-09-27 00:45:02 +00:00
2024-10-02 17:36:31 +08:00
2024-09-15 09:51:32 +02:00
2024-09-18 13:53:31 -07:00
2024-08-22 08:25:54 +02:00
2024-04-15 08:54:11 -04:00
2024-08-27 19:29:52 +02:00
2024-08-11 09:10:30 +01:00
2024-07-11 12:23:44 +10:00
2024-10-04 14:06:48 +02:00
2024-09-22 13:55:06 -04:00
2024-08-06 04:08:10 +00:00
2024-07-29 23:49:51 +00:00
2024-09-27 21:35:08 +02:00
2024-07-12 03:02:57 +00:00
2024-07-25 15:14:42 -04:00
2024-07-22 22:51:53 +00:00
2024-09-20 10:02:14 -07:00
2024-09-22 13:55:06 -04:00
2024-07-12 03:22:32 +00:00
2024-09-06 10:32:48 -04:00
2024-09-29 23:40:43 -05:00
2024-09-20 10:02:14 -07:00
2024-09-13 14:10:56 +03:00
2024-10-05 00:19:26 +00:00
2024-07-04 02:02:21 +00:00
2024-06-20 04:25:17 +00:00
2024-04-29 14:53:38 +02:00
2024-09-13 14:10:56 +03:00
2024-10-04 15:42:53 +02:00
2024-07-18 20:08:38 +00:00
2024-05-28 12:31:33 +02:00
2024-03-31 20:44:30 -04:00
2024-07-05 11:17:49 -04:00
2024-05-20 11:13:10 -04:00
2024-09-29 11:57:18 -04:00
2024-09-25 11:10:38 -04:00
2024-10-04 14:06:48 +02:00
2024-04-21 20:10:12 -04:00
2024-04-07 17:38:07 -03:00
2024-07-26 14:41:56 -04:00
2024-07-04 02:02:21 +00:00
2024-06-13 20:22:21 +00:00
2024-07-11 12:23:44 +10:00
2024-10-05 19:10:47 -04:00
2024-09-24 22:20:46 +02:00
2024-09-22 13:55:06 -04:00
2024-07-18 18:20:35 +00:00
2024-09-06 17:06:35 +03:00
2024-09-27 21:35:08 +02:00
2024-09-14 12:41:25 +02:00
2024-09-22 13:55:06 -04:00
2024-07-12 03:02:57 +00:00
2024-08-19 21:39:57 +02:00
2024-08-18 19:46:53 +02:00
2024-10-05 17:12:46 +02:00
2024-05-28 12:31:12 +02:00
2024-08-02 11:34:54 +00:00
2024-10-06 01:44:59 +00:00
2024-06-27 22:24:36 +02:00
2024-09-23 02:12:53 +03:00
2024-08-08 20:53:25 -04:00
2024-04-11 17:53:27 -04:00
2024-04-24 08:05:29 +00:00
2024-10-04 23:38:41 +00:00
2024-08-24 06:57:47 +10:00
2024-08-02 11:34:54 +00:00
2024-08-03 07:57:31 -04:00
2024-07-12 03:02:57 +00:00
2024-10-05 19:10:47 -04:00
2024-10-05 19:10:47 -04:00
2024-07-12 21:16:09 -04:00
2024-09-24 23:12:02 +02:00
2024-07-11 20:39:24 +00:00
2024-09-09 19:39:43 -07:00
2024-10-06 01:44:59 +00:00
2024-08-25 20:30:06 +08:00
2024-10-06 18:12:25 +02:00
2024-05-28 12:31:12 +02:00
2024-08-09 16:16:16 -07:00
2024-05-23 09:07:59 +08:00
2024-09-17 16:43:18 -04:00
2024-05-19 20:09:03 -07:00
2024-07-18 20:08:38 +00:00
2024-08-21 18:15:02 +02:00
2024-10-04 23:38:41 +00:00
2024-05-04 11:30:38 +02:00
2024-09-18 13:53:31 -07:00
2024-10-06 01:44:59 +00:00
2024-09-22 13:55:06 -04:00
2024-09-29 23:40:43 -05:00
2024-06-30 17:08:45 +00:00
2024-07-29 09:50:07 +02:00
2024-09-26 13:21:15 +02:00
2024-09-27 21:35:08 +02:00
2024-08-31 23:56:45 +02:00
2024-09-03 14:36:21 +02:00
2024-04-28 16:10:12 -04:00
2024-07-18 20:08:38 +00:00
2024-08-05 17:56:50 -07:00
2024-09-18 17:31:56 +08:00
2024-09-27 21:35:08 +02:00
2024-07-17 11:01:29 +01:00
2024-05-24 17:44:37 -07:00
2024-09-01 16:35:53 +02:00
2024-07-15 22:21:41 +00:00
2024-07-11 00:18:47 +08:00
2024-09-20 17:25:34 +00:00
2024-08-24 05:32:52 +02:00
2024-07-10 18:56:06 -04:00
2024-07-11 12:23:44 +10:00
2024-09-27 00:45:02 +00:00
2024-10-04 19:19:27 -07:00
2024-10-01 20:52:17 +00:00
2024-07-11 12:23:44 +10:00
2024-07-11 12:23:44 +10:00
2024-07-23 01:48:03 +02:00
2024-07-26 14:41:56 -04:00
2024-09-25 19:00:19 -07:00
2024-07-14 13:50:09 +09:00
2024-09-27 21:35:08 +02:00
2024-07-23 01:26:25 +02:00
2024-08-28 22:55:57 +09:00
2024-10-04 23:44:29 +00:00
2024-07-15 22:05:45 +02:00
2024-10-04 23:38:41 +00:00
2024-07-11 20:39:24 +00:00
2024-09-22 13:55:06 -04:00
2024-07-22 22:51:53 +00:00
2024-07-29 23:49:51 +00:00
2024-09-13 21:01:29 +02:00
2024-09-13 14:10:56 +03:00
2024-07-26 14:41:56 -04:00
2024-08-18 19:46:53 +02:00
2024-07-29 23:49:51 +00:00
2024-09-18 16:45:48 -04:00
2024-09-27 00:45:02 +00:00
2024-03-24 09:34:11 +01:00
2024-06-03 07:25:32 +02:00
2024-06-19 13:54:55 +01:00
2024-05-21 20:16:39 +00:00
2024-08-23 23:00:45 +00:00
2024-07-18 00:00:04 +00:00
2024-05-09 14:47:09 +10:00
2024-09-29 23:40:43 -05:00
2024-06-19 04:41:56 +00:00
2024-07-02 11:37:59 -04:00
2024-06-13 12:55:55 +02:00
2024-03-27 14:02:16 +00:00
2024-07-04 02:02:21 +00:00
2024-07-04 02:02:21 +00:00
2024-04-17 13:00:43 +02:00
2024-07-26 14:41:56 -04:00
2024-07-26 14:41:56 -04:00
2024-06-25 10:00:30 +08:00
2024-07-04 05:36:34 +00:00
2024-07-04 02:02:21 +00:00
2024-07-22 22:51:53 +00:00
2024-07-11 12:23:44 +10:00
2024-06-11 15:47:00 +02:00
2024-08-22 09:36:14 -07:00
2024-08-22 09:36:14 -07:00
2024-10-04 14:06:48 +02:00
2024-04-07 01:16:45 +02:00
2024-09-11 17:57:04 -04:00
2024-09-11 17:57:04 -04:00
2024-04-07 01:16:45 +02:00
2024-09-13 14:10:56 +03:00
2024-07-25 20:53:33 +03:00
2024-07-06 21:00:30 +08:00
2024-07-06 21:00:30 +08:00
2024-06-25 10:00:30 +08:00
2024-07-02 11:37:59 -04:00
2024-04-07 01:16:45 +02:00
2024-04-15 08:54:11 -04:00
2024-04-15 08:54:11 -04:00
2024-07-14 13:50:09 +09:00
2024-04-11 16:41:42 +00:00
2024-08-03 20:09:42 -04:00
2024-09-21 13:04:14 +02:00
2024-07-22 22:51:53 +00:00
2024-08-06 04:08:10 +00:00
2024-06-25 18:06:22 +02:00
2024-07-12 21:16:09 -04:00
2024-07-02 15:48:48 -04:00
2024-04-17 13:00:43 +02:00
2024-04-17 13:00:43 +02:00
2024-07-11 12:23:44 +10:00
2024-07-05 00:52:01 +00:00
2024-06-29 19:39:09 +08:00
2024-05-20 11:13:10 -04:00
2024-04-24 08:05:29 +00:00
2024-05-11 15:13:18 +02:00