add comment about maintaining OsStr encoding

This commit is contained in:
TyPR124 2020-03-20 09:00:53 -04:00
parent e4a65e83e7
commit 45416cd91a
2 changed files with 4 additions and 0 deletions

View File

@ -534,6 +534,8 @@ fn from_inner(inner: &Slice) -> &OsStr {
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.
// Any method that mutates OsStr must be careful not to
// break platform-specific encoding, in particular Wtf8 on Windows.
unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
}

View File

@ -87,6 +87,8 @@ pub fn as_mut_slice(&mut self) -> &mut Slice {
// Safety: Slice is just a wrapper for Wtf8,
// and self.inner.as_mut_slice() returns &mut Wtf8.
// Therefore, transmuting &mut Wtf8 to &mut Slice is safe.
// Additionally, care should be taken to ensure the slice
// is always valid Wtf8.
unsafe { mem::transmute(self.inner.as_mut_slice()) }
}