Skip to content

Commit 21975a1

Browse files
committed
add comments about safety
1 parent ef2957d commit 21975a1

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/libstd/ffi/os_str.rs

+4
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,15 @@ impl OsStr {
525525

526526
#[inline]
527527
fn from_inner(inner: &Slice) -> &OsStr {
528+
// Safety: OsStr is just a wrapper of Slice,
529+
// therefore converting &Slice to &OsStr is safe.
528530
unsafe { &*(inner as *const Slice as *const OsStr) }
529531
}
530532

531533
#[inline]
532534
fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
535+
// Safety: OsStr is just a wrapper of Slice,
536+
// therefore converting &mut Slice to &mut OsStr is safe.
533537
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
534538
}
535539

src/libstd/sys/windows/os_str.rs

+6
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,17 @@ impl Buf {
7777
}
7878

7979
pub fn as_slice(&self) -> &Slice {
80+
// Safety: Slice is just a wrapper for Wtf8,
81+
// and as_slice returns &Wtf8. Therefore,
82+
// transmute &Wtf8 to &Slice is safe.
8083
unsafe { mem::transmute(self.inner.as_slice()) }
8184
}
8285

8386
#[inline]
8487
pub fn as_mut_slice(&mut self) -> &mut Slice {
88+
// Safety: Slice is just a wrapper for Wtf8,
89+
// and as_slice returns &Wtf8. Therefore,
90+
// transmute &mut Wtf8 to &mut Slice is safe.
8591
unsafe { mem::transmute(self.inner.as_mut_slice()) }
8692
}
8793

src/libstd/sys_common/os_str_bytes.rs

+6
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,17 @@ impl Buf {
106106

107107
#[inline]
108108
pub fn as_slice(&self) -> &Slice {
109+
// Safety: Slice just wraps [u8],
110+
// and &*self.inner is &[u8], therefore
111+
// transmuting &[u8] to &Slice is safe.
109112
unsafe { mem::transmute(&*self.inner) }
110113
}
111114

112115
#[inline]
113116
pub fn as_mut_slice(&mut self) -> &mut Slice {
117+
// Safety: Slice just wraps [u8],
118+
// and &mut *self.inner is &mut [u8], therefore
119+
// transmuting &mut [u8] to &mut Slice is safe.
114120
unsafe { mem::transmute(&mut *self.inner) }
115121
}
116122

0 commit comments

Comments
 (0)