From 877b6f6ebb9626a6469e725f065f84e22962b3b5 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Wed, 27 Nov 2024 04:20:12 -0500 Subject: [PATCH] Create an internal prelude When building with `rustc-dep-of-std`, we don't get the core types imported by default (`Clone`, `Copy`, `Option`). In order to avoid needing to import these individually, introduce a prelude that includes them, along with commonly used C numeric types. This allows cleaning up some of the `use` statements. (backport ) (cherry picked from commit 30bc78b2ccc2d620cdd1f2ac16be083eff6dcabc) --- src/lib.rs | 51 ++++++++++++++++++++++++++------------------------- src/macros.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f95e9ba6afd3..1be0a003382d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,35 +33,10 @@ mod macros; cfg_if! { if #[cfg(feature = "rustc-dep-of-std")] { extern crate rustc_std_workspace_core as core; - #[allow(unused_imports)] - use core::iter; - #[allow(unused_imports)] - use core::ops; - #[allow(unused_imports)] - use core::option; } } -#[doc(hidden)] -#[allow(unused_imports)] -use core::clone::Clone; -#[allow(unused_imports)] -use core::ffi; pub use core::ffi::c_void; -#[allow(unused_imports)] -use core::fmt; -#[allow(unused_imports)] -use core::hash; -#[doc(hidden)] -#[allow(unused_imports)] -use core::marker::{Copy, Send, Sync}; -#[allow(unused_imports)] -use core::mem; -#[allow(unused_imports)] -use core::num; -#[doc(hidden)] -#[allow(unused_imports)] -use core::option::Option; cfg_if! { if #[cfg(windows)] { @@ -70,78 +45,104 @@ cfg_if! { mod windows; pub use crate::windows::*; + + prelude!(); } else if #[cfg(target_os = "fuchsia")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod fuchsia; pub use crate::fuchsia::*; + + prelude!(); } else if #[cfg(target_os = "switch")] { mod fixed_width_ints; pub use fixed_width_ints::*; mod switch; pub use switch::*; + + prelude!(); } else if #[cfg(target_os = "psp")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod psp; pub use crate::psp::*; + + prelude!(); } else if #[cfg(target_os = "vxworks")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod vxworks; pub use crate::vxworks::*; + + prelude!(); } else if #[cfg(target_os = "solid_asp3")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod solid; pub use crate::solid::*; + + prelude!(); } else if #[cfg(unix)] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod unix; pub use crate::unix::*; + + prelude!(); } else if #[cfg(target_os = "hermit")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod hermit; pub use crate::hermit::*; + + prelude!(); } else if #[cfg(target_os = "teeos")] { mod fixed_width_ints; pub use fixed_width_ints::*; mod teeos; pub use teeos::*; + + prelude!(); } else if #[cfg(target_os = "trusty")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod trusty; pub use crate::trusty::*; + + prelude!(); } else if #[cfg(all(target_env = "sgx", target_vendor = "fortanix"))] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod sgx; pub use crate::sgx::*; + + prelude!(); } else if #[cfg(any(target_env = "wasi", target_os = "wasi"))] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod wasi; pub use crate::wasi::*; + + prelude!(); } else if #[cfg(target_os = "xous")] { mod fixed_width_ints; pub use crate::fixed_width_ints::*; mod xous; pub use crate::xous::*; + + prelude!(); } else { // non-supported targets: empty... } diff --git a/src/macros.rs b/src/macros.rs index 354f42a56880..2ea084398da4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -61,6 +61,33 @@ macro_rules! cfg_if { }; } +/// Create an internal crate prelude with `core` reexports and common types. +macro_rules! prelude { + () => { + /// Frequently-used types that are available on all platforms + /// + /// We need to reexport the core types so this works with `rust-dep-of-std`. + mod prelude { + // Exports from `core` + #[allow(unused_imports)] + pub(crate) use core::clone::Clone; + #[allow(unused_imports)] + pub(crate) use core::marker::{Copy, Send, Sync}; + #[allow(unused_imports)] + pub(crate) use core::option::Option; + #[allow(unused_imports)] + pub(crate) use core::{fmt, hash, iter, mem}; + + // Commonly used types defined in this crate + #[allow(unused_imports)] + pub(crate) use crate::{ + c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar, c_uint, + c_ulong, c_ulonglong, c_ushort, c_void, intptr_t, size_t, ssize_t, uintptr_t, + }; + } + }; +} + /// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and /// `PartialEq` if the `extra_traits` feature is enabled. ///