Skip to content

Commit 9448662

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 2feca1e + 3a53537 commit 9448662

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

core/src/str/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ impl str {
338338
/// assert_eq!("🍔∈🌏", s);
339339
/// ```
340340
#[stable(feature = "str_mut_extras", since = "1.20.0")]
341+
#[rustc_const_unstable(feature = "const_str_as_mut", issue = "130086")]
341342
#[must_use]
342343
#[inline(always)]
343-
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
344+
pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
344345
// SAFETY: the cast from `&str` to `&[u8]` is safe since `str`
345346
// has the same layout as `&[u8]` (only std can make this guarantee).
346347
// The pointer dereference is safe since it comes from a mutable reference which
@@ -383,10 +384,11 @@ impl str {
383384
/// It is your responsibility to make sure that the string slice only gets
384385
/// modified in a way that it remains valid UTF-8.
385386
#[stable(feature = "str_as_mut_ptr", since = "1.36.0")]
387+
#[rustc_const_unstable(feature = "const_str_as_mut", issue = "130086")]
386388
#[rustc_never_returns_null_ptr]
387389
#[must_use]
388390
#[inline(always)]
389-
pub fn as_mut_ptr(&mut self) -> *mut u8 {
391+
pub const fn as_mut_ptr(&mut self) -> *mut u8 {
390392
self as *mut str as *mut u8
391393
}
392394

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy