Skip to content

Commit cb2f34d

Browse files
committed
Auto merge of #58953 - jethrogb:jb/unify-ffi, r=alexcrichton
Unify OsString/OsStr for byte-based implementations As requested in #57860 r? @joshtriplett
2 parents ed19622 + 2079df1 commit cb2f34d

File tree

13 files changed

+186
-809
lines changed

13 files changed

+186
-809
lines changed

src/libstd/sys/cloudabi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ pub mod io;
1414
pub mod memchr;
1515
pub mod mutex;
1616
pub mod os;
17-
#[path = "../unix/os_str.rs"]
18-
pub mod os_str;
1917
pub mod rwlock;
2018
pub mod stack_overflow;
2119
pub mod stdio;
@@ -24,6 +22,8 @@ pub mod thread;
2422
pub mod thread_local;
2523
pub mod time;
2624

25+
pub use crate::sys_common::os_str_bytes as os_str;
26+
2727
mod abi;
2828

2929
mod shims;

src/libstd/sys/redox/ext/ffi.rs

+33-50
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
11
//! Redox-specific extension to the primitives in the `std::ffi` module.
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! use std::ffi::OsString;
7+
//! use std::os::redox::ffi::OsStringExt;
8+
//!
9+
//! let bytes = b"foo".to_vec();
10+
//!
11+
//! // OsStringExt::from_vec
12+
//! let os_string = OsString::from_vec(bytes);
13+
//! assert_eq!(os_string.to_str(), Some("foo"));
14+
//!
15+
//! // OsStringExt::into_vec
16+
//! let bytes = os_string.into_vec();
17+
//! assert_eq!(bytes, b"foo");
18+
//! ```
19+
//!
20+
//! ```
21+
//! use std::ffi::OsStr;
22+
//! use std::os::redox::ffi::OsStrExt;
23+
//!
24+
//! let bytes = b"foo";
25+
//!
26+
//! // OsStrExt::from_bytes
27+
//! let os_str = OsStr::from_bytes(bytes);
28+
//! assert_eq!(os_str.to_str(), Some("foo"));
29+
//!
30+
//! // OsStrExt::as_bytes
31+
//! let bytes = os_str.as_bytes();
32+
//! assert_eq!(bytes, b"foo");
33+
//! ```
234
335
#![stable(feature = "rust1", since = "1.0.0")]
436

5-
use crate::ffi::{OsStr, OsString};
6-
use crate::mem;
7-
use crate::sys::os_str::Buf;
8-
use crate::sys_common::{FromInner, IntoInner, AsInner};
9-
10-
/// Redox-specific extensions to [`OsString`].
11-
///
12-
/// [`OsString`]: ../../../../std/ffi/struct.OsString.html
13-
#[stable(feature = "rust1", since = "1.0.0")]
14-
pub trait OsStringExt {
15-
/// Creates an `OsString` from a byte vector.
16-
#[stable(feature = "rust1", since = "1.0.0")]
17-
fn from_vec(vec: Vec<u8>) -> Self;
18-
19-
/// Yields the underlying byte vector of this `OsString`.
20-
#[stable(feature = "rust1", since = "1.0.0")]
21-
fn into_vec(self) -> Vec<u8>;
22-
}
23-
24-
#[stable(feature = "rust1", since = "1.0.0")]
25-
impl OsStringExt for OsString {
26-
fn from_vec(vec: Vec<u8>) -> OsString {
27-
FromInner::from_inner(Buf { inner: vec })
28-
}
29-
fn into_vec(self) -> Vec<u8> {
30-
self.into_inner().inner
31-
}
32-
}
33-
34-
/// Redox-specific extensions to [`OsStr`].
35-
///
36-
/// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
37-
#[stable(feature = "rust1", since = "1.0.0")]
38-
pub trait OsStrExt {
39-
#[stable(feature = "rust1", since = "1.0.0")]
40-
fn from_bytes(slice: &[u8]) -> &Self;
41-
42-
/// Gets the underlying byte view of the `OsStr` slice.
43-
#[stable(feature = "rust1", since = "1.0.0")]
44-
fn as_bytes(&self) -> &[u8];
45-
}
46-
4737
#[stable(feature = "rust1", since = "1.0.0")]
48-
impl OsStrExt for OsStr {
49-
fn from_bytes(slice: &[u8]) -> &OsStr {
50-
unsafe { mem::transmute(slice) }
51-
}
52-
fn as_bytes(&self) -> &[u8] {
53-
&self.as_inner().inner
54-
}
55-
}
38+
pub use crate::sys_common::os_str_bytes::*;

src/libstd/sys/redox/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub mod memchr;
2222
pub mod mutex;
2323
pub mod net;
2424
pub mod os;
25-
pub mod os_str;
2625
pub mod path;
2726
pub mod pipe;
2827
pub mod process;
@@ -35,6 +34,8 @@ pub mod thread;
3534
pub mod thread_local;
3635
pub mod time;
3736

37+
pub use crate::sys_common::os_str_bytes as os_str;
38+
3839
#[cfg(not(test))]
3940
pub fn init() {}
4041

src/libstd/sys/sgx/ext/ffi.rs

