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.
  • Loading branch information
tgross35 committed Nov 27, 2024
1 parent 6bee30e commit 30bc78b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,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 @@ -69,72 +44,96 @@ 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 = "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 30bc78b

Please sign in to comment.