From 95af5cc7f02c0627a24941a774affa7243a8f92b Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Tue, 21 Oct 2025 18:46:38 -0600 Subject: [PATCH 1/2] Unify shared OsStrExt/OsStringExt traits --- library/std/src/os/fortanix_sgx/ffi.rs | 41 ------------------- library/std/src/os/fortanix_sgx/mod.rs | 1 + library/std/src/os/hermit/ffi.rs | 41 ------------------- library/std/src/os/hermit/mod.rs | 1 + library/std/src/os/motor/mod.rs | 1 + library/std/src/os/solid/ffi.rs | 41 ------------------- library/std/src/os/solid/mod.rs | 1 + library/std/src/os/uefi/mod.rs | 2 +- library/std/src/os/unix/mod.rs | 1 + library/std/src/os/wasi/ffi.rs | 11 ----- library/std/src/os/wasi/mod.rs | 1 + library/std/src/os/windows/mod.rs | 1 + library/std/src/os/xous/ffi.rs | 2 +- .../unix/ffi/os_str.rs => sys/ffi/bytes.rs} | 21 ++++++++-- .../src/{os/motor/ffi.rs => sys/ffi/utf8.rs} | 4 ++ .../{os/windows/ffi.rs => sys/ffi/windows.rs} | 5 +++ 16 files changed, 36 insertions(+), 139 deletions(-) delete mode 100644 library/std/src/os/fortanix_sgx/ffi.rs delete mode 100644 library/std/src/os/hermit/ffi.rs delete mode 100644 library/std/src/os/solid/ffi.rs delete mode 100644 library/std/src/os/wasi/ffi.rs rename library/std/src/{os/unix/ffi/os_str.rs => sys/ffi/bytes.rs} (79%) rename library/std/src/{os/motor/ffi.rs => sys/ffi/utf8.rs} (88%) rename library/std/src/{os/windows/ffi.rs => sys/ffi/windows.rs} (97%) 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/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/os/unix/ffi/os_str.rs b/library/std/src/sys/ffi/bytes.rs similarity index 79% rename from library/std/src/os/unix/ffi/os_str.rs rename to library/std/src/sys/ffi/bytes.rs index 650f712bc6eef..12e80629d32a5 100644 --- a/library/std/src/os/unix/ffi/os_str.rs +++ b/library/std/src/sys/ffi/bytes.rs @@ -1,12 +1,27 @@ +// 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` + +#![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}; -// 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. 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 From b55973d247d3a20cb33021e8038faf20776b06ca Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Tue, 21 Oct 2025 19:23:18 -0600 Subject: [PATCH 2/2] Unify docs for bytes OsStrExt/OsStringExt platforms --- library/std/src/os/unix/ffi/mod.rs | 42 ---------------------- library/std/src/sys/ffi/bytes.rs | 58 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 42 deletions(-) delete mode 100644 library/std/src/os/unix/ffi/mod.rs 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/sys/ffi/bytes.rs b/library/std/src/sys/ffi/bytes.rs index 12e80629d32a5..5fb63532c04c0 100644 --- a/library/std/src/sys/ffi/bytes.rs +++ b/library/std/src/sys/ffi/bytes.rs @@ -7,6 +7,64 @@ // - `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")