diff options
| author | Ralf Jung <post@ralfj.de> | 2023-09-03 09:28:31 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-09-03 09:28:31 +0200 |
| commit | fb26c21c356075c0cd35d98fa0b786b7633249d8 (patch) | |
| tree | 62acd3e15506a4b53b29a70b7882c0c2fa0352b4 /library/std/src | |
| parent | dc81d6c2827bb23e75f1dc6c42e9c705ed976ba1 (diff) | |
| parent | a989e25f1b87949a886eab3da10324d14189fe95 (diff) | |
| download | rust-fb26c21c356075c0cd35d98fa0b786b7633249d8.tar.gz rust-fb26c21c356075c0cd35d98fa0b786b7633249d8.zip | |
Merge from rustc
Diffstat (limited to 'library/std/src')
| -rw-r--r-- | library/std/src/ffi/mod.rs | 4 | ||||
| -rw-r--r-- | library/std/src/ffi/os_str.rs | 68 | ||||
| -rw-r--r-- | library/std/src/fs.rs | 12 | ||||
| -rw-r--r-- | library/std/src/path.rs | 34 | ||||
| -rw-r--r-- | library/std/src/sys/common/small_c_string.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/common/tests.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/solid/error.rs | 5 | ||||
| -rw-r--r-- | library/std/src/sys/solid/mod.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/solid/net.rs | 3 | ||||
| -rw-r--r-- | library/std/src/sys/unix/os_str.rs | 10 | ||||
| -rw-r--r-- | library/std/src/sys/unix/os_str/tests.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/unix/path.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/unix/process/process_common.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/windows/args.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/windows/os_str.rs | 8 | ||||
| -rw-r--r-- | library/std/src/sys/windows/path.rs | 28 | ||||
| -rw-r--r-- | library/std/src/sys/windows/process.rs | 4 |
17 files changed, 95 insertions, 103 deletions
diff --git a/library/std/src/ffi/mod.rs b/library/std/src/ffi/mod.rs index ee9f6ed087c..97e78d17786 100644 --- a/library/std/src/ffi/mod.rs +++ b/library/std/src/ffi/mod.rs @@ -132,8 +132,8 @@ //! On all platforms, [`OsStr`] consists of a sequence of bytes that is encoded as a superset of //! UTF-8; see [`OsString`] for more details on its encoding on different platforms. //! -//! For limited, inexpensive conversions from and to bytes, see [`OsStr::as_os_str_bytes`] and -//! [`OsStr::from_os_str_bytes_unchecked`]. +//! For limited, inexpensive conversions from and to bytes, see [`OsStr::as_encoded_bytes`] and +//! [`OsStr::from_encoded_bytes_unchecked`]. //! //! [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value //! [Unicode code point]: https://www.unicode.org/glossary/#code_point diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 43cecb19b14..93b9bf0cc20 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -154,36 +154,34 @@ impl OsString { /// # Safety /// /// As the encoding is unspecified, callers must pass in bytes that originated as a mixture of - /// validated UTF-8 and bytes from [`OsStr::as_os_str_bytes`] from within the same rust version + /// validated UTF-8 and bytes from [`OsStr::as_encoded_bytes`] from within the same rust version /// built for the same target platform. For example, reconstructing an `OsString` from bytes sent /// over the network or stored in a file will likely violate these safety rules. /// - /// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_os_str_bytes`] can be + /// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_encoded_bytes`] can be /// split either immediately before or immediately after any valid non-empty UTF-8 substring. /// /// # Example /// /// ``` - /// #![feature(os_str_bytes)] - /// /// use std::ffi::OsStr; /// /// let os_str = OsStr::new("Mary had a little lamb"); - /// let bytes = os_str.as_os_str_bytes(); + /// let bytes = os_str.as_encoded_bytes(); /// let words = bytes.split(|b| *b == b' '); /// let words: Vec<&OsStr> = words.map(|word| { /// // SAFETY: - /// // - Each `word` only contains content that originated from `OsStr::as_os_str_bytes` + /// // - Each `word` only contains content that originated from `OsStr::as_encoded_bytes` /// // - Only split with ASCII whitespace which is a non-empty UTF-8 substring - /// unsafe { OsStr::from_os_str_bytes_unchecked(word) } + /// unsafe { OsStr::from_encoded_bytes_unchecked(word) } /// }).collect(); /// ``` /// /// [conversions]: super#conversions #[inline] - #[unstable(feature = "os_str_bytes", issue = "111544")] - pub unsafe fn from_os_str_bytes_unchecked(bytes: Vec<u8>) -> Self { - OsString { inner: Buf::from_os_str_bytes_unchecked(bytes) } + #[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")] + pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec<u8>) -> Self { + OsString { inner: Buf::from_encoded_bytes_unchecked(bytes) } } /// Converts to an [`OsStr`] slice. @@ -205,7 +203,7 @@ impl OsString { } /// Converts the `OsString` into a byte slice. To convert the byte slice back into an - /// `OsString`, use the [`OsStr::from_os_str_bytes_unchecked`] function. + /// `OsString`, use the [`OsStr::from_encoded_bytes_unchecked`] function. /// /// The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. /// By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit @@ -219,9 +217,9 @@ impl OsString { /// /// [`std::ffi`]: crate::ffi #[inline] - #[unstable(feature = "os_str_bytes", issue = "111544")] - pub fn into_os_str_bytes(self) -> Vec<u8> { - self.inner.into_os_str_bytes() + #[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")] + pub fn into_encoded_bytes(self) -> Vec<u8> { + self.inner.into_encoded_bytes() } /// Converts the `OsString` into a [`String`] if it contains valid Unicode data. @@ -745,36 +743,34 @@ impl OsStr { /// # Safety /// /// As the encoding is unspecified, callers must pass in bytes that originated as a mixture of - /// validated UTF-8 and bytes from [`OsStr::as_os_str_bytes`] from within the same rust version + /// validated UTF-8 and bytes from [`OsStr::as_encoded_bytes`] from within the same rust version /// built for the same target platform. For example, reconstructing an `OsStr` from bytes sent /// over the network or stored in a file will likely violate these safety rules. /// - /// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_os_str_bytes`] can be + /// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_encoded_bytes`] can be /// split either immediately before or immediately after any valid non-empty UTF-8 substring. /// /// # Example /// /// ``` - /// #![feature(os_str_bytes)] - /// /// use std::ffi::OsStr; /// /// let os_str = OsStr::new("Mary had a little lamb"); - /// let bytes = os_str.as_os_str_bytes(); + /// let bytes = os_str.as_encoded_bytes(); /// let words = bytes.split(|b| *b == b' '); /// let words: Vec<&OsStr> = words.map(|word| { /// // SAFETY: - /// // - Each `word` only contains content that originated from `OsStr::as_os_str_bytes` + /// // - Each `word` only contains content that originated from `OsStr::as_encoded_bytes` /// // - Only split with ASCII whitespace which is a non-empty UTF-8 substring - /// unsafe { OsStr::from_os_str_bytes_unchecked(word) } + /// unsafe { OsStr::from_encoded_bytes_unchecked(word) } /// }).collect(); /// ``` /// /// [conversions]: super#conversions #[inline] - #[unstable(feature = "os_str_bytes", issue = "111544")] - pub unsafe fn from_os_str_bytes_unchecked(bytes: &[u8]) -> &Self { - Self::from_inner(Slice::from_os_str_bytes_unchecked(bytes)) + #[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")] + pub unsafe fn from_encoded_bytes_unchecked(bytes: &[u8]) -> &Self { + Self::from_inner(Slice::from_encoded_bytes_unchecked(bytes)) } #[inline] @@ -948,7 +944,7 @@ impl OsStr { } /// Converts an OS string slice to a byte slice. To convert the byte slice back into an OS - /// string slice, use the [`OsStr::from_os_str_bytes_unchecked`] function. + /// string slice, use the [`OsStr::from_encoded_bytes_unchecked`] function. /// /// The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. /// By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit @@ -962,9 +958,9 @@ impl OsStr { /// /// [`std::ffi`]: crate::ffi #[inline] - #[unstable(feature = "os_str_bytes", issue = "111544")] - pub fn as_os_str_bytes(&self) -> &[u8] { - self.inner.as_os_str_bytes() + #[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")] + pub fn as_encoded_bytes(&self) -> &[u8] { + self.inner.as_encoded_bytes() } /// Converts this string to its ASCII lower case equivalent in-place. @@ -1270,7 +1266,7 @@ impl Default for &OsStr { impl PartialEq for OsStr { #[inline] fn eq(&self, other: &OsStr) -> bool { - self.as_os_str_bytes().eq(other.as_os_str_bytes()) + self.as_encoded_bytes().eq(other.as_encoded_bytes()) } } @@ -1297,23 +1293,23 @@ impl Eq for OsStr {} impl PartialOrd for OsStr { #[inline] fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> { - self.as_os_str_bytes().partial_cmp(other.as_os_str_bytes()) + self.as_encoded_bytes().partial_cmp(other.as_encoded_bytes()) } #[inline] fn lt(&self, other: &OsStr) -> bool { - self.as_os_str_bytes().lt(other.as_os_str_bytes()) + self.as_encoded_bytes().lt(other.as_encoded_bytes()) } #[inline] fn le(&self, other: &OsStr) -> bool { - self.as_os_str_bytes().le(other.as_os_str_bytes()) + self.as_encoded_bytes().le(other.as_encoded_bytes()) } #[inline] fn gt(&self, other: &OsStr) -> bool { - self.as_os_str_bytes().gt(other.as_os_str_bytes()) + self.as_encoded_bytes().gt(other.as_encoded_bytes()) } #[inline] fn ge(&self, other: &OsStr) -> bool { - self.as_os_str_bytes().ge(other.as_os_str_bytes()) + self.as_encoded_bytes().ge(other.as_encoded_bytes()) } } @@ -1332,7 +1328,7 @@ impl PartialOrd<str> for OsStr { impl Ord for OsStr { #[inline] fn cmp(&self, other: &OsStr) -> cmp::Ordering { - self.as_os_str_bytes().cmp(other.as_os_str_bytes()) + self.as_encoded_bytes().cmp(other.as_encoded_bytes()) } } @@ -1382,7 +1378,7 @@ impl_cmp!(Cow<'a, OsStr>, OsString); impl Hash for OsStr { #[inline] fn hash<H: Hasher>(&self, state: &mut H) { - self.as_os_str_bytes().hash(state) + self.as_encoded_bytes().hash(state) } } diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 3c67bea7a22..adc1b85fd85 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -233,8 +233,8 @@ pub struct DirBuilder { /// This function will return an error if `path` does not already exist. /// Other errors may also be returned according to [`OpenOptions::open`]. /// -/// It will also return an error if it encounters while reading an error -/// of a kind other than [`io::ErrorKind::Interrupted`]. +/// While reading from the file, this function handles [`io::ErrorKind::Interrupted`] +/// with automatic retries. See [io::Read] documentation for details. /// /// # Examples /// @@ -271,9 +271,11 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> { /// This function will return an error if `path` does not already exist. /// Other errors may also be returned according to [`OpenOptions::open`]. /// -/// It will also return an error if it encounters while reading an error -/// of a kind other than [`io::ErrorKind::Interrupted`], -/// or if the contents of the file are not valid UTF-8. +/// If the contents of the file are not valid UTF-8, then an error will also be +/// returned. +/// +/// While reading from the file, this function handles [`io::ErrorKind::Interrupted`] +/// with automatic retries. See [io::Read] documentation for details. /// /// # Examples /// diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 5842c096f1a..60562f64c90 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.as_os_str_bytes().len() + s.as_encoded_bytes().len() } match *self { Verbatim(x) => 4 + os_str_len(x), @@ -316,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.as_os_str_bytes() == b".." { + if file.as_encoded_bytes() == b".." { return (Some(file), None); } @@ -324,7 +324,7 @@ 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.as_os_str_bytes().rsplitn(2, |b| *b == b'.'); + let mut iter = file.as_encoded_bytes().rsplitn(2, |b| *b == b'.'); let after = iter.next(); let before = iter.next(); if before == Some(b"") { @@ -332,15 +332,15 @@ fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) { } else { unsafe { ( - before.map(|s| OsStr::from_os_str_bytes_unchecked(s)), - after.map(|s| OsStr::from_os_str_bytes_unchecked(s)), + before.map(|s| OsStr::from_encoded_bytes_unchecked(s)), + after.map(|s| OsStr::from_encoded_bytes_unchecked(s)), ) } } } fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) { - let slice = file.as_os_str_bytes(); + let slice = file.as_encoded_bytes(); if slice == b".." { return (file, None); } @@ -357,8 +357,8 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) { let after = &slice[i + 1..]; unsafe { ( - OsStr::from_os_str_bytes_unchecked(before), - Some(OsStr::from_os_str_bytes_unchecked(after)), + OsStr::from_encoded_bytes_unchecked(before), + Some(OsStr::from_encoded_bytes_unchecked(after)), ) } } @@ -739,7 +739,7 @@ impl<'a> Components<'a> { // separately via `include_cur_dir` b".." => Some(Component::ParentDir), b"" => None, - _ => Some(Component::Normal(unsafe { OsStr::from_os_str_bytes_unchecked(comp) })), + _ => Some(Component::Normal(unsafe { OsStr::from_encoded_bytes_unchecked(comp) })), } } @@ -896,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 { OsStr::from_os_str_bytes_unchecked(raw) }, + raw: unsafe { OsStr::from_encoded_bytes_unchecked(raw) }, parsed: self.prefix.unwrap(), })); } @@ -968,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 { OsStr::from_os_str_bytes_unchecked(self.path) }, + raw: unsafe { OsStr::from_encoded_bytes_unchecked(self.path) }, parsed: self.prefix.unwrap(), })); } @@ -1477,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.as_os_str_bytes(), + Some(f) => f.as_encoded_bytes(), }; // truncate until right after the file stem let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr(); - let start = self.inner.as_os_str_bytes().as_ptr().addr(); + let start = self.inner.as_encoded_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.as_os_str_bytes(); + let new = extension.as_encoded_bytes(); if !new.is_empty() { v.reserve_exact(new.len() + 1); v.push(b'.'); @@ -2007,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(OsStr::from_os_str_bytes_unchecked(s)) } + unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) } } // The following (private!) function reveals the byte encoding used for OsStr. fn as_u8_slice(&self) -> &[u8] { - self.inner.as_os_str_bytes() + self.inner.as_encoded_bytes() } /// Directly wraps a string slice as a `Path` slice. @@ -2609,7 +2609,7 @@ impl Path { fn _with_extension(&self, extension: &OsStr) -> PathBuf { let self_len = self.as_os_str().len(); - let self_bytes = self.as_os_str().as_os_str_bytes(); + let self_bytes = self.as_os_str().as_encoded_bytes(); let (new_capacity, slice_to_copy) = match self.extension() { None => { diff --git a/library/std/src/sys/common/small_c_string.rs b/library/std/src/sys/common/small_c_string.rs index 963d17a47e4..af9b18e372d 100644 --- a/library/std/src/sys/common/small_c_string.rs +++ b/library/std/src/sys/common/small_c_string.rs @@ -19,7 +19,7 @@ pub fn run_path_with_cstr<T, F>(path: &Path, f: F) -> io::Result<T> where F: FnOnce(&CStr) -> io::Result<T>, { - run_with_cstr(path.as_os_str().as_os_str_bytes(), f) + run_with_cstr(path.as_os_str().as_encoded_bytes(), f) } #[inline] diff --git a/library/std/src/sys/common/tests.rs b/library/std/src/sys/common/tests.rs index 0a1cbcbe8ef..32dc18ee1cf 100644 --- a/library/std/src/sys/common/tests.rs +++ b/library/std/src/sys/common/tests.rs @@ -8,7 +8,7 @@ use core::iter::repeat; fn stack_allocation_works() { let path = Path::new("abc"); let result = run_path_with_cstr(path, |p| { - assert_eq!(p, &*CString::new(path.as_os_str().as_os_str_bytes()).unwrap()); + assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap()); Ok(42) }); assert_eq!(result.unwrap(), 42); @@ -25,7 +25,7 @@ fn heap_allocation_works() { let path = repeat("a").take(384).collect::<String>(); let path = Path::new(&path); let result = run_path_with_cstr(path, |p| { - assert_eq!(p, &*CString::new(path.as_os_str().as_os_str_bytes()).unwrap()); + assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap()); Ok(42) }); assert_eq!(result.unwrap(), 42); diff --git a/library/std/src/sys/solid/error.rs b/library/std/src/sys/solid/error.rs index d1877a8bcd2..547b4f3a984 100644 --- a/library/std/src/sys/solid/error.rs +++ b/library/std/src/sys/solid/error.rs @@ -31,11 +31,6 @@ pub fn error_name(er: abi::ER) -> Option<&'static str> { } } -#[inline] -fn is_interrupted(er: abi::ER) -> bool { - false -} - pub fn decode_error_kind(er: abi::ER) -> ErrorKind { match er { // Success diff --git a/library/std/src/sys/solid/mod.rs b/library/std/src/sys/solid/mod.rs index e7029174511..5af83653cf8 100644 --- a/library/std/src/sys/solid/mod.rs +++ b/library/std/src/sys/solid/mod.rs @@ -74,7 +74,7 @@ pub fn unsupported_err() -> crate::io::Error { #[inline] pub fn is_interrupted(code: i32) -> bool { - error::is_interrupted(code) + net::is_interrupted(code) } pub fn decode_error_kind(code: i32) -> crate::io::ErrorKind { diff --git a/library/std/src/sys/solid/net.rs b/library/std/src/sys/solid/net.rs index bdd64ab02b7..6adced787f3 100644 --- a/library/std/src/sys/solid/net.rs +++ b/library/std/src/sys/solid/net.rs @@ -183,8 +183,7 @@ pub(super) fn error_name(er: abi::ER) -> Option<&'static str> { #[inline] pub fn is_interrupted(er: abi::ER) -> bool { - let errno = netc::SOLID_NET_ERR_BASE - er; - errno as libc::c_int == libc::EINTR + er == netc::SOLID_NET_ERR_BASE - libc::EINTR } pub(super) fn decode_error_kind(er: abi::ER) -> ErrorKind { diff --git a/library/std/src/sys/unix/os_str.rs b/library/std/src/sys/unix/os_str.rs index 463b0a27515..7bd2f656a24 100644 --- a/library/std/src/sys/unix/os_str.rs +++ b/library/std/src/sys/unix/os_str.rs @@ -97,12 +97,12 @@ impl AsInner<[u8]> for Buf { impl Buf { #[inline] - pub fn into_os_str_bytes(self) -> Vec<u8> { + pub fn into_encoded_bytes(self) -> Vec<u8> { self.inner } #[inline] - pub unsafe fn from_os_str_bytes_unchecked(s: Vec<u8>) -> Self { + pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self { Self { inner: s } } @@ -203,18 +203,18 @@ impl Buf { impl Slice { #[inline] - pub fn as_os_str_bytes(&self) -> &[u8] { + pub fn as_encoded_bytes(&self) -> &[u8] { &self.inner } #[inline] - pub unsafe fn from_os_str_bytes_unchecked(s: &[u8]) -> &Slice { + pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { unsafe { mem::transmute(s) } } #[inline] pub fn from_str(s: &str) -> &Slice { - unsafe { Slice::from_os_str_bytes_unchecked(s.as_bytes()) } + unsafe { Slice::from_encoded_bytes_unchecked(s.as_bytes()) } } pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> { diff --git a/library/std/src/sys/unix/os_str/tests.rs b/library/std/src/sys/unix/os_str/tests.rs index 91bc0e61a4a..e2a99045e41 100644 --- a/library/std/src/sys/unix/os_str/tests.rs +++ b/library/std/src/sys/unix/os_str/tests.rs @@ -2,7 +2,7 @@ use super::*; #[test] fn slice_debug_output() { - let input = unsafe { Slice::from_os_str_bytes_unchecked(b"\xF0hello,\tworld") }; + let input = unsafe { Slice::from_encoded_bytes_unchecked(b"\xF0hello,\tworld") }; let expected = r#""\xF0hello,\tworld""#; let output = format!("{input:?}"); @@ -12,6 +12,6 @@ fn slice_debug_output() { #[test] fn display() { assert_eq!("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye", unsafe { - Slice::from_os_str_bytes_unchecked(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string() + Slice::from_encoded_bytes_unchecked(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string() },); } diff --git a/library/std/src/sys/unix/path.rs b/library/std/src/sys/unix/path.rs index 935245f637b..837f68d3eaf 100644 --- a/library/std/src/sys/unix/path.rs +++ b/library/std/src/sys/unix/path.rs @@ -30,7 +30,7 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> { // Get the components, skipping the redundant leading "." component if it exists. let mut components = path.strip_prefix(".").unwrap_or(path).components(); - let path_os = path.as_os_str().as_os_str_bytes(); + let path_os = path.as_os_str().as_encoded_bytes(); let mut normalized = if path.is_absolute() { // "If a pathname begins with two successive <slash> characters, the diff --git a/library/std/src/sys/unix/process/process_common.rs b/library/std/src/sys/unix/process/process_common.rs index 640648e8707..e7d1533f122 100644 --- a/library/std/src/sys/unix/process/process_common.rs +++ b/library/std/src/sys/unix/process/process_common.rs @@ -164,9 +164,9 @@ pub enum ProgramKind { impl ProgramKind { fn new(program: &OsStr) -> Self { - if program.as_os_str_bytes().starts_with(b"/") { + if program.as_encoded_bytes().starts_with(b"/") { Self::Absolute - } else if program.as_os_str_bytes().contains(&b'/') { + } else if program.as_encoded_bytes().contains(&b'/') { // If the program has more than one component in it, it is a relative path. Self::Relative } else { diff --git a/library/std/src/sys/windows/args.rs b/library/std/src/sys/windows/args.rs index 6b597f499bc..ee7dba6e5b3 100644 --- a/library/std/src/sys/windows/args.rs +++ b/library/std/src/sys/windows/args.rs @@ -226,7 +226,7 @@ pub(crate) fn append_arg(cmd: &mut Vec<u16>, arg: &Arg, force_quotes: bool) -> i // that it actually gets passed through on the command line or otherwise // it will be dropped entirely when parsed on the other end. ensure_no_nuls(arg)?; - let arg_bytes = arg.as_os_str_bytes(); + let arg_bytes = arg.as_encoded_bytes(); let (quote, escape) = match quote { Quote::Always => (true, true), Quote::Auto => { @@ -298,7 +298,7 @@ pub(crate) fn make_bat_command_line( const SPECIAL: &[u8] = b"\t &()[]{}^=;!'+,`~%|<>"; let force_quotes = match arg { Arg::Regular(arg) if !force_quotes => { - arg.as_os_str_bytes().iter().any(|c| SPECIAL.contains(c)) + arg.as_encoded_bytes().iter().any(|c| SPECIAL.contains(c)) } _ => force_quotes, }; diff --git a/library/std/src/sys/windows/os_str.rs b/library/std/src/sys/windows/os_str.rs index 4708657a907..237854fac4e 100644 --- a/library/std/src/sys/windows/os_str.rs +++ b/library/std/src/sys/windows/os_str.rs @@ -64,12 +64,12 @@ impl fmt::Display for Slice { impl Buf { #[inline] - pub fn into_os_str_bytes(self) -> Vec<u8> { + pub fn into_encoded_bytes(self) -> Vec<u8> { self.inner.into_bytes() } #[inline] - pub unsafe fn from_os_str_bytes_unchecked(s: Vec<u8>) -> Self { + pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self { Self { inner: Wtf8Buf::from_bytes_unchecked(s) } } @@ -162,12 +162,12 @@ impl Buf { impl Slice { #[inline] - pub fn as_os_str_bytes(&self) -> &[u8] { + pub fn as_encoded_bytes(&self) -> &[u8] { self.inner.as_bytes() } #[inline] - pub unsafe fn from_os_str_bytes_unchecked(s: &[u8]) -> &Slice { + pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice { mem::transmute(Wtf8::from_bytes_unchecked(s)) } diff --git a/library/std/src/sys/windows/path.rs b/library/std/src/sys/windows/path.rs index c9c2d10e6c4..8c0e07b3566 100644 --- a/library/std/src/sys/windows/path.rs +++ b/library/std/src/sys/windows/path.rs @@ -22,12 +22,12 @@ pub fn is_verbatim_sep(b: u8) -> bool { /// Returns true if `path` looks like a lone filename. pub(crate) fn is_file_name(path: &OsStr) -> bool { - !path.as_os_str_bytes().iter().copied().any(is_sep_byte) + !path.as_encoded_bytes().iter().copied().any(is_sep_byte) } pub(crate) fn has_trailing_slash(path: &OsStr) -> bool { - let is_verbatim = path.as_os_str_bytes().starts_with(br"\\?\"); + let is_verbatim = path.as_encoded_bytes().starts_with(br"\\?\"); let is_separator = if is_verbatim { is_verbatim_sep } else { is_sep_byte }; - if let Some(&c) = path.as_os_str_bytes().last() { is_separator(c) } else { false } + if let Some(&c) = path.as_encoded_bytes().last() { is_separator(c) } else { false } } /// Appends a suffix to a path. @@ -49,7 +49,7 @@ impl<'a, const LEN: usize> PrefixParser<'a, LEN> { fn get_prefix(path: &OsStr) -> [u8; LEN] { let mut prefix = [0; LEN]; // SAFETY: Only ASCII characters are modified. - for (i, &ch) in path.as_os_str_bytes().iter().take(LEN).enumerate() { + for (i, &ch) in path.as_encoded_bytes().iter().take(LEN).enumerate() { prefix[i] = if ch == b'/' { b'\\' } else { ch }; } prefix @@ -82,7 +82,7 @@ impl<'a> PrefixParserSlice<'a, '_> { } fn prefix_bytes(&self) -> &'a [u8] { - &self.path.as_os_str_bytes()[..self.index] + &self.path.as_encoded_bytes()[..self.index] } fn finish(self) -> &'a OsStr { @@ -90,7 +90,7 @@ impl<'a> PrefixParserSlice<'a, '_> { // &[u8] 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. - unsafe { OsStr::from_os_str_bytes_unchecked(&self.path.as_os_str_bytes()[self.index..]) } + unsafe { OsStr::from_encoded_bytes_unchecked(&self.path.as_encoded_bytes()[self.index..]) } } } @@ -162,7 +162,7 @@ fn parse_drive(path: &OsStr) -> Option<u8> { drive.is_ascii_alphabetic() } - match path.as_os_str_bytes() { + match path.as_encoded_bytes() { [drive, b':', ..] if is_valid_drive_letter(drive) => Some(drive.to_ascii_uppercase()), _ => None, } @@ -171,7 +171,7 @@ fn parse_drive(path: &OsStr) -> Option<u8> { // Parses a drive prefix exactly, e.g. "C:" fn parse_drive_exact(path: &OsStr) -> Option<u8> { // only parse two bytes: the drive letter and the drive separator - if path.as_os_str_bytes().get(2).map(|&x| is_sep_byte(x)).unwrap_or(true) { + if path.as_encoded_bytes().get(2).map(|&x| is_sep_byte(x)).unwrap_or(true) { parse_drive(path) } else { None @@ -185,15 +185,15 @@ fn parse_drive_exact(path: &OsStr) -> Option<u8> { fn parse_next_component(path: &OsStr, verbatim: bool) -> (&OsStr, &OsStr) { let separator = if verbatim { is_verbatim_sep } else { is_sep_byte }; - match path.as_os_str_bytes().iter().position(|&x| separator(x)) { + match path.as_encoded_bytes().iter().position(|&x| separator(x)) { Some(separator_start) => { let separator_end = separator_start + 1; - let component = &path.as_os_str_bytes()[..separator_start]; + let component = &path.as_encoded_bytes()[..separator_start]; // Panic safe // The max `separator_end` is `bytes.len()` and `bytes[bytes.len()..]` is a valid index. - let path = &path.as_os_str_bytes()[separator_end..]; + let path = &path.as_encoded_bytes()[separator_end..]; // SAFETY: `path` is a valid wtf8 encoded slice and each of the separators ('/', '\') // is encoded in a single byte, therefore `bytes[separator_start]` and @@ -201,8 +201,8 @@ fn parse_next_component(path: &OsStr, verbatim: bool) -> (&OsStr, &OsStr) { // `bytes[..separator_start]` and `bytes[separator_end..]` are valid wtf8 slices. unsafe { ( - OsStr::from_os_str_bytes_unchecked(component), - OsStr::from_os_str_bytes_unchecked(path), + OsStr::from_encoded_bytes_unchecked(component), + OsStr::from_encoded_bytes_unchecked(path), ) } } @@ -323,7 +323,7 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> { // Verbatim paths should not be modified. if prefix.map(|x| x.is_verbatim()).unwrap_or(false) { // NULs in verbatim paths are rejected for consistency. - if path.as_os_str_bytes().contains(&0) { + if path.as_encoded_bytes().contains(&0) { return Err(io::const_io_error!( io::ErrorKind::InvalidInput, "strings passed to WinAPI cannot contain NULs", diff --git a/library/std/src/sys/windows/process.rs b/library/std/src/sys/windows/process.rs index 92edd2f6112..6f46da1748a 100644 --- a/library/std/src/sys/windows/process.rs +++ b/library/std/src/sys/windows/process.rs @@ -429,7 +429,7 @@ fn resolve_exe<'a>( // Test if the file name has the `exe` extension. // This does a case-insensitive `ends_with`. let has_exe_suffix = if exe_path.len() >= EXE_SUFFIX.len() { - exe_path.as_os_str_bytes()[exe_path.len() - EXE_SUFFIX.len()..] + exe_path.as_encoded_bytes()[exe_path.len() - EXE_SUFFIX.len()..] .eq_ignore_ascii_case(EXE_SUFFIX.as_bytes()) } else { false @@ -459,7 +459,7 @@ fn resolve_exe<'a>( // From the `CreateProcessW` docs: // > If the file name does not contain an extension, .exe is appended. // Note that this rule only applies when searching paths. - let has_extension = exe_path.as_os_str_bytes().contains(&b'.'); + let has_extension = exe_path.as_encoded_bytes().contains(&b'.'); // Search the directories given by `search_paths`. let result = search_paths(parent_paths, child_paths, |mut path| { |
