Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,7 @@ impl<'a> From<&'a str> for Box<str> {
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
impl From<Box<str>> for Box<[u8]> {
fn from(s: Box<str>) -> Self {
unsafe {
mem::transmute(s)
}
unsafe { Box::from_raw(Box::into_raw(s) as *mut [u8]) }
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,10 +2047,8 @@ impl str {
/// ```
#[stable(feature = "box_str", since = "1.4.0")]
pub fn into_string(self: Box<str>) -> String {
unsafe {
let slice = mem::transmute::<Box<str>, Box<[u8]>>(self);
String::from_utf8_unchecked(slice.into_vec())
}
let slice = Box::<[u8]>::from(self);
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
}

/// Create a [`String`] by repeating a string `n` times.
Expand Down Expand Up @@ -2087,5 +2085,5 @@ impl str {
/// ```
#[stable(feature = "str_box_extras", since = "1.20.0")]
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
mem::transmute(v)
Box::from_raw(Box::into_raw(v) as *mut str)
}
15 changes: 8 additions & 7 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl CString {
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
let len = libc::strlen(ptr) + 1; // Including the NUL byte
let slice = slice::from_raw_parts(ptr, len as usize);
CString { inner: mem::transmute(slice) }
CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use slice::from_raw_parts_mut to get a &mut [i8].

[00:02:36] error[E0606]: casting `&[i8]` as `*mut [i8]` is invalid
[00:02:36]    --> /checkout/src/libstd/ffi/c_str.rs:315:40
[00:02:36]     |
[00:02:36] 315 |         CString { inner: Box::from_raw(slice as *mut [c_char] as *mut [u8]) }
[00:02:36]     |                                        ^^^^^^^^^^^^^^^^^^^^^^

}

/// Transfers ownership of the string to a C caller.
Expand Down Expand Up @@ -480,7 +480,7 @@ impl CString {
/// ```
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
pub fn into_boxed_c_str(self) -> Box<CStr> {
unsafe { mem::transmute(self.into_inner()) }
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
}

// Bypass "move out of struct which implements [`Drop`] trait" restriction.
Expand Down Expand Up @@ -569,7 +569,7 @@ impl Borrow<CStr> for CString {
impl<'a> From<&'a CStr> for Box<CStr> {
fn from(s: &'a CStr) -> Box<CStr> {
let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
unsafe { mem::transmute(boxed) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
}
}

Expand All @@ -593,7 +593,7 @@ impl From<CString> for Box<CStr> {
impl Default for Box<CStr> {
fn default() -> Box<CStr> {
let boxed: Box<[u8]> = Box::from([0]);
unsafe { mem::transmute(boxed) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
}
}

Expand Down Expand Up @@ -817,7 +817,7 @@ impl CStr {
#[inline]
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &CStr {
mem::transmute(bytes)
&*(bytes as *const [u8] as *const CStr)
}

/// Returns the inner pointer to this C string.
Expand Down Expand Up @@ -913,7 +913,7 @@ impl CStr {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn to_bytes_with_nul(&self) -> &[u8] {
unsafe { mem::transmute(&self.inner) }
unsafe { &*(&self.inner as *const [c_char] as *const [u8]) }
}

/// Yields a [`&str`] slice if the `CStr` contains valid UTF-8.
Expand Down Expand Up @@ -1005,7 +1005,8 @@ impl CStr {
/// ```
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
pub fn into_c_string(self: Box<CStr>) -> CString {
unsafe { mem::transmute(self) }
let raw = Box::into_raw(self) as *mut [u8];
CString { inner: unsafe { Box::from_raw(raw) } }
}
}

Expand Down