Rollup merge of #150047 - joboet:remove-sys_common, r=Mark-Simulacrum

std: merge `sys::pal::common` and `sys_common` into `sys::helpers`

As per rust-lang/rust#117276, this removes `sys_common` and moves its functionality and that of `sys::pal::common` into a newly created `sys::helpers` module.
This commit is contained in:
Jonathan Brouwer 2026-01-02 23:13:21 +01:00 committed by GitHub
commit 418beb48c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 87 additions and 140 deletions

View file

@ -681,9 +681,7 @@ pub mod arch {
#[stable(feature = "simd_x86", since = "1.27.0")]
pub use std_detect::is_x86_feature_detected;
// Platform-abstraction modules
mod sys;
mod sys_common;
pub mod alloc;

View file

@ -11,11 +11,11 @@ use crate::ffi::{OsStr, OsString};
use crate::num::NonZero;
use crate::os::windows::prelude::*;
use crate::path::{Path, PathBuf};
use crate::sys::helpers::WStrUnits;
use crate::sys::pal::os::current_exe;
use crate::sys::pal::{ensure_no_nuls, fill_utf16_buf};
use crate::sys::path::get_long_path;
use crate::sys::{AsInner, c, to_u16s};
use crate::sys_common::wstr::WStrUnits;
use crate::{io, iter, ptr};
pub fn args() -> Args {

View file

@ -1,3 +1,7 @@
//! The configure builtins provides runtime support compiler-builtin features
//! which require dynamic initialization to work as expected, e.g. aarch64
//! outline-atomics.
/// Enable LSE atomic operations at startup, if supported.
///
/// Linker sections are based on what [`ctor`] does, with priorities to run slightly before user

View file

@ -6,7 +6,7 @@ use crate::io;
use crate::os::raw::{c_char, c_int};
use crate::os::solid::ffi::{OsStrExt, OsStringExt};
use crate::sync::{PoisonError, RwLock};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;
static ENV_LOCK: RwLock<()> = RwLock::new(());

View file

@ -24,7 +24,7 @@ mod uefi_env {
use crate::io;
use crate::os::uefi::ffi::OsStringExt;
use crate::ptr::NonNull;
use crate::sys::{helpers, unsupported_err};
use crate::sys::pal::{helpers, unsupported_err};
pub(crate) fn get(key: &OsStr) -> Option<OsString> {
let shell = helpers::open_shell()?;

View file

@ -7,8 +7,8 @@ use crate::ffi::{CStr, OsStr, OsString};
use crate::io;
use crate::os::unix::prelude::*;
use crate::sync::{PoisonError, RwLock};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_with_cstr;
// Use `_NSGetEnviron` on Apple platforms.
//

View file

@ -4,7 +4,7 @@ pub use super::common::Env;
use crate::ffi::{CStr, OsStr, OsString};
use crate::io;
use crate::os::wasi::prelude::*;
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;
use crate::sys::pal::os::{cvt, libc};
cfg_select! {

View file

@ -3,7 +3,7 @@
use crate::fs;
use crate::io::{self, Error, ErrorKind};
use crate::path::Path;
use crate::sys_common::ignore_notfound;
use crate::sys::helpers::ignore_notfound;
pub(crate) const NOT_FILE_ERROR: Error = io::const_error!(
ErrorKind::InvalidInput,

View file

@ -9,9 +9,9 @@ use crate::os::hermit::hermit_abi::{
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::{copy, exists};
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, cvt, unsupported, unsupported_err};
use crate::{fmt, mem};

View file

@ -17,7 +17,7 @@ cfg_select! {
pub(crate) use unix::debug_assert_fd_is_open;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(super) use unix::CachedFileMetadata;
use crate::sys::common::small_c_string::run_path_with_cstr as with_native_path;
use crate::sys::helpers::run_path_with_cstr as with_native_path;
}
target_os = "windows" => {
mod windows;

View file

@ -10,10 +10,10 @@ use crate::os::solid::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
pub use crate::sys::fs::common::exists;
use crate::sys::helpers::ignore_notfound;
use crate::sys::pal::{abi, error};
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, unsupported_err};
use crate::sys_common::ignore_notfound;
type CIntNotMinusOne = core::num::niche_types::NotAllOnes<c_int>;

View file

@ -6,8 +6,8 @@ use crate::fs::TryLockError;
use crate::hash::Hash;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::path::{Path, PathBuf};
use crate::sys::pal::{helpers, unsupported};
use crate::sys::time::SystemTime;
use crate::sys::{helpers, unsupported};
const FILE_PERMISSIONS_MASK: u64 = r_efi::protocols::file::READ_ONLY;
@ -497,7 +497,7 @@ mod uefi_fs {
use crate::os::uefi::ffi::OsStringExt;
use crate::path::Path;
use crate::ptr::NonNull;
use crate::sys::helpers::{self, UefiBox};
use crate::sys::pal::helpers::{self, UefiBox};
use crate::sys::time::{self, SystemTime};
pub(crate) struct File {

View file

@ -89,9 +89,9 @@ use crate::os::unix::prelude::*;
use crate::os::wasi::prelude::*;
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::exists;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
#[cfg(all(target_os = "linux", target_env = "gnu"))]
use crate::sys::weak::syscall;
@ -2498,9 +2498,8 @@ mod remove_dir_impl {
use crate::ffi::CStr;
use crate::io;
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::{ignore_notfound, run_path_with_cstr};
use crate::sys::{cvt, cvt_r};
use crate::sys_common::ignore_notfound;
pub fn openat_nofollow_dironly(parent_fd: Option<RawFd>, p: &CStr) -> io::Result<OwnedFd> {
let fd = cvt_r(|| unsafe {

View file

@ -4,7 +4,7 @@ use crate::fs::TryLockError;
use crate::hash::Hash;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, unsupported_err};

View file

@ -0,0 +1,39 @@
//! Small helper functions used inside `sys`.
//!
//! If any of these have uses outside of `sys`, please move them to a different
//! module.
#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
mod small_c_string;
#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
mod wstr;
#[cfg(test)]
mod tests;
#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
pub use small_c_string::{run_path_with_cstr, run_with_cstr};
#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
pub use wstr::WStrUnits;
/// Computes `(value*numerator)/denom` without overflow, as long as both
/// `numerator*denom` and the overall result fit into `u64` (which is the case
/// for our time conversions).
#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
let q = value / denom;
let r = value % denom;
// Decompose value as (value/denom*denom + value%denom),
// substitute into (value*numerator)/denom and simplify.
// r < denom, so (denom*numerator) is the upper bound of (r*numerator)
q * numerator + r * numerator / denom
}
#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
match result {
Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}

View file

@ -1,9 +1,10 @@
use core::iter::repeat;
use super::mul_div_u64;
use super::small_c_string::run_path_with_cstr;
use crate::ffi::CString;
use crate::hint::black_box;
use crate::path::Path;
use crate::sys::common::small_c_string::run_path_with_cstr;
#[test]
fn stack_allocation_works() {
@ -65,3 +66,8 @@ fn bench_heap_path_alloc(b: &mut test::Bencher) {
.unwrap();
});
}
#[test]
fn test_muldiv() {
assert_eq!(mul_div_u64(1_000_000_000_001, 1_000_000_000, 1_000_000), 1_000_000_000_001_000);
}

View file

@ -1,5 +1,4 @@
//! This module contains constructs to work with 16-bit characters (UCS-2 or UTF-16)
#![allow(dead_code)]
use crate::marker::PhantomData;
use crate::num::NonZero;

View file

@ -1,16 +1,9 @@
#![allow(unsafe_op_in_unsafe_fn)]
/// The configure builtins provides runtime support compiler-builtin features
/// which require dynamic initialization to work as expected, e.g. aarch64
/// outline-atomics.
mod configure_builtins;
/// The PAL (platform abstraction layer) contains platform-specific abstractions
/// for implementing the features in the other submodules, e.g. UNIX file
/// descriptors.
mod pal;
mod alloc;
mod configure_builtins;
mod helpers;
mod pal;
mod personality;
pub mod args;

View file

@ -300,7 +300,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}
// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> RawFd {
self.0.as_raw_fd()
}

View file

@ -7,7 +7,7 @@ use crate::mem::MaybeUninit;
use crate::net::{
Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs,
};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;
use crate::sys::net::connection::each_addr;
use crate::sys::{AsInner, FromInner};
use crate::time::Duration;

View file

@ -353,7 +353,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}
// This method is used by sys_common code to abstract over targets.
pub fn as_raw(&self) -> c_int {
self.as_raw_fd()
}

View file

@ -614,7 +614,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}
// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> RawFd {
self.as_raw_fd()
}

View file

@ -439,7 +439,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}
// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> c::SOCKET {
debug_assert_eq!(size_of::<c::SOCKET>(), size_of::<RawSocket>());
debug_assert_eq!(align_of::<c::SOCKET>(), align_of::<RawSocket>());

View file

@ -2,7 +2,7 @@ use super::tcp4;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::net::SocketAddr;
use crate::ptr::NonNull;
use crate::sys::{helpers, unsupported};
use crate::sys::pal::{helpers, unsupported};
use crate::time::Duration;
pub(crate) enum Tcp {

View file

@ -1,16 +0,0 @@
// This module contains code that is shared between all platforms, mostly utility or fallback code.
// This explicitly does not include code that is shared between only a few platforms,
// such as when reusing an implementation from `unix` or `unsupported`.
// In those cases the desired code should be included directly using the #[path] attribute,
// not moved to this module.
//
// Currently `sys_common` contains a lot of code that should live in this module,
// ideally `sys_common` would only contain platform-independent abstractions on top of `sys`.
// Progress on this is tracked in #84187.
#![allow(dead_code)]
pub mod small_c_string;
#[cfg(test)]
mod tests;

View file

@ -1,29 +1,8 @@
//! Platform-dependent platform abstraction.
//!
//! The `std::sys` module is the abstracted interface through which
//! `std` talks to the underlying operating system. It has different
//! implementations for different operating system families, today
//! just Unix and Windows, and initial support for Redox.
//!
//! The centralization of platform-specific code in this module is
//! enforced by the "platform abstraction layer" tidy script in
//! `tools/tidy/src/pal.rs`.
//!
//! This module is closely related to the platform-independent system
//! integration code in `std::sys_common`. See that module's
//! documentation for details.
//!
//! In the future it would be desirable for the independent
//! implementations of this module to be extracted to their own crates
//! that `std` can link to, thus enabling their implementation
//! out-of-tree via crate replacement. Though due to the complex
//! inter-dependencies within `std` that will be a challenging goal to
//! achieve.
//! The PAL (platform abstraction layer) contains platform-specific abstractions
//! for implementing the features in the other submodules, such as e.g. bindings.
#![allow(missing_debug_implementations)]
pub mod common;
cfg_select! {
unix => {
mod unix;

View file

@ -24,7 +24,7 @@ use crate::path::Path;
use crate::ptr::NonNull;
use crate::slice;
use crate::sync::atomic::{Atomic, AtomicPtr, Ordering};
use crate::sys_common::wstr::WStrUnits;
use crate::sys::helpers::WStrUnits;
type BootInstallMultipleProtocolInterfaces =
unsafe extern "efiapi" fn(_: *mut r_efi::efi::Handle, _: ...) -> r_efi::efi::Status;

View file

@ -300,7 +300,7 @@ pub(crate) mod instant_internal {
use crate::mem::MaybeUninit;
use crate::ptr::NonNull;
use crate::sync::atomic::{Atomic, AtomicPtr, Ordering};
use crate::sys_common::mul_div_u64;
use crate::sys::helpers::mul_div_u64;
const NS_PER_SEC: u64 = 1_000_000_000;

View file

@ -10,8 +10,8 @@ use libc::{c_char, c_int, c_void};
use crate::ffi::{CStr, OsStr, OsString};
use crate::os::unix::prelude::*;
use crate::path::{self, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_path_with_cstr;
use crate::{fmt, io, iter, mem, ptr, slice, str};
const TMPBUF_SZ: usize = 128;

View file

@ -4,7 +4,7 @@ use crate::ffi::{CStr, OsStr, OsString};
use crate::marker::PhantomData;
use crate::os::wasi::prelude::*;
use crate::path::{self, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::unsupported;
use crate::{fmt, io, str};

View file

@ -179,8 +179,8 @@ fn intervals2dur(intervals: u64) -> Duration {
mod perf_counter {
use super::NANOS_PER_SEC;
use crate::sync::atomic::{Atomic, AtomicU64, Ordering};
use crate::sys::helpers::mul_div_u64;
use crate::sys::{c, cvt};
use crate::sys_common::mul_div_u64;
use crate::time::Duration;
pub struct PerformanceCounterInstant {

View file

@ -1,8 +1,8 @@
use crate::ffi::OsString;
use crate::os::unix::ffi::OsStringExt;
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_path_with_cstr;
use crate::{io, ptr};
#[inline]

View file

@ -2,7 +2,8 @@
use crate::ffi::OsStr;
use crate::io;
use crate::path::{Path, PathBuf, Prefix};
use crate::sys::{helpers, unsupported_err};
use crate::sys::pal::helpers;
use crate::sys::unsupported_err;
const FORWARD_SLASH: u8 = b'/';
const COLON: u8 = b':';

View file

@ -3,7 +3,7 @@
use super::root_relative;
use crate::ffi::{CStr, c_char, c_void};
use crate::ptr::null_mut;
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::run_path_with_cstr;
// MacTypes.h
pub(super) type Boolean = u8;

View file

@ -377,8 +377,8 @@ mod uefi_command_internal {
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
use crate::ptr::NonNull;
use crate::slice;
use crate::sys::helpers::WStrUnits;
use crate::sys::pal::helpers::{self, OwnedTable};
use crate::sys_common::wstr::WStrUnits;
pub struct Image {
handle: NonNull<crate::ffi::c_void>,

View file

@ -1,44 +0,0 @@
//! Platform-independent platform abstraction
//!
//! This is the platform-independent portion of the standard library's
//! platform abstraction layer, whereas `std::sys` is the
//! platform-specific portion.
//!
//! The relationship between `std::sys_common`, `std::sys` and the
//! rest of `std` is complex, with dependencies going in all
//! directions: `std` depending on `sys_common`, `sys_common`
//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
//! This is because `sys_common` not only contains platform-independent code,
//! but also code that is shared between the different platforms in `sys`.
//! Ideally all that shared code should be moved to `sys::common`,
//! and the dependencies between `std`, `sys_common` and `sys` all would form a DAG.
//! Progress on this is tracked in #84187.
#![allow(missing_docs)]
#[cfg(test)]
mod tests;
pub mod wstr;
// common error constructors
// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
// overall result fit into i64 (which is the case for our time conversions).
#[allow(dead_code)] // not used on all platforms
pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
let q = value / denom;
let r = value % denom;
// Decompose value as (value/denom*denom + value%denom),
// substitute into (value*numerator)/denom and simplify.
// r < denom, so (denom*numerator) is the upper bound of (r*numerator)
q * numerator + r * numerator / denom
}
pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
match result {
Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}

View file

@ -1,6 +0,0 @@
use super::mul_div_u64;
#[test]
fn test_muldiv() {
assert_eq!(mul_div_u64(1_000_000_000_001, 1_000_000_000, 1_000_000), 1_000_000_000_001_000);
}

View file

@ -15,12 +15,12 @@ LL | let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode a
at RUSTLIB/std/src/sys/fs/PLATFORM.rs:LL:CC
3: std::sys::fs::PLATFORM::File::open::{closure#0}
at RUSTLIB/std/src/sys/fs/PLATFORM.rs:LL:CC
4: std::sys::pal::PLATFORM::small_c_string::run_with_cstr_stack
at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
5: std::sys::pal::PLATFORM::small_c_string::run_with_cstr
at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
6: std::sys::pal::PLATFORM::small_c_string::run_path_with_cstr
at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
4: std::sys::helpers::small_c_string::run_with_cstr_stack
at RUSTLIB/std/src/sys/helpers/small_c_string.rs:LL:CC
5: std::sys::helpers::small_c_string::run_with_cstr
at RUSTLIB/std/src/sys/helpers/small_c_string.rs:LL:CC
6: std::sys::helpers::small_c_string::run_path_with_cstr
at RUSTLIB/std/src/sys/helpers/small_c_string.rs:LL:CC
7: std::sys::fs::PLATFORM::File::open
at RUSTLIB/std/src/sys/fs/PLATFORM.rs:LL:CC
8: std::fs::OpenOptions::_open

View file

@ -24,7 +24,6 @@
//! - `sys/`
//! - `os/`
//!
//! `std/sys_common` should _not_ contain platform-specific code.
//! Finally, because std contains tests with platform-specific
//! `ignore` attributes, once the parser encounters `mod tests`,
//! platform-specific cfgs are allowed. Not sure yet how to deal with