@@ -193,7 +193,7 @@ impl<'a> Prefix<'a> {
193193 fn len ( & self ) -> usize {
194194 use self :: Prefix :: * ;
195195 fn os_str_len ( s : & OsStr ) -> usize {
196- s. bytes ( ) . len ( )
196+ s. as_os_str_bytes ( ) . len ( )
197197 }
198198 match * self {
199199 Verbatim ( x) => 4 + os_str_len ( x) ,
@@ -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':' )
@@ -330,26 +316,31 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
330316
331317// basic workhorse for splitting stem and extension
332318fn rsplit_file_at_dot ( file : & OsStr ) -> ( Option < & OsStr > , Option < & OsStr > ) {
333- if file. bytes ( ) == b".." {
319+ if file. as_os_str_bytes ( ) == b".." {
334320 return ( Some ( file) , None ) ;
335321 }
336322
337323 // The unsafety here stems from converting between &OsStr and &[u8]
338324 // and back. This is safe to do because (1) we only look at ASCII
339325 // contents of the encoding and (2) new &OsStr values are produced
340326 // only from ASCII-bounded slices of existing &OsStr values.
341- let mut iter = file. bytes ( ) . rsplitn ( 2 , |b| * b == b'.' ) ;
327+ let mut iter = file. as_os_str_bytes ( ) . rsplitn ( 2 , |b| * b == b'.' ) ;
342328 let after = iter. next ( ) ;
343329 let before = iter. next ( ) ;
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
351342fn split_file_at_dot ( file : & OsStr ) -> ( & OsStr , Option < & OsStr > ) {
352- let slice = file. bytes ( ) ;
343+ let slice = file. as_os_str_bytes ( ) ;
353344 if slice == b".." {
354345 return ( file, None ) ;
355346 }
@@ -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 }
@@ -1481,17 +1477,17 @@ impl PathBuf {
14811477 fn _set_extension ( & mut self , extension : & OsStr ) -> bool {
14821478 let file_stem = match self . file_stem ( ) {
14831479 None => return false ,
1484- Some ( f) => f. bytes ( ) ,
1480+ Some ( f) => f. as_os_str_bytes ( ) ,
14851481 } ;
14861482
14871483 // truncate until right after the file stem
14881484 let end_file_stem = file_stem[ file_stem. len ( ) ..] . as_ptr ( ) . addr ( ) ;
1489- let start = self . inner . bytes ( ) . as_ptr ( ) . addr ( ) ;
1485+ let start = self . inner . as_os_str_bytes ( ) . as_ptr ( ) . addr ( ) ;
14901486 let v = self . as_mut_vec ( ) ;
14911487 v. truncate ( end_file_stem. wrapping_sub ( start) ) ;
14921488
14931489 // add the new extension, if any
1494- let new = extension. bytes ( ) ;
1490+ let new = extension. as_os_str_bytes ( ) ;
14951491 if !new. is_empty ( ) {
14961492 v. reserve_exact ( new. len ( ) + 1 ) ;
14971493 v. push ( b'.' ) ;
@@ -2011,11 +2007,11 @@ 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 ] {
2018- self . inner . bytes ( )
2014+ self . inner . as_os_str_bytes ( )
20192015 }
20202016
20212017 /// Directly wraps a string slice as a `Path` slice.
0 commit comments