diff --git a/library/std/src/os/fortanix_sgx/ffi.rs b/library/std/src/os/fortanix_sgx/ffi.rs deleted file mode 100644 index ac1db0e5e39cc..0000000000000 --- a/library/std/src/os/fortanix_sgx/ffi.rs +++ /dev/null @@ -1,41 +0,0 @@ -//! SGX-specific extension to the primitives in the `std::ffi` module -//! -//! # Examples -//! -//! ``` -//! use std::ffi::OsString; -//! use std::os::fortanix_sgx::ffi::OsStringExt; -//! -//! let bytes = b"foo".to_vec(); -//! -//! // OsStringExt::from_vec -//! let os_string = OsString::from_vec(bytes); -//! assert_eq!(os_string.to_str(), Some("foo")); -//! -//! // OsStringExt::into_vec -//! let bytes = os_string.into_vec(); -//! assert_eq!(bytes, b"foo"); -//! ``` -//! -//! ``` -//! use std::ffi::OsStr; -//! use std::os::fortanix_sgx::ffi::OsStrExt; -//! -//! let bytes = b"foo"; -//! -//! // OsStrExt::from_bytes -//! let os_str = OsStr::from_bytes(bytes); -//! assert_eq!(os_str.to_str(), Some("foo")); -//! -//! // OsStrExt::as_bytes -//! let bytes = os_str.as_bytes(); -//! assert_eq!(bytes, b"foo"); -//! ``` - -#![unstable(feature = "sgx_platform", issue = "56975")] - -#[path = "../unix/ffi/os_str.rs"] -mod os_str; - -#[unstable(feature = "sgx_platform", issue = "56975")] -pub use self::os_str::{OsStrExt, OsStringExt}; diff --git a/library/std/src/os/fortanix_sgx/mod.rs b/library/std/src/os/fortanix_sgx/mod.rs index 2b5ccbe98f1b3..73210a90769ce 100644 --- a/library/std/src/os/fortanix_sgx/mod.rs +++ b/library/std/src/os/fortanix_sgx/mod.rs @@ -38,6 +38,7 @@ pub mod mem { } pub mod arch; +#[path = "../../sys/ffi/bytes.rs"] pub mod ffi; pub mod io; diff --git a/library/std/src/os/hermit/ffi.rs b/library/std/src/os/hermit/ffi.rs deleted file mode 100644 index 19761fd99b400..0000000000000 --- a/library/std/src/os/hermit/ffi.rs +++ /dev/null @@ -1,41 +0,0 @@ -//! HermitCore-specific extension to the primitives in the `std::ffi` module -//! -//! # Examples -//! -//! ``` -//! use std::ffi::OsString; -//! use std::os::hermit::ffi::OsStringExt; -//! -//! let bytes = b"foo".to_vec(); -//! -//! // OsStringExt::from_vec -//! let os_string = OsString::from_vec(bytes); -//! assert_eq!(os_string.to_str(), Some("foo")); -//! -//! // OsStringExt::into_vec -//! let bytes = os_string.into_vec(); -//! assert_eq!(bytes, b"foo"); -//! ``` -//! -//! ``` -//! use std::ffi::OsStr; -//! use std::os::hermit::ffi::OsStrExt; -//! -//! let bytes = b"foo"; -//! -//! // OsStrExt::from_bytes -//! let os_str = OsStr::from_bytes(bytes); -//! assert_eq!(os_str.to_str(), Some("foo")); -//! -//! // OsStrExt::as_bytes -//! let bytes = os_str.as_bytes(); -//! assert_eq!(bytes, b"foo"); -//! ``` - -#![stable(feature = "rust1", since = "1.0.0")] - -#[path = "../unix/ffi/os_str.rs"] -mod os_str; - -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::os_str::{OsStrExt, OsStringExt}; diff --git a/library/std/src/os/hermit/mod.rs b/library/std/src/os/hermit/mod.rs index 5812206a25759..08e922bb09300 100644 --- a/library/std/src/os/hermit/mod.rs +++ b/library/std/src/os/hermit/mod.rs @@ -5,6 +5,7 @@ #[stable(feature = "rust1", since = "1.0.0")] pub extern crate hermit_abi; +#[path = "../../sys/ffi/bytes.rs"] pub mod ffi; pub mod io; diff --git a/library/std/src/os/motor/mod.rs b/library/std/src/os/motor/mod.rs index 18da079c74a15..adc2d7d053289 100644 --- a/library/std/src/os/motor/mod.rs +++ b/library/std/src/os/motor/mod.rs @@ -1,4 +1,5 @@ #![unstable(feature = "motor_ext", issue = "147456")] +#[path = "../../sys/ffi/utf8.rs"] pub mod ffi; pub mod process; diff --git a/library/std/src/os/solid/ffi.rs b/library/std/src/os/solid/ffi.rs deleted file mode 100644 index aaa2070a6abe9..0000000000000 --- a/library/std/src/os/solid/ffi.rs +++ /dev/null @@ -1,41 +0,0 @@ -//! SOLID-specific extension to the primitives in the `std::ffi` module -//! -//! # Examples -//! -//! ``` -//! use std::ffi::OsString; -//! use std::os::solid::ffi::OsStringExt; -//! -//! let bytes = b"foo".to_vec(); -//! -//! // OsStringExt::from_vec -//! let os_string = OsString::from_vec(bytes); -//! assert_eq!(os_string.to_str(), Some("foo")); -//! -//! // OsStringExt::into_vec -//! let bytes = os_string.into_vec(); -//! assert_eq!(bytes, b"foo"); -//! ``` -//! -//! ``` -//! use std::ffi::OsStr; -//! use std::os::solid::ffi::OsStrExt; -//! -//! let bytes = b"foo"; -//! -//! // OsStrExt::from_bytes -//! let os_str = OsStr::from_bytes(bytes); -//! assert_eq!(os_str.to_str(), Some("foo")); -//! -//! // OsStrExt::as_bytes -//! let bytes = os_str.as_bytes(); -//! assert_eq!(bytes, b"foo"); -//! ``` - -#![stable(feature = "rust1", since = "1.0.0")] - -#[path = "../unix/ffi/os_str.rs"] -mod os_str; - -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::os_str::{OsStrExt, OsStringExt}; diff --git a/library/std/src/os/solid/mod.rs b/library/std/src/os/solid/mod.rs index 75824048e2498..80e81800092a1 100644 --- a/library/std/src/os/solid/mod.rs +++ b/library/std/src/os/solid/mod.rs @@ -1,6 +1,7 @@ #![stable(feature = "rust1", since = "1.0.0")] #![forbid(unsafe_op_in_unsafe_fn)] +#[path = "../../sys/ffi/bytes.rs"] pub mod ffi; pub mod io; diff --git a/library/std/src/os/uefi/mod.rs b/library/std/src/os/uefi/mod.rs index b42d796b28f69..4c2d1f6707872 100644 --- a/library/std/src/os/uefi/mod.rs +++ b/library/std/src/os/uefi/mod.rs @@ -5,5 +5,5 @@ #![forbid(unsafe_op_in_unsafe_fn)] pub mod env; -#[path = "../windows/ffi.rs"] +#[path = "../../sys/ffi/windows.rs"] pub mod ffi; diff --git a/library/std/src/os/unix/ffi/mod.rs b/library/std/src/os/unix/ffi/mod.rs deleted file mode 100644 index 5b49f50763d74..0000000000000 --- a/library/std/src/os/unix/ffi/mod.rs +++ /dev/null @@ -1,42 +0,0 @@ -//! Unix-specific extensions to primitives in the [`std::ffi`] module. -//! -//! # Examples -//! -//! ``` -//! use std::ffi::OsString; -//! use std::os::unix::ffi::OsStringExt; -//! -//! let bytes = b"foo".to_vec(); -//! -//! // OsStringExt::from_vec -//! let os_string = OsString::from_vec(bytes); -//! assert_eq!(os_string.to_str(), Some("foo")); -//! -//! // OsStringExt::into_vec -//! let bytes = os_string.into_vec(); -//! assert_eq!(bytes, b"foo"); -//! ``` -//! -//! ``` -//! use std::ffi::OsStr; -//! use std::os::unix::ffi::OsStrExt; -//! -//! let bytes = b"foo"; -//! -//! // OsStrExt::from_bytes -//! let os_str = OsStr::from_bytes(bytes); -//! assert_eq!(os_str.to_str(), Some("foo")); -//! -//! // OsStrExt::as_bytes -//! let bytes = os_str.as_bytes(); -//! assert_eq!(bytes, b"foo"); -//! ``` -//! -//! [`std::ffi`]: crate::ffi - -#![stable(feature = "rust1", since = "1.0.0")] - -mod os_str; - -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::os_str::{OsStrExt, OsStringExt}; diff --git a/library/std/src/os/unix/ffi/os_str.rs b/library/std/src/os/unix/ffi/os_str.rs deleted file mode 100644 index 650f712bc6eef..0000000000000 --- a/library/std/src/os/unix/ffi/os_str.rs +++ /dev/null @@ -1,70 +0,0 @@ -use crate::ffi::{OsStr, OsString}; -use crate::mem; -use crate::sealed::Sealed; -use crate::sys::os_str::Buf; -use crate::sys_common::{AsInner, FromInner, IntoInner}; - -// Note: this file is currently reused in other `std::os::{platform}::ffi` modules to reduce duplication. -// Keep this in mind when applying changes to this file that only apply to `unix`. - -/// Platform-specific extensions to [`OsString`]. -/// -/// This trait is sealed: it cannot be implemented outside the standard library. -/// This is so that future additional methods are not breaking changes. -#[stable(feature = "rust1", since = "1.0.0")] -pub trait OsStringExt: Sealed { - /// Creates an [`OsString`] from a byte vector. - /// - /// See the module documentation for an example. - #[stable(feature = "rust1", since = "1.0.0")] - fn from_vec(vec: Vec) -> Self; - - /// Yields the underlying byte vector of this [`OsString`]. - /// - /// See the module documentation for an example. - #[stable(feature = "rust1", since = "1.0.0")] - fn into_vec(self) -> Vec; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl OsStringExt for OsString { - #[inline] - fn from_vec(vec: Vec) -> OsString { - FromInner::from_inner(Buf { inner: vec }) - } - #[inline] - fn into_vec(self) -> Vec { - self.into_inner().inner - } -} - -/// Platform-specific extensions to [`OsStr`]. -/// -/// This trait is sealed: it cannot be implemented outside the standard library. -/// This is so that future additional methods are not breaking changes. -#[stable(feature = "rust1", since = "1.0.0")] -pub trait OsStrExt: Sealed { - #[stable(feature = "rust1", since = "1.0.0")] - /// Creates an [`OsStr`] from a byte slice. - /// - /// See the module documentation for an example. - fn from_bytes(slice: &[u8]) -> &Self; - - /// Gets the underlying byte view of the [`OsStr`] slice. - /// - /// See the module documentation for an example. - #[stable(feature = "rust1", since = "1.0.0")] - fn as_bytes(&self) -> &[u8]; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl OsStrExt for OsStr { - #[inline] - fn from_bytes(slice: &[u8]) -> &OsStr { - unsafe { mem::transmute(slice) } - } - #[inline] - fn as_bytes(&self) -> &[u8] { - &self.as_inner().inner - } -} diff --git a/library/std/src/os/unix/mod.rs b/library/std/src/os/unix/mod.rs index 78c957270c451..4013088407977 100644 --- a/library/std/src/os/unix/mod.rs +++ b/library/std/src/os/unix/mod.rs @@ -87,6 +87,7 @@ mod platform { pub use crate::os::vxworks::*; } +#[path = "../../sys/ffi/bytes.rs"] pub mod ffi; pub mod fs; pub mod io; diff --git a/library/std/src/os/wasi/ffi.rs b/library/std/src/os/wasi/ffi.rs deleted file mode 100644 index 41dd8702e98e9..0000000000000 --- a/library/std/src/os/wasi/ffi.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! WASI-specific extensions to primitives in the [`std::ffi`] module -//! -//! [`std::ffi`]: crate::ffi - -#![stable(feature = "rust1", since = "1.0.0")] - -#[path = "../unix/ffi/os_str.rs"] -mod os_str; - -#[stable(feature = "rust1", since = "1.0.0")] -pub use self::os_str::{OsStrExt, OsStringExt}; diff --git a/library/std/src/os/wasi/mod.rs b/library/std/src/os/wasi/mod.rs index 2ee6aa4660094..15f142f5089ba 100644 --- a/library/std/src/os/wasi/mod.rs +++ b/library/std/src/os/wasi/mod.rs @@ -33,6 +33,7 @@ #![forbid(unsafe_op_in_unsafe_fn)] #![doc(cfg(target_os = "wasi"))] +#[path = "../../sys/ffi/bytes.rs"] pub mod ffi; pub mod fs; pub mod io; diff --git a/library/std/src/os/windows/mod.rs b/library/std/src/os/windows/mod.rs index f452403ee8426..2923d89656942 100644 --- a/library/std/src/os/windows/mod.rs +++ b/library/std/src/os/windows/mod.rs @@ -26,6 +26,7 @@ #![doc(cfg(windows))] #![deny(unsafe_op_in_unsafe_fn)] +#[path = "../../sys/ffi/windows.rs"] pub mod ffi; pub mod fs; pub mod io; diff --git a/library/std/src/os/xous/ffi.rs b/library/std/src/os/xous/ffi.rs index 9394f0a0496b2..cbc5d6b118711 100644 --- a/library/std/src/os/xous/ffi.rs +++ b/library/std/src/os/xous/ffi.rs @@ -2,7 +2,7 @@ #![allow(unused_variables)] #![stable(feature = "rust1", since = "1.0.0")] -#[path = "../unix/ffi/os_str.rs"] +#[path = "../../sys/ffi/bytes.rs"] mod os_str; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/sys/ffi/bytes.rs b/library/std/src/sys/ffi/bytes.rs new file mode 100644 index 0000000000000..5fb63532c04c0 --- /dev/null +++ b/library/std/src/sys/ffi/bytes.rs @@ -0,0 +1,143 @@ +// Note: This file is publicly exported as various modules to reduce +// duplication. Currently they are: +// - `std::os::unix::ffi` +// - `std::os::hermit::ffi` +// - `std::os::fortanix_sgx::ffi` +// - `std::os::solid::ffi` +// - `std::os::wasi::ffi` +// - `std::os::xous::ffi` + +#![doc = cfg_select! { + target_family = "unix" => "Unix-specific", + target_os = "hermit" => "HermitCore-specific", + all(target_vendor = "fortanix", target_env = "sgx") => "SGX-specific", + target_os = "solid_asp3" => "SOLID-specific", + target_os = "wasi" => "WASI-specific", + target_os = "xous" => "Xous-specific", +}] +//! extensions to primitives in the [`std::ffi`] module. +//! +//! # Examples +//! +//! ``` +//! use std::ffi::OsString; +#![doc = cfg_select! { + target_family = "unix" => "use std::os::unix::ffi::OsStringExt;", + target_os = "hermit" => "use std::os::hermit::ffi::OsStringExt;", + all(target_vendor = "fortanix", target_env = "sgx") => "use std::os::fortanix_sgx::ffi::OsStringExt;", + target_os = "solid_asp3" => "use std::os::solid::ffi::OsStringExt;", + target_os = "wasi" => "use std::os::wasi::ffi::OsStringExt;", + target_os = "xous" => "use std::os::xous::ffi::OsStringExt;", +}] +//! +//! let bytes = b"foo".to_vec(); +//! +//! // OsStringExt::from_vec +//! let os_string = OsString::from_vec(bytes); +//! assert_eq!(os_string.to_str(), Some("foo")); +//! +//! // OsStringExt::into_vec +//! let bytes = os_string.into_vec(); +//! assert_eq!(bytes, b"foo"); +//! ``` +//! +//! ``` +//! use std::ffi::OsStr; +#![doc = cfg_select! { + target_family = "unix" => "use std::os::unix::ffi::OsStrExt;", + target_os = "hermit" => "use std::os::hermit::ffi::OsStrExt;", + all(target_vendor = "fortanix", target_env = "sgx") => "use std::os::fortanix_sgx::ffi::OsStrExt;", + target_os = "solid_asp3" => "use std::os::solid::ffi::OsStrExt;", + target_os = "wasi" => "use std::os::wasi::ffi::OsStrExt;", + target_os = "xous" => "use std::os::xous::ffi::OsStrExt;", +}] +//! +//! let bytes = b"foo"; +//! +//! // OsStrExt::from_bytes +//! let os_str = OsStr::from_bytes(bytes); +//! assert_eq!(os_str.to_str(), Some("foo")); +//! +//! // OsStrExt::as_bytes +//! let bytes = os_str.as_bytes(); +//! assert_eq!(bytes, b"foo"); +//! ``` +//! +//! [`std::ffi`]: crate::ffi + +#![cfg_attr( + not(all(target_vendor = "fortanix", target_env = "sgx")), + stable(feature = "rust1", since = "1.0.0") +)] +#![cfg_attr( + all(target_vendor = "fortanix", target_env = "sgx"), + unstable(feature = "sgx_platform", issue = "56975") +)] + +use crate::ffi::{OsStr, OsString}; +use crate::mem; +use crate::sealed::Sealed; +use crate::sys::os_str::Buf; +use crate::sys_common::{AsInner, FromInner, IntoInner}; + +/// Platform-specific extensions to [`OsString`]. +/// +/// This trait is sealed: it cannot be implemented outside the standard library. +/// This is so that future additional methods are not breaking changes. +#[stable(feature = "rust1", since = "1.0.0")] +pub trait OsStringExt: Sealed { + /// Creates an [`OsString`] from a byte vector. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn from_vec(vec: Vec) -> Self; + + /// Yields the underlying byte vector of this [`OsString`]. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn into_vec(self) -> Vec; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl OsStringExt for OsString { + #[inline] + fn from_vec(vec: Vec) -> OsString { + FromInner::from_inner(Buf { inner: vec }) + } + #[inline] + fn into_vec(self) -> Vec { + self.into_inner().inner + } +} + +/// Platform-specific extensions to [`OsStr`]. +/// +/// This trait is sealed: it cannot be implemented outside the standard library. +/// This is so that future additional methods are not breaking changes. +#[stable(feature = "rust1", since = "1.0.0")] +pub trait OsStrExt: Sealed { + #[stable(feature = "rust1", since = "1.0.0")] + /// Creates an [`OsStr`] from a byte slice. + /// + /// See the module documentation for an example. + fn from_bytes(slice: &[u8]) -> &Self; + + /// Gets the underlying byte view of the [`OsStr`] slice. + /// + /// See the module documentation for an example. + #[stable(feature = "rust1", since = "1.0.0")] + fn as_bytes(&self) -> &[u8]; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl OsStrExt for OsStr { + #[inline] + fn from_bytes(slice: &[u8]) -> &OsStr { + unsafe { mem::transmute(slice) } + } + #[inline] + fn as_bytes(&self) -> &[u8] { + &self.as_inner().inner + } +} diff --git a/library/std/src/os/motor/ffi.rs b/library/std/src/sys/ffi/utf8.rs similarity index 88% rename from library/std/src/os/motor/ffi.rs rename to library/std/src/sys/ffi/utf8.rs index 10e8da392dcc5..e27644b471ce9 100644 --- a/library/std/src/os/motor/ffi.rs +++ b/library/std/src/sys/ffi/utf8.rs @@ -1,3 +1,7 @@ +// Note: This file is publicly exported as various modules to reduce +// duplication. Currently it is only `std::os::motor::ffi`, so the documentation +// is specific to Motor OS. + //! Motor OS-specific extensions to primitives in the [`std::ffi`] module. #![unstable(feature = "motor_ext", issue = "147456")] diff --git a/library/std/src/os/windows/ffi.rs b/library/std/src/sys/ffi/windows.rs similarity index 97% rename from library/std/src/os/windows/ffi.rs rename to library/std/src/sys/ffi/windows.rs index 20e5383dc09e0..0842fbf16d34d 100644 --- a/library/std/src/os/windows/ffi.rs +++ b/library/std/src/sys/ffi/windows.rs @@ -1,3 +1,8 @@ +// Note: This file is publicly exported as various modules to reduce +// duplication. Currently they are: +// - `std::os::windows::ffi` +// - `std::os::uefi::ffi` + //! Windows-specific extensions to primitives in the [`std::ffi`] module. //! //! # Overview