Skip to content

Commit dd94145

Browse files
committed
Auto merge of #84967 - CDirkx:os_str_ext, r=m-ou-se
Move `OsStringExt` and `OsStrExt` to `std::os` Moves the `OsStringExt` and `OsStrExt` traits and implementations from `sys_common` to `os`. `sys_common` is for abstractions over `sys` and shouldn't really contain publicly exported items. This does introduce some duplication: the traits and implementations are now duplicated in `unix`, `wasi`, `hermit`, and `sgx`. However, I would argue that this duplication is no different to how something like `MetadataExt` is duplicated in `linux`, `vxworkx`, `redox`, `solaris` etc. The duplication also matches the fact that the traits on different platforms are technically distinct types: any platform is free to add it's own extra methods to the extension trait.
2 parents 2e940ac + ad7b897 commit dd94145

File tree

9 files changed

+92
-78
lines changed

9 files changed

+92
-78
lines changed

library/std/src/os/fortanix_sgx/ffi.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
3535
#![unstable(feature = "sgx_platform", issue = "56975")]
3636

37+
#[path = "../unix/ffi/os_str.rs"]
38+
mod os_str;
39+
3740
#[unstable(feature = "sgx_platform", issue = "56975")]
38-
pub use crate::sys_common::os_str_bytes::*;
41+
pub use self::os_str::{OsStrExt, OsStringExt};

library/std/src/os/hermit/ffi.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
3535
#![stable(feature = "rust1", since = "1.0.0")]
3636

37+
#[path = "../unix/ffi/os_str.rs"]
38+
mod os_str;
39+
3740
#[stable(feature = "rust1", since = "1.0.0")]
38-
pub use crate::sys_common::os_str_bytes::*;
41+
pub use self::os_str::{OsStrExt, OsStringExt};

library/std/src/os/unix/ffi.rs renamed to library/std/src/os/unix/ffi/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@
3434
3535
#![stable(feature = "rust1", since = "1.0.0")]
3636

37+
mod os_str;
38+
3739
#[stable(feature = "rust1", since = "1.0.0")]
38-
pub use crate::sys_common::os_str_bytes::*;
40+
pub use self::os_str::{OsStrExt, OsStringExt};

library/std/src/os/unix/ffi/os_str.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::ffi::{OsStr, OsString};
2+
use crate::mem;
3+
use crate::sealed::Sealed;
4+
use crate::sys::os_str::Buf;
5+
use crate::sys_common::{AsInner, FromInner, IntoInner};
6+
7+
// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication.
8+
// Keep this in mind when applying changes to this file that only apply to `unix`.
9+
10+
/// Platform-specific extensions to [`OsString`].
11+
///
12+
/// This trait is sealed: it cannot be implemented outside the standard library.
13+
/// This is so that future additional methods are not breaking changes.
14+
#[stable(feature = "rust1", since = "1.0.0")]
15+
pub trait OsStringExt: Sealed {
16+
/// Creates an [`OsString`] from a byte vector.
17+
///
18+
/// See the module documentation for an example.
19+
#[stable(feature = "rust1", since = "1.0.0")]
20+
fn from_vec(vec: Vec<u8>) -> Self;
21+
22+
/// Yields the underlying byte vector of this [`OsString`].
23+
///
24+
/// See the module documentation for an example.
25+
#[stable(feature = "rust1", since = "1.0.0")]
26+
fn into_vec(self) -> Vec<u8>;
27+
}
28+
29+
#[stable(feature = "rust1", since = "1.0.0")]
30+
impl OsStringExt for OsString {
31+
fn from_vec(vec: Vec<u8>) -> OsString {
32+
FromInner::from_inner(Buf { inner: vec })
33+
}
34+
fn into_vec(self) -> Vec<u8> {
35+
self.into_inner().inner
36+
}
37+
}
38+
39+
/// Platform-specific extensions to [`OsStr`].
40+
///
41+
/// This trait is sealed: it cannot be implemented outside the standard library.
42+
/// This is so that future additional methods are not breaking changes.
43+
#[stable(feature = "rust1", since = "1.0.0")]
44+
pub trait OsStrExt: Sealed {
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
/// Creates an [`OsStr`] from a byte slice.
47+
///
48+
/// See the module documentation for an example.
49+
fn from_bytes(slice: &[u8]) -> &Self;
50+
51+
/// Gets the underlying byte view of the [`OsStr`] slice.
52+
///
53+
/// See the module documentation for an example.
54+
#[stable(feature = "rust1", since = "1.0.0")]
55+
fn as_bytes(&self) -> &[u8];
56+
}
57+
58+
#[stable(feature = "rust1", since = "1.0.0")]
59+
impl OsStrExt for OsStr {
60+
#[inline]
61+
fn from_bytes(slice: &[u8]) -> &OsStr {
62+
unsafe { mem::transmute(slice) }
63+
}
64+
#[inline]
65+
fn as_bytes(&self) -> &[u8] {
66+
&self.as_inner().inner
67+
}
68+
}

library/std/src/os/wasi/ffi.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
33
#![stable(feature = "rust1", since = "1.0.0")]
44

5+
#[path = "../unix/ffi/os_str.rs"]
6+
mod os_str;
7+
58
#[stable(feature = "rust1", since = "1.0.0")]
6-
pub use crate::sys_common::os_str_bytes::*;
9+
pub use self::os_str::{OsStrExt, OsStringExt};

