From 51688e29b0b1baccd9185bf0d963c4fbaafdaf5b Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 2 Jul 2021 17:23:40 +0800 Subject: [PATCH] constified str::from_utf8_unchecked_mut --- library/core/src/str/converts.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/str/converts.rs b/library/core/src/str/converts.rs index a51d142074822..d6fe9c92b1b7a 100644 --- a/library/core/src/str/converts.rs +++ b/library/core/src/str/converts.rs @@ -183,10 +183,10 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str { /// ``` #[inline] #[stable(feature = "str_mut_extras", since = "1.20.0")] -pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str { - // SAFETY: the caller must guarantee that the bytes `v` - // are valid UTF-8, thus the cast to `*mut str` is safe. - // Also, the pointer dereference is safe because that pointer - // comes from a reference which is guaranteed to be valid for writes. - unsafe { &mut *(v as *mut [u8] as *mut str) } +#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")] +#[rustc_allow_const_fn_unstable(const_fn_transmute)] +pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str { + // SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8. + // Also relies on `&mut str` and `&mut [u8]` having the same layout. + unsafe { mem::transmute(v) } }