Skip to content

Commit 51688e2

Browse files
committed
constified str::from_utf8_unchecked_mut
1 parent 44860d1 commit 51688e2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Diff for: library/core/src/str/converts.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
183183
/// ```
184184
#[inline]
185185
#[stable(feature = "str_mut_extras", since = "1.20.0")]
186-
pub unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
187-
// SAFETY: the caller must guarantee that the bytes `v`
188-
// are valid UTF-8, thus the cast to `*mut str` is safe.
189-
// Also, the pointer dereference is safe because that pointer
190-
// comes from a reference which is guaranteed to be valid for writes.
191-
unsafe { &mut *(v as *mut [u8] as *mut str) }
186+
#[rustc_const_stable(feature = "const_str_from_utf8_unchecked", since = "1.55.0")]
187+
#[rustc_allow_const_fn_unstable(const_fn_transmute)]
188+
pub const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str {
189+
// SAFETY: the caller must guarantee that the bytes `v` are valid UTF-8.
190+
// Also relies on `&mut str` and `&mut [u8]` having the same layout.
191+
unsafe { mem::transmute(v) }
192192
}

0 commit comments

Comments
 (0)