+33-104
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,38 @@
11
//! SGX-specific extension to the primitives in the `std::ffi` module
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! use std::ffi::OsString;
7+
//! use std::os::fortanix_sgx::ffi::OsStringExt;
8+
//!
9+
//! let bytes = b"foo".to_vec();
10+
//!
11+
//! // OsStringExt::from_vec
12+
//! let os_string = OsString::from_vec(bytes);
13+
//! assert_eq!(os_string.to_str(), Some("foo"));
14+
//!
15+
//! // OsStringExt::into_vec
16+
//! let bytes = os_string.into_vec();
17+
//! assert_eq!(bytes, b"foo");
18+
//! ```
19+
//!
20+
//! ```
21+
//! use std::ffi::OsStr;
22+
//! use std::os::fortanix_sgx::ffi::OsStrExt;
23+
//!
24+
//! let bytes = b"foo";
25+
//!
26+
//! // OsStrExt::from_bytes
27+
//! let os_str = OsStr::from_bytes(bytes);
28+
//! assert_eq!(os_str.to_str(), Some("foo"));
29+
//!
30+
//! // OsStrExt::as_bytes
31+
//! let bytes = os_str.as_bytes();
32+
//! assert_eq!(bytes, b"foo");
33+
//! ```
234
335
#![unstable(feature = "sgx_platform", issue = "56975")]
436

5-
use crate::ffi::{OsStr, OsString};
6-
use crate::mem;
7-
use crate::sys::os_str::Buf;
8-
use crate::sys_common::{FromInner, IntoInner, AsInner};
9-
10-
/// SGX-specific extensions to [`OsString`].
11-
///
12-
/// [`OsString`]: ../../../../std/ffi/struct.OsString.html
13-
#[unstable(feature = "sgx_platform", issue = "56975")]
14-
pub trait OsStringExt {
15-
/// Creates an [`OsString`] from a byte vector.
16-
///
17-
/// # Examples
18-
///
19-
/// ```
20-
/// use std::ffi::OsString;
21-
/// use std::os::unix::ffi::OsStringExt;
22-
///
23-
/// let bytes = b"foo".to_vec();
24-
/// let os_string = OsString::from_vec(bytes);
25-
/// assert_eq!(os_string.to_str(), Some("foo"));
26-
/// ```
27-
///
28-
/// [`OsString`]: ../../../ffi/struct.OsString.html
29-
#[unstable(feature = "sgx_platform", issue = "56975")]
30-
fn from_vec(vec: Vec<u8>) -> Self;
31-
32-
/// Yields the underlying byte vector of this [`OsString`].
33-
///
34-
/// # Examples
35-
///
36-
/// ```
37-
/// use std::ffi::OsString;
38-
/// use std::os::unix::ffi::OsStringExt;
39-
///
40-
/// let mut os_string = OsString::new();
41-
/// os_string.push("foo");
42-
/// let bytes = os_string.into_vec();
43-
/// assert_eq!(bytes, b"foo");
44-
/// ```
45-
///
46-
/// [`OsString`]: ../../../ffi/struct.OsString.html
47-
#[unstable(feature = "sgx_platform", issue = "56975")]
48-
fn into_vec(self) -> Vec<u8>;
49-
}
50-
51-
#[unstable(feature = "sgx_platform", issue = "56975")]
52-
impl OsStringExt for OsString {
53-
fn from_vec(vec: Vec<u8>) -> OsString {
54-
FromInner::from_inner(Buf { inner: vec })
55-
}
56-
fn into_vec(self) -> Vec<u8> {
57-
self.into_inner().inner
58-
}
59-
}
60-
61-
/// SGX-specific extensions to [`OsStr`].
62-
///
63-
/// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
64-
#[unstable(feature = "sgx_platform", issue = "56975")]
65-
pub trait OsStrExt {
66-
#[unstable(feature = "sgx_platform", issue = "56975")]
67-
/// Creates an [`OsStr`] from a byte slice.
68-
///
69-
/// # Examples
70-
///
71-
/// ```
72-
/// use std::ffi::OsStr;
73-
/// use std::os::unix::ffi::OsStrExt;
74-
///
75-
/// let bytes = b"foo";
76-
/// let os_str = OsStr::from_bytes(bytes);
77-
/// assert_eq!(os_str.to_str(), Some("foo"));
78-
/// ```
79-
///
80-
/// [`OsStr`]: ../../../ffi/struct.OsStr.html
81-
fn from_bytes(slice: &[u8]) -> &Self;
82-
83-
/// Gets the underlying byte view of the [`OsStr`] slice.
84-
///
85-
/// # Examples
86-
///
87-
/// ```
88-
/// use std::ffi::OsStr;
89-
/// use std::os::unix::ffi::OsStrExt;
90-
///
91-
/// let mut os_str = OsStr::new("foo");
92-
/// let bytes = os_str.as_bytes();
93-
/// assert_eq!(bytes, b"foo");
94-
/// ```
95-
///
96-
/// [`OsStr`]: ../../../ffi/struct.OsStr.html
97-
#[unstable(feature = "sgx_platform", issue = "56975")]
98-
fn as_bytes(&self) -> &[u8];
99-
}
100-
10137
#[unstable(feature = "sgx_platform", issue = "56975")]
102-
impl OsStrExt for OsStr {
103-
fn from_bytes(slice: &[u8]) -> &OsStr {
104-
unsafe { mem::transmute(slice) }
105-
}
106-
fn as_bytes(&self) -> &[u8] {
107-
&self.as_inner().inner
108-
}
109-
}
38+
pub use crate::sys_common::os_str_bytes::*;

src/libstd/sys/sgx/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub mod memchr;
2525
pub mod mutex;
2626
pub mod net;
2727
pub mod os;
28-
pub mod os_str;
2928
pub mod path;
3029
pub mod pipe;
3130
pub mod process;
@@ -36,6 +35,8 @@ pub mod thread_local;
3635
pub mod time;
3736
pub mod stdio;
3837

38+
pub use crate::sys_common::os_str_bytes as os_str;
39+
3940
#[cfg(not(test))]
4041
pub fn init() {
4142
}

0 commit comments

Comments
 (0)