@@ -193,7 +193,7 @@ impl<'a> Prefix<'a> {
193
193
fn len ( & self ) -> usize {
194
194
use self :: Prefix :: * ;
195
195
fn os_str_len ( s : & OsStr ) -> usize {
196
- s. bytes ( ) . len ( )
196
+ s. as_os_str_bytes ( ) . len ( )
197
197
}
198
198
match * self {
199
199
Verbatim ( x) => 4 + os_str_len ( x) ,
@@ -299,20 +299,6 @@ where
299
299
}
300
300
}
301
301
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
-
316
302
// Detect scheme on Redox
317
303
fn has_redox_scheme ( s : & [ u8 ] ) -> bool {
318
304
cfg ! ( target_os = "redox" ) && s. contains ( & b':' )
@@ -330,26 +316,31 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
330
316
331
317
// basic workhorse for splitting stem and extension
332
318
fn rsplit_file_at_dot ( file : & OsStr ) -> ( Option < & OsStr > , Option < & OsStr > ) {
333
- if file. bytes ( ) == b".." {
319
+ if file. as_os_str_bytes ( ) == b".." {
334
320
return ( Some ( file) , None ) ;
335
321
}
336
322
337
323
// The unsafety here stems from converting between &OsStr and &[u8]
338
324
// and back. This is safe to do because (1) we only look at ASCII
339
325
// contents of the encoding and (2) new &OsStr values are produced
340
326
// 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'.' ) ;
342
328
let after = iter. next ( ) ;
343
329
let before = iter. next ( ) ;
344
330
if before == Some ( b"" ) {
345
331
( Some ( file) , None )
346
332
} 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
+ }
348
339
}
349
340
}
350
341
351
342
fn split_file_at_dot ( file : & OsStr ) -> ( & OsStr , Option < & OsStr > ) {
352
- let slice = file. bytes ( ) ;
343
+ let slice = file. as_os_str_bytes ( ) ;
353
344
if slice == b".." {
354
345
return ( file, None ) ;
355
346
}
@@ -364,7 +355,12 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
364
355
} ;
365
356
let before = & slice[ ..i] ;
366
357
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
+ }
368
364
}
369
365
370
366
////////////////////////////////////////////////////////////////////////////////
@@ -743,7 +739,7 @@ impl<'a> Components<'a> {
743
739
// separately via `include_cur_dir`
744
740
b".." => Some ( Component :: ParentDir ) ,
745
741
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) } ) ) ,
747
743
}
748
744
}
749
745
@@ -900,7 +896,7 @@ impl<'a> Iterator for Components<'a> {
900
896
let raw = & self . path [ ..self . prefix_len ( ) ] ;
901
897
self . path = & self . path [ self . prefix_len ( ) ..] ;
902
898
return Some ( Component :: Prefix ( PrefixComponent {
903
- raw : unsafe { u8_slice_as_os_str ( raw) } ,
899
+ raw : unsafe { OsStr :: from_os_str_bytes_unchecked ( raw) } ,
904
900
parsed : self . prefix . unwrap ( ) ,
905
901
} ) ) ;
906
902
}
@@ -972,7 +968,7 @@ impl<'a> DoubleEndedIterator for Components<'a> {
972
968
State :: Prefix if self . prefix_len ( ) > 0 => {
973
969
self . back = State :: Done ;
974
970
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 ) } ,
976
972
parsed : self . prefix . unwrap ( ) ,
977
973
} ) ) ;
978
974
}
@@ -1481,17 +1477,17 @@ impl PathBuf {
1481
1477
fn _set_extension ( & mut self , extension : & OsStr ) -> bool {
1482
1478
let file_stem = match self . file_stem ( ) {
1483
1479
None => return false ,
1484
- Some ( f) => f. bytes ( ) ,
1480
+ Some ( f) => f. as_os_str_bytes ( ) ,
1485
1481
} ;
1486
1482
1487
1483
// truncate until right after the file stem
1488
1484
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 ( ) ;
1490
1486
let v = self . as_mut_vec ( ) ;
1491
1487
v. truncate ( end_file_stem. wrapping_sub ( start) ) ;
1492
1488
1493
1489
// add the new extension, if any
1494
- let new = extension. bytes ( ) ;
1490
+ let new = extension. as_os_str_bytes ( ) ;
1495
1491
if !new. is_empty ( ) {
1496
1492
v. reserve_exact ( new. len ( ) + 1 ) ;
1497
1493
v. push ( b'.' ) ;
@@ -2011,11 +2007,11 @@ impl Path {
2011
2007
// The following (private!) function allows construction of a path from a u8
2012
2008
// slice, which is only safe when it is known to follow the OsStr encoding.
2013
2009
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) ) }
2015
2011
}
2016
2012
// The following (private!) function reveals the byte encoding used for OsStr.
2017
2013
fn as_u8_slice ( & self ) -> & [ u8 ] {
2018
- self . inner . bytes ( )
2014
+ self . inner . as_os_str_bytes ( )
2019
2015
}
2020
2016
2021
2017
/// Directly wraps a string slice as a `Path` slice.
0 commit comments