Skip to content

Commit 4a11759

Browse files
authored
Rollup merge of #147933 - thaliaarchi:consistent-osstring, r=tgross35
os_str: Make platform docs more consistent - Port `Buf::as_slice`/`as_mut_slice` wording from wtf8 to bytes - Make `Buf::extend_from_slice_unchecked` docs more platform-independent - wtf8 `Buf` was missing `#[repr(transparent)]`
2 parents aa65c31 + 563302e commit 4a11759

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,17 @@ impl Buf {
176176

177177
#[inline]
178178
pub fn as_slice(&self) -> &Slice {
179-
// SAFETY: Slice just wraps [u8],
180-
// and &*self.inner is &[u8], therefore
181-
// transmuting &[u8] to &Slice is safe.
179+
// SAFETY: Slice is just a wrapper for [u8],
180+
// and self.inner.as_slice() returns &[u8].
181+
// Therefore, transmuting &[u8] to &Slice is safe.
182182
unsafe { mem::transmute(self.inner.as_slice()) }
183183
}
184184

185185
#[inline]
186186
pub fn as_mut_slice(&mut self) -> &mut Slice {
187-
// SAFETY: Slice just wraps [u8],
188-
// and &mut *self.inner is &mut [u8], therefore
189-
// transmuting &mut [u8] to &mut Slice is safe.
187+
// SAFETY: Slice is just a wrapper for [u8],
188+
// and self.inner.as_mut_slice() returns &mut [u8].
189+
// Therefore, transmuting &mut [u8] to &mut Slice is safe.
190190
unsafe { mem::transmute(self.inner.as_mut_slice()) }
191191
}
192192

@@ -233,7 +233,9 @@ impl Buf {
233233
///
234234
/// # Safety
235235
///
236-
/// This encoding has no safety requirements.
236+
/// The slice must be valid for the platform encoding (as described in
237+
/// `OsStr::from_encoded_bytes_unchecked`). This encoding has no safety
238+
/// requirements.
237239
#[inline]
238240
pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
239241
self.inner.extend_from_slice(other);

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The underlying OsString/OsStr implementation on Windows is a
22
//! wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
3+
34
use alloc::wtf8::{Wtf8, Wtf8Buf};
45
use core::clone::CloneToUninit;
56

@@ -11,6 +12,7 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
1112
use crate::{fmt, mem};
1213

1314
#[derive(Hash)]
15+
#[repr(transparent)]
1416
pub struct Buf {
1517
pub inner: Wtf8Buf,
1618
}
@@ -213,11 +215,12 @@ impl Buf {
213215
/// # Safety
214216
///
215217
/// The slice must be valid for the platform encoding (as described in
216-
/// [`Slice::from_encoded_bytes_unchecked`]).
218+
/// `OsStr::from_encoded_bytes_unchecked`). For this encoding, that means
219+
/// `other` must be valid WTF-8.
217220
///
218-
/// This bypasses the WTF-8 surrogate joining, so either `self` must not
219-
/// end with a leading surrogate half, or `other` must not start with a
220-
/// trailing surrogate half.
221+
/// Additionally, this method bypasses the WTF-8 surrogate joining, so
222+
/// either `self` must not end with a leading surrogate half, or `other`
223+
/// must not start with a trailing surrogate half.
221224
#[inline]
222225
pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
223226
unsafe {

0 commit comments

Comments
 (0)