1
Fork 0

add comments about safety

This commit is contained in:
TyPR124 2020-03-16 16:12:54 -04:00
parent ef2957de13
commit 21975a1aaa
3 changed files with 16 additions and 0 deletions

View file

@ -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) }
}

View file

@ -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()) }
}

View file

@ -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) }
}