about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-09-07 23:30:14 +0200
committerGitHub <noreply@github.com>2024-09-07 23:30:14 +0200
commitc139dc62812313bf97483a1160594bfd25ffbd1c (patch)
tree5b92b7eb0ccb87e21bb6b3103743e9282cb6690a
parent04b4523efcf0790458740f1c59f7c2d248be7c9c (diff)
parenta8a7fd418e217f6b0e66eaa81c3139efdb9174cc (diff)
downloadrust-c139dc62812313bf97483a1160594bfd25ffbd1c.tar.gz
rust-c139dc62812313bf97483a1160594bfd25ffbd1c.zip
Rollup merge of #130046 - RalfJung:const_str_as_mut, r=dtolnay
str: make as_mut_ptr and as_bytes_mut unstably const

`@rust-lang/libs-api` the corresponding non-mutable methods are already const fn, so this seems pretty trivial. I hope this is small enough that it does not need an ACP? :)

I would like to get these stabilized ASAP because I want to avoid people doing `s.as_ptr().cast_mut()`, which is UB if they ever write to it, but is already const-stable.

TODO: create a tracking issue.
-rw-r--r--library/core/src/str/mod.rs6
1 files changed, 4 insertions, 2 deletions
diff --git a/library/core/src/str/mod.rs b/library/core/src/str/mod.rs
index cf9f1bfc0eb..e947686487e 100644
--- a/library/core/src/str/mod.rs
+++ b/library/core/src/str/mod.rs
@@ -338,9 +338,10 @@ impl str {
     /// assert_eq!("🍔∈🌏", s);
     /// ```
     #[stable(feature = "str_mut_extras", since = "1.20.0")]
+    #[rustc_const_unstable(feature = "const_str_as_mut", issue = "130086")]
     #[must_use]
     #[inline(always)]
-    pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
+    pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
         // SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
         // has the same layout as `&[u8]` (only std can make this guarantee).
         // The pointer dereference is safe since it comes from a mutable reference which
@@ -383,10 +384,11 @@ impl str {
     /// It is your responsibility to make sure that the string slice only gets
     /// modified in a way that it remains valid UTF-8.
     #[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
+    #[rustc_const_unstable(feature = "const_str_as_mut", issue = "130086")]
     #[rustc_never_returns_null_ptr]
     #[must_use]
     #[inline(always)]
-    pub fn as_mut_ptr(&mut self) -> *mut u8 {
+    pub const fn as_mut_ptr(&mut self) -> *mut u8 {
         self as *mut str as *mut u8
     }