Skip to content

Commit 36d663f

Browse files
committed
Remove mem::transmute used in Box<str> conversions
1 parent 930d3b1 commit 36d663f

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

src/liballoc/boxed.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,7 @@ impl<'a> From<&'a str> for Box<str> {
528528
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
529529
impl From<Box<str>> for Box<[u8]> {
530530
fn from(s: Box<str>) -> Self {
531-
unsafe {
532-
mem::transmute(s)
533-
}
531+
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
534532
}
535533
}
536534

src/liballoc/str.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2047,10 +2047,8 @@ impl str {
20472047
/// ```
20482048
#[stable(feature = "box_str", since = "1.4.0")]
20492049
pub fn into_string(self: Box<str>) -> String {
2050-
unsafe {
2051-
let slice = mem::transmute::<Box<str>, Box<[u8]>>(self);
2052-
String::from_utf8_unchecked(slice.into_vec())
2053-
}
2050+
let slice = Box::<[u8]>::from(self);
2051+
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
20542052
}
20552053

20562054
/// Create a [`String`] by repeating a string `n` times.
@@ -2087,5 +2085,5 @@ impl str {
20872085
/// ```
20882086
#[stable(feature = "str_box_extras", since = "1.20.0")]
20892087
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
2090-
mem::transmute(v)
2088+
Box::from_raw(Box::into_raw(v) as *mut str)
20912089
}

0 commit comments

Comments
 (0)