library/std/src/sys/hermit/args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl DoubleEndedIterator for Args {
5555
mod imp {
5656
use super::Args;
5757
use crate::ffi::{CStr, OsString};
58+
use crate::os::unix::ffi::OsStringExt;
5859
use crate::ptr;
59-
use crate::sys_common::os_str_bytes::*;
6060

6161
use crate::sys_common::mutex::StaticMutex;
6262

library/std/src/sys/hermit/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use crate::fmt;
33
use crate::hash::{Hash, Hasher};
44
use crate::io::{self, Error, ErrorKind};
55
use crate::io::{IoSlice, IoSliceMut, SeekFrom};
6+
use crate::os::unix::ffi::OsStrExt;
67
use crate::path::{Path, PathBuf};
78
use crate::sys::cvt;
89
use crate::sys::hermit::abi;
910
use crate::sys::hermit::abi::{O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
1011
use crate::sys::hermit::fd::FileDesc;
1112
use crate::sys::time::SystemTime;
1213
use crate::sys::unsupported;
13-
use crate::sys_common::os_str_bytes::OsStrExt;
1414

1515
pub use crate::sys_common::fs::{copy, try_exists};
1616
//pub use crate::sys_common::fs::remove_dir_all;

library/std/src/sys/hermit/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::ffi::{CStr, OsStr, OsString};
44
use crate::fmt;
55
use crate::io;
66
use crate::marker::PhantomData;
7+
use crate::os::unix::ffi::OsStringExt;
78
use crate::path::{self, PathBuf};
89
use crate::str;
910
use crate::sync::Mutex;
1011
use crate::sys::hermit::abi;
1112
use crate::sys::memchr;
1213
use crate::sys::unsupported;
13-
use crate::sys_common::os_str_bytes::*;
1414
use crate::vec;
1515

1616
pub fn errno() -> i32 {

library/std/src/sys_common/os_str_bytes.rs

+6-71
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,25 @@
22
//! systems: just a `Vec<u8>`/`[u8]`.
33
44
use crate::borrow::Cow;
5-
use crate::ffi::{OsStr, OsString};
5+
66
use crate::fmt;
77
use crate::mem;
88
use crate::rc::Rc;
9-
use crate::sealed::Sealed;
109
use crate::str;
1110
use crate::sync::Arc;
1211
use crate::sys_common::bytestring::debug_fmt_bytestring;
13-
use crate::sys_common::{AsInner, FromInner, IntoInner};
12+
use crate::sys_common::{AsInner, IntoInner};
1413

1514
use core::str::lossy::Utf8Lossy;
1615

1716
#[derive(Hash)]
18-
pub(crate) struct Buf {
17+
#[repr(transparent)]
18+
pub struct Buf {
1919
pub inner: Vec<u8>,
2020
}
2121

22-
// FIXME:
23-
// `Buf::as_slice` current implementation relies
24-
// on `Slice` being layout-compatible with `[u8]`.
25-
// When attribute privacy is implemented, `Slice` should be annotated as `#[repr(transparent)]`.
26-
// Anyway, `Slice` representation and layout are considered implementation detail, are
27-
// not documented and must not be relied upon.
28-
pub(crate) struct Slice {
22+
#[repr(transparent)]
23+
pub struct Slice {
2924
pub inner: [u8],
3025
}
3126

@@ -243,63 +238,3 @@ impl Slice {
243238
self.inner.eq_ignore_ascii_case(&other.inner)
244239
}
245240
}
246-
247-
/// Platform-specific extensions to [`OsString`].
248-
///
249-
/// This trait is sealed: it cannot be implemented outside the standard library.
250-
/// This is so that future additional methods are not breaking changes.
251-
#[stable(feature = "rust1", since = "1.0.0")]
252-
pub trait OsStringExt: Sealed {
253-
/// Creates an [`OsString`] from a byte vector.
254-
///
255-
/// See the module documentation for an example.
256-
#[stable(feature = "rust1", since = "1.0.0")]
257-
fn from_vec(vec: Vec<u8>) -> Self;
258-
259-
/// Yields the underlying byte vector of this [`OsString`].
260-
///
261-
/// See the module documentation for an example.
262-
#[stable(feature = "rust1", since = "1.0.0")]
263-
fn into_vec(self) -> Vec<u8>;
264-
}
265-
266-
#[stable(feature = "rust1", since = "1.0.0")]
267-
impl OsStringExt for OsString {
268-
fn from_vec(vec: Vec<u8>) -> OsString {
269-
FromInner::from_inner(Buf { inner: vec })
270-
}
271-
fn into_vec(self) -> Vec<u8> {
272-
self.into_inner().inner
273-
}
274-
}
275-
276-
/// Platform-specific extensions to [`OsStr`].
277-
///
278-
/// This trait is sealed: it cannot be implemented outside the standard library.
279-
/// This is so that future additional methods are not breaking changes.
280-
#[stable(feature = "rust1", since = "1.0.0")]
281-
pub trait OsStrExt: Sealed {
282-
#[stable(feature = "rust1", since = "1.0.0")]
283-
/// Creates an [`OsStr`] from a byte slice.
284-
///
285-
/// See the module documentation for an example.
286-
fn from_bytes(slice: &[u8]) -> &Self;
287-
288-
/// Gets the underlying byte view of the [`OsStr`] slice.
289-
///
290-
/// See the module documentation for an example.
291-
#[stable(feature = "rust1", since = "1.0.0")]
292-
fn as_bytes(&self) -> &[u8];
293-
}
294-
295-
#[stable(feature = "rust1", since = "1.0.0")]
296-
impl OsStrExt for OsStr {
297-
#[inline]
298-
fn from_bytes(slice: &[u8]) -> &OsStr {
299-
unsafe { mem::transmute(slice) }
300-
}
301-
#[inline]
302-
fn as_bytes(&self) -> &[u8] {
303-
&self.as_inner().inner
304-
}
305-
}

0 commit comments

Comments
 (0)