@@ -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
- os_str_as_u8_slice ( s ) . len ( )
196
+ s . bytes ( ) . len ( )
197
197
}
198
198
match * self {
199
199
Verbatim ( x) => 4 + os_str_len ( x) ,
@@ -299,19 +299,17 @@ where
299
299
}
300
300
}
301
301
302
- // See note at the top of this module to understand why these are used:
303
- //
304
- // These casts are safe as OsStr is internally a wrapper around [u8] on all
305
- // platforms.
306
- //
307
- // Note that currently this relies on the special knowledge that libstd has;
308
- // these types are single-element structs but are not marked repr(transparent)
309
- // or repr(C) which would make these casts allowable outside std.
310
- fn os_str_as_u8_slice ( s : & OsStr ) -> & [ u8 ] {
311
- unsafe { & * ( s as * const OsStr as * const [ u8 ] ) }
312
- }
313
302
unsafe fn u8_slice_as_os_str ( s : & [ u8 ] ) -> & OsStr {
314
- // SAFETY: see the comment of `os_str_as_u8_slice`
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 libstd 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.
315
313
unsafe { & * ( s as * const [ u8 ] as * const OsStr ) }
316
314
}
317
315
@@ -332,15 +330,15 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
332
330
333
331
// basic workhorse for splitting stem and extension
334
332
fn rsplit_file_at_dot ( file : & OsStr ) -> ( Option < & OsStr > , Option < & OsStr > ) {
335
- if os_str_as_u8_slice ( file) == b".." {
333
+ if file. bytes ( ) == b".." {
336
334
return ( Some ( file) , None ) ;
337
335
}
338
336
339
337
// The unsafety here stems from converting between &OsStr and &[u8]
340
338
// and back. This is safe to do because (1) we only look at ASCII
341
339
// contents of the encoding and (2) new &OsStr values are produced
342
340
// only from ASCII-bounded slices of existing &OsStr values.
343
- let mut iter = os_str_as_u8_slice ( file) . rsplitn ( 2 , |b| * b == b'.' ) ;
341
+ let mut iter = file. bytes ( ) . rsplitn ( 2 , |b| * b == b'.' ) ;
344
342
let after = iter. next ( ) ;
345
343
let before = iter. next ( ) ;
346
344
if before == Some ( b"" ) {
@@ -351,7 +349,7 @@ fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
351
349
}
352
350
353
351
fn split_file_at_dot ( file : & OsStr ) -> ( & OsStr , Option < & OsStr > ) {
354
- let slice = os_str_as_u8_slice ( file) ;
352
+ let slice = file. bytes ( ) ;
355
353
if slice == b".." {
356
354
return ( file, None ) ;
357
355
}
@@ -1445,17 +1443,17 @@ impl PathBuf {
1445
1443
fn _set_extension ( & mut self , extension : & OsStr ) -> bool {
1446
1444
let file_stem = match self . file_stem ( ) {
1447
1445
None => return false ,
1448
- Some ( f) => os_str_as_u8_slice ( f ) ,
1446
+ Some ( f) => f . bytes ( ) ,
1449
1447
} ;
1450
1448
1451
1449
// truncate until right after the file stem
1452
1450
let end_file_stem = file_stem[ file_stem. len ( ) ..] . as_ptr ( ) . addr ( ) ;
1453
- let start = os_str_as_u8_slice ( & self . inner ) . as_ptr ( ) . addr ( ) ;
1451
+ let start = self . inner . bytes ( ) . as_ptr ( ) . addr ( ) ;
1454
1452
let v = self . as_mut_vec ( ) ;
1455
1453
v. truncate ( end_file_stem. wrapping_sub ( start) ) ;
1456
1454
1457
1455
// add the new extension, if any
1458
- let new = os_str_as_u8_slice ( extension) ;
1456
+ let new = extension. bytes ( ) ;
1459
1457
if !new. is_empty ( ) {
1460
1458
v. reserve_exact ( new. len ( ) + 1 ) ;
1461
1459
v. push ( b'.' ) ;
@@ -1948,7 +1946,7 @@ impl Path {
1948
1946
}
1949
1947
// The following (private!) function reveals the byte encoding used for OsStr.
1950
1948
fn as_u8_slice ( & self ) -> & [ u8 ] {
1951
- os_str_as_u8_slice ( & self . inner )
1949
+ self . inner . bytes ( )
1952
1950
}
1953
1951
1954
1952
/// Directly wraps a string slice as a `Path` slice.
0 commit comments