Skip to content

Commit

Permalink
Create an internal prelude
Browse files Browse the repository at this point in the history
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 <rust-lang#4161>)
(cherry picked from commit 30bc78b)
  • Loading branch information
tgross35 committed Nov 28, 2024
1 parent 1abd74b commit 877b6f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)] {
Expand All @@ -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...
}
Expand Down
27 changes: 27 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 877b6f6

Please sign in to comment.