Skip to content

Commit 30292bb

Browse files
committed
fix(std): Rename os_str_bytes to encoded_bytes
1 parent 9aee1de commit 30292bb

File tree

16 files changed

+87
-87
lines changed

16 files changed

+87
-87
lines changed

library/std/src/ffi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@
132132
//! On all platforms, [`OsStr`] consists of a sequence of bytes that is encoded as a superset of
133133
//! UTF-8; see [`OsString`] for more details on its encoding on different platforms.
134134
//!
135-
//! For limited, inexpensive conversions from and to bytes, see [`OsStr::as_os_str_bytes`] and
136-
//! [`OsStr::from_os_str_bytes_unchecked`].
135+
//! For limited, inexpensive conversions from and to bytes, see [`OsStr::as_encoded_bytes`] and
136+
//! [`OsStr::from_encoded_bytes_unchecked`].
137137
//!
138138
//! [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value
139139
//! [Unicode code point]: https://www.unicode.org/glossary/#code_point

library/std/src/ffi/os_str.rs

+28-28
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ impl OsString {
154154
/// # Safety
155155
///
156156
/// As the encoding is unspecified, callers must pass in bytes that originated as a mixture of
157-
/// validated UTF-8 and bytes from [`OsStr::as_os_str_bytes`] from within the same rust version
157+
/// validated UTF-8 and bytes from [`OsStr::as_encoded_bytes`] from within the same rust version
158158
/// built for the same target platform. For example, reconstructing an `OsString` from bytes sent
159159
/// over the network or stored in a file will likely violate these safety rules.
160160
///
161-
/// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_os_str_bytes`] can be
161+
/// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_encoded_bytes`] can be
162162
/// split either immediately before or immediately after any valid non-empty UTF-8 substring.
163163
///
164164
/// # Example
@@ -167,21 +167,21 @@ impl OsString {
167167
/// use std::ffi::OsStr;
168168
///
169169
/// let os_str = OsStr::new("Mary had a little lamb");
170-
/// let bytes = os_str.as_os_str_bytes();
170+
/// let bytes = os_str.as_encoded_bytes();
171171
/// let words = bytes.split(|b| *b == b' ');
172172
/// let words: Vec<&OsStr> = words.map(|word| {
173173
/// // SAFETY:
174-
/// // - Each `word` only contains content that originated from `OsStr::as_os_str_bytes`
174+
/// // - Each `word` only contains content that originated from `OsStr::as_encoded_bytes`
175175
/// // - Only split with ASCII whitespace which is a non-empty UTF-8 substring
176-
/// unsafe { OsStr::from_os_str_bytes_unchecked(word) }
176+
/// unsafe { OsStr::from_encoded_bytes_unchecked(word) }
177177
/// }).collect();
178178
/// ```
179179
///
180180
/// [conversions]: super#conversions
181181
#[inline]
182182
#[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")]
183-
pub unsafe fn from_os_str_bytes_unchecked(bytes: Vec<u8>) -> Self {
184-
OsString { inner: Buf::from_os_str_bytes_unchecked(bytes) }
183+
pub unsafe fn from_encoded_bytes_unchecked(bytes: Vec<u8>) -> Self {
184+
OsString { inner: Buf::from_encoded_bytes_unchecked(bytes) }
185185
}
186186

187187
/// Converts to an [`OsStr`] slice.
@@ -203,7 +203,7 @@ impl OsString {
203203
}
204204

205205
/// Converts the `OsString` into a byte slice. To convert the byte slice back into an
206-
/// `OsString`, use the [`OsStr::from_os_str_bytes_unchecked`] function.
206+
/// `OsString`, use the [`OsStr::from_encoded_bytes_unchecked`] function.
207207
///
208208
/// The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8.
209209
/// By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit
@@ -218,8 +218,8 @@ impl OsString {
218218
/// [`std::ffi`]: crate::ffi
219219
#[inline]
220220
#[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")]
221-
pub fn into_os_str_bytes(self) -> Vec<u8> {
222-
self.inner.into_os_str_bytes()
221+
pub fn into_encoded_bytes(self) -> Vec<u8> {
222+
self.inner.into_encoded_bytes()
223223
}
224224

225225
/// Converts the `OsString` into a [`String`] if it contains valid Unicode data.
@@ -743,11 +743,11 @@ impl OsStr {
743743
/// # Safety
744744
///
745745
/// As the encoding is unspecified, callers must pass in bytes that originated as a mixture of
746-
/// validated UTF-8 and bytes from [`OsStr::as_os_str_bytes`] from within the same rust version
746+
/// validated UTF-8 and bytes from [`OsStr::as_encoded_bytes`] from within the same rust version
747747
/// built for the same target platform. For example, reconstructing an `OsStr` from bytes sent
748748
/// over the network or stored in a file will likely violate these safety rules.
749749
///
750-
/// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_os_str_bytes`] can be
750+
/// Due to the encoding being self-synchronizing, the bytes from [`OsStr::as_encoded_bytes`] can be
751751
/// split either immediately before or immediately after any valid non-empty UTF-8 substring.
752752
///
753753
/// # Example
@@ -756,21 +756,21 @@ impl OsStr {
756756
/// use std::ffi::OsStr;
757757
///
758758
/// let os_str = OsStr::new("Mary had a little lamb");
759-
/// let bytes = os_str.as_os_str_bytes();
759+
/// let bytes = os_str.as_encoded_bytes();
760760
/// let words = bytes.split(|b| *b == b' ');
761761
/// let words: Vec<&OsStr> = words.map(|word| {
762762
/// // SAFETY:
763-
/// // - Each `word` only contains content that originated from `OsStr::as_os_str_bytes`
763+
/// // - Each `word` only contains content that originated from `OsStr::as_encoded_bytes`
764764
/// // - Only split with ASCII whitespace which is a non-empty UTF-8 substring
765-
/// unsafe { OsStr::from_os_str_bytes_unchecked(word) }
765+
/// unsafe { OsStr::from_encoded_bytes_unchecked(word) }
766766
/// }).collect();
767767
/// ```
768768
///
769769
/// [conversions]: super#conversions
770770
#[inline]
771771
#[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")]
772-
pub unsafe fn from_os_str_bytes_unchecked(bytes: &[u8]) -> &Self {
773-
Self::from_inner(Slice::from_os_str_bytes_unchecked(bytes))
772+
pub unsafe fn from_encoded_bytes_unchecked(bytes: &[u8]) -> &Self {
773+
Self::from_inner(Slice::from_encoded_bytes_unchecked(bytes))
774774
}
775775

776776
#[inline]
@@ -944,7 +944,7 @@ impl OsStr {
944944
}
945945

946946
/// Converts an OS string slice to a byte slice. To convert the byte slice back into an OS
947-
/// string slice, use the [`OsStr::from_os_str_bytes_unchecked`] function.
947+
/// string slice, use the [`OsStr::from_encoded_bytes_unchecked`] function.
948948
///
949949
/// The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8.
950950
/// By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit
@@ -959,8 +959,8 @@ impl OsStr {
959959
/// [`std::ffi`]: crate::ffi
960960
#[inline]
961961
#[stable(feature = "os_str_bytes", since = "CURRENT_RUSTC_VERSION")]
962-
pub fn as_os_str_bytes(&self) -> &[u8] {
963-
self.inner.as_os_str_bytes()
962+
pub fn as_encoded_bytes(&self) -> &[u8] {
963+
self.inner.as_encoded_bytes()
964964
}
965965

966966
/// Converts this string to its ASCII lower case equivalent in-place.
@@ -1266,7 +1266,7 @@ impl Default for &OsStr {
12661266
impl PartialEq for OsStr {
12671267
#[inline]
12681268
fn eq(&self, other: &OsStr) -> bool {
1269-
self.as_os_str_bytes().eq(other.as_os_str_bytes())
1269+
self.as_encoded_bytes().eq(other.as_encoded_bytes())
12701270
}
12711271
}
12721272

@@ -1293,23 +1293,23 @@ impl Eq for OsStr {}
12931293
impl PartialOrd for OsStr {
12941294
#[inline]
12951295
fn partial_cmp(&self, other: &OsStr) -> Option<cmp::Ordering> {
1296-
self.as_os_str_bytes().partial_cmp(other.as_os_str_bytes())
1296+
self.as_encoded_bytes().partial_cmp(other.as_encoded_bytes())
12971297
}
12981298
#[inline]
12991299
fn lt(&self, other: &OsStr) -> bool {
1300-
self.as_os_str_bytes().lt(other.as_os_str_bytes())
1300+
self.as_encoded_bytes().lt(other.as_encoded_bytes())
13011301
}
13021302
#[inline]
13031303
fn le(&self, other: &OsStr) -> bool {
1304-
self.as_os_str_bytes().le(other.as_os_str_bytes())
1304+
self.as_encoded_bytes().le(other.as_encoded_bytes())
13051305
}
13061306
#[inline]
13071307
fn gt(&self, other: &OsStr) -> bool {
1308-
self.as_os_str_bytes().gt(other.as_os_str_bytes())
1308+
self.as_encoded_bytes().gt(other.as_encoded_bytes())
13091309
}
13101310
#[inline]
13111311
fn ge(&self, other: &OsStr) -> bool {
1312-
self.as_os_str_bytes().ge(other.as_os_str_bytes())
1312+
self.as_encoded_bytes().ge(other.as_encoded_bytes())
13131313
}
13141314
}
13151315

@@ -1328,7 +1328,7 @@ impl PartialOrd<str> for OsStr {
13281328
impl Ord for OsStr {
13291329
#[inline]
13301330
fn cmp(&self, other: &OsStr) -> cmp::Ordering {
1331-
self.as_os_str_bytes().cmp(other.as_os_str_bytes())
1331+
self.as_encoded_bytes().cmp(other.as_encoded_bytes())
13321332
}
13331333
}
13341334

@@ -1378,7 +1378,7 @@ impl_cmp!(Cow<'a, OsStr>, OsString);
13781378
impl Hash for OsStr {
13791379
#[inline]
13801380
fn hash<H: Hasher>(&self, state: &mut H) {
1381-
self.as_os_str_bytes().hash(state)
1381+
self.as_encoded_bytes().hash(state)
13821382
}
13831383
}
13841384

library/std/src/path.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -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.as_os_str_bytes().len()
196+
s.as_encoded_bytes().len()
197197
}
198198
match *self {
199199
Verbatim(x) => 4 + os_str_len(x),
@@ -316,31 +316,31 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
316316

317317
// basic workhorse for splitting stem and extension
318318
fn rsplit_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
319-
if file.as_os_str_bytes() == b".." {
319+
if file.as_encoded_bytes() == b".." {
320320
return (Some(file), None);
321321
}
322322

323323
// The unsafety here stems from converting between &OsStr and &[u8]
324324
// and back. This is safe to do because (1) we only look at ASCII
325325
// contents of the encoding and (2) new &OsStr values are produced
326326
// only from ASCII-bounded slices of existing &OsStr values.
327-
let mut iter = file.as_os_str_bytes().rsplitn(2, |b| *b == b'.');
327+
let mut iter = file.as_encoded_bytes().rsplitn(2, |b| *b == b'.');
328328
let after = iter.next();
329329
let before = iter.next();
330330
if before == Some(b"") {
331331
(Some(file), None)
332332
} else {
333333
unsafe {
334334
(
335-
before.map(|s| OsStr::from_os_str_bytes_unchecked(s)),
336-
after.map(|s| OsStr::from_os_str_bytes_unchecked(s)),
335+
before.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
336+
after.map(|s| OsStr::from_encoded_bytes_unchecked(s)),
337337
)
338338
}
339339
}
340340
}
341341

342342
fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
343-
let slice = file.as_os_str_bytes();
343+
let slice = file.as_encoded_bytes();
344344
if slice == b".." {
345345
return (file, None);
346346
}
@@ -357,8 +357,8 @@ fn split_file_at_dot(file: &OsStr) -> (&OsStr, Option<&OsStr>) {
357357
let after = &slice[i + 1..];
358358
unsafe {
359359
(
360-
OsStr::from_os_str_bytes_unchecked(before),
361-
Some(OsStr::from_os_str_bytes_unchecked(after)),
360+
OsStr::from_encoded_bytes_unchecked(before),
361+
Some(OsStr::from_encoded_bytes_unchecked(after)),
362362
)
363363
}
364364
}
@@ -739,7 +739,7 @@ impl<'a> Components<'a> {
739739
// separately via `include_cur_dir`
740740
b".." => Some(Component::ParentDir),
741741
b"" => None,
742-
_ => Some(Component::Normal(unsafe { OsStr::from_os_str_bytes_unchecked(comp) })),
742+
_ => Some(Component::Normal(unsafe { OsStr::from_encoded_bytes_unchecked(comp) })),
743743
}
744744
}
745745

@@ -896,7 +896,7 @@ impl<'a> Iterator for Components<'a> {
896896
let raw = &self.path[..self.prefix_len()];
897897
self.path = &self.path[self.prefix_len()..];
898898
return Some(Component::Prefix(PrefixComponent {
899-
raw: unsafe { OsStr::from_os_str_bytes_unchecked(raw) },
899+
raw: unsafe { OsStr::from_encoded_bytes_unchecked(raw) },
900900
parsed: self.prefix.unwrap(),
901901
}));
902902
}
@@ -968,7 +968,7 @@ impl<'a> DoubleEndedIterator for Components<'a> {
968968
State::Prefix if self.prefix_len() > 0 => {
969969
self.back = State::Done;
970970
return Some(Component::Prefix(PrefixComponent {
971-
raw: unsafe { OsStr::from_os_str_bytes_unchecked(self.path) },
971+
raw: unsafe { OsStr::from_encoded_bytes_unchecked(self.path) },
972972
parsed: self.prefix.unwrap(),
973973
}));
974974
}
@@ -1477,17 +1477,17 @@ impl PathBuf {
14771477
fn _set_extension(&mut self, extension: &OsStr) -> bool {
14781478
let file_stem = match self.file_stem() {
14791479
None => return false,
1480-
Some(f) => f.as_os_str_bytes(),
1480+
Some(f) => f.as_encoded_bytes(),
14811481
};
14821482

14831483
// truncate until right after the file stem
14841484
let end_file_stem = file_stem[file_stem.len()..].as_ptr().addr();
1485-
let start = self.inner.as_os_str_bytes().as_ptr().addr();
1485+
let start = self.inner.as_encoded_bytes().as_ptr().addr();
14861486
let v = self.as_mut_vec();
14871487
v.truncate(end_file_stem.wrapping_sub(start));
14881488

14891489
// add the new extension, if any
1490-
let new = extension.as_os_str_bytes();
1490+
let new = extension.as_encoded_bytes();
14911491
if !new.is_empty() {
14921492
v.reserve_exact(new.len() + 1);
14931493
v.push(b'.');
@@ -2007,11 +2007,11 @@ impl Path {
20072007
// The following (private!) function allows construction of a path from a u8
20082008
// slice, which is only safe when it is known to follow the OsStr encoding.
20092009
unsafe fn from_u8_slice(s: &[u8]) -> &Path {
2010-
unsafe { Path::new(OsStr::from_os_str_bytes_unchecked(s)) }
2010+
unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) }
20112011
}
20122012
// The following (private!) function reveals the byte encoding used for OsStr.
20132013
fn as_u8_slice(&self) -> &[u8] {
2014-
self.inner.as_os_str_bytes()
2014+
self.inner.as_encoded_bytes()
20152015
}
20162016

20172017
/// Directly wraps a string slice as a `Path` slice.
@@ -2609,7 +2609,7 @@ impl Path {
26092609

26102610
fn _with_extension(&self, extension: &OsStr) -> PathBuf {
26112611
let self_len = self.as_os_str().len();
2612-
let self_bytes = self.as_os_str().as_os_str_bytes();
2612+
let self_bytes = self.as_os_str().as_encoded_bytes();
26132613

26142614
let (new_capacity, slice_to_copy) = match self.extension() {
26152615
None => {

library/std/src/sys/common/small_c_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn run_path_with_cstr<T, F>(path: &Path, f: F) -> io::Result<T>
1919
where
2020
F: FnOnce(&CStr) -> io::Result<T>,
2121
{
22-
run_with_cstr(path.as_os_str().as_os_str_bytes(), f)
22+
run_with_cstr(path.as_os_str().as_encoded_bytes(), f)
2323
}
2424

2525
#[inline]

library/std/src/sys/common/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::iter::repeat;
88
fn stack_allocation_works() {
99
let path = Path::new("abc");
1010
let result = run_path_with_cstr(path, |p| {
11-
assert_eq!(p, &*CString::new(path.as_os_str().as_os_str_bytes()).unwrap());
11+
assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap());
1212
Ok(42)
1313
});
1414
assert_eq!(result.unwrap(), 42);
@@ -25,7 +25,7 @@ fn heap_allocation_works() {
2525
let path = repeat("a").take(384).collect::<String>();
2626
let path = Path::new(&path);
2727
let result = run_path_with_cstr(path, |p| {
28-
assert_eq!(p, &*CString::new(path.as_os_str().as_os_str_bytes()).unwrap());
28+
assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap());
2929
Ok(42)
3030
});
3131
assert_eq!(result.unwrap(), 42);

library/std/src/sys/unix/os_str.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ impl AsInner<[u8]> for Buf {
9797

9898
impl Buf {
9999
#[inline]
100-
pub fn into_os_str_bytes(self) -> Vec<u8> {
100+
pub fn into_encoded_bytes(self) -> Vec<u8> {
101101
self.inner
102102
}
103103

104104
#[inline]
105-
pub unsafe fn from_os_str_bytes_unchecked(s: Vec<u8>) -> Self {
105+
pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self {
106106
Self { inner: s }
107107
}
108108

@@ -203,18 +203,18 @@ impl Buf {
203203

204204
impl Slice {
205205
#[inline]
206-
pub fn as_os_str_bytes(&self) -> &[u8] {
206+
pub fn as_encoded_bytes(&self) -> &[u8] {
207207
&self.inner
208208
}
209209

210210
#[inline]
211-
pub unsafe fn from_os_str_bytes_unchecked(s: &[u8]) -> &Slice {
211+
pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice {
212212
unsafe { mem::transmute(s) }
213213
}
214214

215215
#[inline]
216216
pub fn from_str(s: &str) -> &Slice {
217-
unsafe { Slice::from_os_str_bytes_unchecked(s.as_bytes()) }
217+
unsafe { Slice::from_encoded_bytes_unchecked(s.as_bytes()) }
218218
}
219219

220220
pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> {

library/std/src/sys/unix/os_str/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
#[test]
44
fn slice_debug_output() {
5-
let input = unsafe { Slice::from_os_str_bytes_unchecked(b"\xF0hello,\tworld") };
5+
let input = unsafe { Slice::from_encoded_bytes_unchecked(b"\xF0hello,\tworld") };
66
let expected = r#""\xF0hello,\tworld""#;
77
let output = format!("{input:?}");
88

@@ -12,6 +12,6 @@ fn slice_debug_output() {
1212
#[test]
1313
fn display() {
1414
assert_eq!("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye", unsafe {
15-
Slice::from_os_str_bytes_unchecked(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string()
15+
Slice::from_encoded_bytes_unchecked(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string()
1616
},);
1717
}

0 commit comments

Comments
 (0)