@@ -299,20 +299,6 @@ where
299299 }
300300}
301301
302- unsafe fn u8_slice_as_os_str ( s : & [ u8 ] ) -> & OsStr {
303- // SAFETY: See note at the top of this module to understand why this and
304- // `OsStr::bytes` are used:
305- //
306- // This casts are safe as OsStr is internally a wrapper around [u8] on all
307- // platforms.
308- //
309- // Note that currently this relies on the special knowledge that std has;
310- // these types are single-element structs but are not marked
311- // repr(transparent) or repr(C) which would make these casts not allowable
312- // outside std.
313- unsafe { & * ( s as * const [ u8 ] as * const OsStr ) }
314- }
315-
316302// Detect scheme on Redox
317303fn has_redox_scheme ( s : & [ u8 ] ) -> bool {
318304 cfg ! ( target_os = "redox" ) && s. contains ( & b':' )
@@ -344,7 +330,12 @@ fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
344330 if before == Some ( b"" ) {
345331 ( Some ( file) , None )
346332 } else {
347- unsafe { ( before. map ( |s| u8_slice_as_os_str ( s) ) , after. map ( |s| u8_slice_as_os_str ( s) ) ) }
333+ unsafe {
334+ (
335+ before. map ( |s| OsStr :: from_os_str_bytes_unchecked ( s) ) ,
336+ after. map ( |s| OsStr :: from_os_str_bytes_unchecked ( s) ) ,
337+ )
338+ }
348339 }
349340}
350341
@@ -364,7 +355,12 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
364355 } ;
365356 let before = & slice[ ..i] ;
366357 let after = & slice[ i + 1 ..] ;
367- unsafe { ( u8_slice_as_os_str ( before) , Some ( u8_slice_as_os_str ( after) ) ) }
358+ unsafe {
359+ (
360+ OsStr :: from_os_str_bytes_unchecked ( before) ,
361+ Some ( OsStr :: from_os_str_bytes_unchecked ( after) ) ,
362+ )
363+ }
368364}
369365
370366////////////////////////////////////////////////////////////////////////////////
@@ -743,7 +739,7 @@ impl<'a> Components<'a> {
743739 // separately via `include_cur_dir`
744740 b".." => Some ( Component :: ParentDir ) ,
745741 b"" => None ,
746- _ => Some ( Component :: Normal ( unsafe { u8_slice_as_os_str ( comp) } ) ) ,
742+ _ => Some ( Component :: Normal ( unsafe { OsStr :: from_os_str_bytes_unchecked ( comp) } ) ) ,
747743 }
748744 }
749745
@@ -900,7 +896,7 @@ impl<'a> Iterator for Components<'a> {
900896 let raw = & self . path [ ..self . prefix_len ( ) ] ;
901897 self . path = & self . path [ self . prefix_len ( ) ..] ;
902898 return Some ( Component :: Prefix ( PrefixComponent {
903- raw : unsafe { u8_slice_as_os_str ( raw) } ,
899+ raw : unsafe { OsStr :: from_os_str_bytes_unchecked ( raw) } ,
904900 parsed : self . prefix . unwrap ( ) ,
905901 } ) ) ;
906902 }
@@ -972,7 +968,7 @@ impl<'a> DoubleEndedIterator for Components<'a> {
972968 State :: Prefix if self . prefix_len ( ) > 0 => {
973969 self . back = State :: Done ;
974970 return Some ( Component :: Prefix ( PrefixComponent {
975- raw : unsafe { u8_slice_as_os_str ( self . path ) } ,
971+ raw : unsafe { OsStr :: from_os_str_bytes_unchecked ( self . path ) } ,
976972 parsed : self . prefix . unwrap ( ) ,
977973 } ) ) ;
978974 }
@@ -2011,7 +2007,7 @@ impl Path {
20112007 // The following (private!) function allows construction of a path from a u8
20122008 // slice, which is only safe when it is known to follow the OsStr encoding.
20132009 unsafe fn from_u8_slice ( s : & [ u8 ] ) -> & Path {
2014- unsafe { Path :: new ( u8_slice_as_os_str ( s) ) }
2010+ unsafe { Path :: new ( OsStr :: from_os_str_bytes_unchecked ( s) ) }
20152011 }
20162012 // The following (private!) function reveals the byte encoding used for OsStr.
20172013 fn as_u8_slice ( & self ) -> & [ u8 ] {
0 commit comments