summaryrefslogtreecommitdiffstats
path: root/library/std/src/path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/path.rs')
-rw-r--r--library/std/src/path.rs52
1 files changed, 24 insertions, 28 deletions
diff --git a/library/std/src/path.rs b/library/std/src/path.rs
index febdeb514..28cd3c4e4 100644
--- a/library/std/src/path.rs
+++ b/library/std/src/path.rs
@@ -193,7 +193,7 @@ impl<'a> Prefix<'a> {
fn len(&self) -> usize {
use self::Prefix::*;
fn os_str_len(s: &OsStr) -> usize {
- s.bytes().len()
+ s.as_os_str_bytes().len()
}
match *self {
Verbatim(x) => 4 + os_str_len(x),
@@ -299,20 +299,6 @@ where
}
}
-unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
- // SAFETY: See note at the top of this module to understand why this and
- // `OsStr::bytes` are used:
- //
- // This casts are safe as OsStr is internally a wrapper around [u8] on all
- // platforms.
- //
- // Note that currently this relies on the special knowledge that std has;
- // these types are single-element structs but are not marked
- // repr(transparent) or repr(C) which would make these casts not allowable
- // outside std.
- unsafe { &*(s as *const [u8] as *const OsStr) }
-}
-
// Detect scheme on Redox
fn has_redox_scheme(s: &[u8]) -> bool {
cfg!(target_os = "redox") && s.contains(&b':')
@@ -330,7 +316,7 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
// basic workhorse for splitting stem and extension
fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
- if file.bytes() == b".." {
+ if file.as_os_str_bytes() == b".." {
return (Some(file), None);
}
@@ -338,18 +324,23 @@ fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
// and back. This is safe to do because (1) we only look at ASCII
// contents of the encoding and (2) new &OsStr values are produced
// only from ASCII-bounded slices of existing &OsStr values.
- let mut iter = file.bytes().rsplitn(2, |b| *b == b'.');
+ let mut iter = file.as_os_str_bytes().rsplitn(2, |b| *b == b'.');
let after = iter.next();
let before = iter.next();
if before == Some(b"") {
(Some(file), None)
} else {
- unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
+ unsafe {
+ (
+ before.map(|s| OsStr::from_os_str_bytes_unchecked(s)),
+ after.map(|s| OsStr::from_os_str_bytes_unchecked(s)),
+ )
+ }
}
}
fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
- let slice = file.bytes();
+ let slice = file.as_os_str_bytes();
if slice == b".." {
return (file, None);
}
@@ -364,7 +355,12 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
};
let before = &slice[..i];
let after = &slice[i + 1..];
- unsafe { (u8_slice_as_os_str(before), Some(u8_slice_as_os_str(after))) }
+ unsafe {
+ (
+ OsStr::from_os_str_bytes_unchecked(before),
+ Some(OsStr::from_os_str_bytes_unchecked(after)),
+ )
+ }
}
////////////////////////////////////////////////////////////////////////////////
@@ -743,7 +739,7 @@ impl<'a> Components<'a> {
// separately via `include_cur_dir`
b".." => Some(Component::ParentDir),
b"" => None,
- _ => Some(Component::Normal(unsafe { u8_slice_as_os_str(comp) })),
+ _ => Some(Component::Normal(unsafe { OsStr::from_os_str_bytes_unchecked(comp) })),
}
}
@@ -900,7 +896,7 @@ impl<'a> Iterator for Components<'a> {
let raw = &self.path[..self.prefix_len()];
self.path = &self.path[self.prefix_len()..];
return Some(Component::Prefix(PrefixComponent {
- raw: unsafe { u8_slice_as_os_str(raw) },
+ raw: unsafe { OsStr::from_os_str_bytes_unchecked(raw) },
parsed: self.prefix.unwrap(),
}));
}
@@ -972,7 +968,7 @@ impl<'a> DoubleEndedIterator for Components<'a> {
State::Prefix if self.prefix_len() > 0 => {
self.back = State::Done;
return Some(Component::Prefix(PrefixComponent {
- raw: unsafe { u8_slice_as_os_str(self.path) },
+ raw: unsafe { OsStr::from_os_str_bytes_unchecked(self.path) },
parsed: self.prefix.unwrap(),
}));
}
@@ -1481,17 +1477,17 @@ impl PathBuf {
fn _set_extension(&mut self, extension: &OsStr) -> bool {
let file_stem = match self.file_stem() {
None => return false,
- Some(f) => f.bytes(),
+ Some(f) => f.as_os_str_bytes(),
};
// truncate until right after the file stem
let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
- let start = self.inner.bytes().as_ptr().addr();
+ let start = self.inner.as_os_str_bytes().as_ptr().addr();
let v = self.as_mut_vec();
v.truncate(end_file_stem.wrapping_sub(start));
// add the new extension, if any
- let new = extension.bytes();
+ let new = extension.as_os_str_bytes();
if !new.is_empty() {
v.reserve_exact(new.len() + 1);
v.push(b'.');
@@ -2011,11 +2007,11 @@ impl Path {
// The following (private!) function allows construction of a path from a u8
// slice, which is only safe when it is known to follow the OsStr encoding.
unsafe fn from_u8_slice(s: &[u8]) -> &Path {
- unsafe { Path::new(u8_slice_as_os_str(s)) }
+ unsafe { Path::new(OsStr::from_os_str_bytes_unchecked(s)) }
}
// The following (private!) function reveals the byte encoding used for OsStr.
fn as_u8_slice(&self) -> &[u8] {
- self.inner.bytes()
+ self.inner.as_os_str_bytes()
}
/// Directly wraps a string slice as a `Path` slice.