From ef2957de137fb8d6959310e5d8f2fa1d600d7d36 Mon Sep 17 00:00:00 2001 From: TyPR124 Date: Mon, 16 Mar 2020 13:13:07 -0400 Subject: allowing getting &mut OsStr from OsString --- src/libstd/sys_common/os_str_bytes.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/libstd/sys_common') diff --git a/src/libstd/sys_common/os_str_bytes.rs b/src/libstd/sys_common/os_str_bytes.rs index e965ea79aa0..5767350336d 100644 --- a/src/libstd/sys_common/os_str_bytes.rs +++ b/src/libstd/sys_common/os_str_bytes.rs @@ -109,6 +109,11 @@ impl Buf { unsafe { mem::transmute(&*self.inner) } } + #[inline] + pub fn as_mut_slice(&mut self) -> &mut Slice { + unsafe { mem::transmute(&mut *self.inner) } + } + pub fn into_string(self) -> Result { String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() }) } -- cgit 1.4.1-3-g733a5 From 21975a1aaa682a88266970459f6f67829774d6c4 Mon Sep 17 00:00:00 2001 From: TyPR124 Date: Mon, 16 Mar 2020 16:12:54 -0400 Subject: add comments about safety --- src/libstd/ffi/os_str.rs | 4 ++++ src/libstd/sys/windows/os_str.rs | 6 ++++++ src/libstd/sys_common/os_str_bytes.rs | 6 ++++++ 3 files changed, 16 insertions(+) (limited to 'src/libstd/sys_common') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 2f921ce7a5f..45417403c10 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -525,11 +525,15 @@ impl OsStr { #[inline] fn from_inner(inner: &Slice) -> &OsStr { + // Safety: OsStr is just a wrapper of Slice, + // therefore converting &Slice to &OsStr is safe. unsafe { &*(inner as *const Slice as *const OsStr) } } #[inline] fn from_inner_mut(inner: &mut Slice) -> &mut OsStr { + // Safety: OsStr is just a wrapper of Slice, + // therefore converting &mut Slice to &mut OsStr is safe. unsafe { &mut *(inner as *mut Slice as *mut OsStr) } } diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs index 4cff23b2f4a..e768d9c326b 100644 --- a/src/libstd/sys/windows/os_str.rs +++ b/src/libstd/sys/windows/os_str.rs @@ -77,11 +77,17 @@ impl Buf { } pub fn as_slice(&self) -> &Slice { + // Safety: Slice is just a wrapper for Wtf8, + // and as_slice returns &Wtf8. Therefore, + // transmute &Wtf8 to &Slice is safe. unsafe { mem::transmute(self.inner.as_slice()) } } #[inline] pub fn as_mut_slice(&mut self) -> &mut Slice { + // Safety: Slice is just a wrapper for Wtf8, + // and as_slice returns &Wtf8. Therefore, + // transmute &mut Wtf8 to &mut Slice is safe. unsafe { mem::transmute(self.inner.as_mut_slice()) } } diff --git a/src/libstd/sys_common/os_str_bytes.rs b/src/libstd/sys_common/os_str_bytes.rs index 5767350336d..c5d02fb1772 100644 --- a/src/libstd/sys_common/os_str_bytes.rs +++ b/src/libstd/sys_common/os_str_bytes.rs @@ -106,11 +106,17 @@ impl Buf { #[inline] pub fn as_slice(&self) -> &Slice { + // Safety: Slice just wraps [u8], + // and &*self.inner is &[u8], therefore + // transmuting &[u8] to &Slice is safe. unsafe { mem::transmute(&*self.inner) } } #[inline] pub fn as_mut_slice(&mut self) -> &mut Slice { + // Safety: Slice just wraps [u8], + // and &mut *self.inner is &mut [u8], therefore + // transmuting &mut [u8] to &mut Slice is safe. unsafe { mem::transmute(&mut *self.inner) } } -- cgit 1.4.1-3-g733a5