Skip to content

Commit 30bc78b

Browse files
committed
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.
1 parent 6bee30e commit 30bc78b

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

src/lib.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,10 @@ mod macros;
3232
cfg_if! {
3333
if #[cfg(feature = "rustc-dep-of-std")] {
3434
extern crate rustc_std_workspace_core as core;
35-
#[allow(unused_imports)]
36-
use core::iter;
37-
#[allow(unused_imports)]
38-
use core::ops;
39-
#[allow(unused_imports)]
40-
use core::option;
4135
}
4236
}
4337

44-
#[doc(hidden)]
45-
#[allow(unused_imports)]
46-
use core::clone::Clone;
47-
#[allow(unused_imports)]
48-
use core::ffi;
4938
pub use core::ffi::c_void;
50-
#[allow(unused_imports)]
51-
use core::fmt;
52-
#[allow(unused_imports)]
53-
use core::hash;
54-
#[doc(hidden)]
55-
#[allow(unused_imports)]
56-
use core::marker::{Copy, Send, Sync};
57-
#[allow(unused_imports)]
58-
use core::mem;
59-
#[allow(unused_imports)]
60-
use core::num;
61-
#[doc(hidden)]
62-
#[allow(unused_imports)]
63-
use core::option::Option;
6439

6540
cfg_if! {
6641
if #[cfg(windows)] {
@@ -69,72 +44,96 @@ cfg_if! {
6944

7045
mod windows;
7146
pub use crate::windows::*;
47+
48+
prelude!();
7249
} else if #[cfg(target_os = "fuchsia")] {
7350
mod fixed_width_ints;
7451
pub use crate::fixed_width_ints::*;
7552

7653
mod fuchsia;
7754
pub use crate::fuchsia::*;
55+
56+
prelude!();
7857
} else if #[cfg(target_os = "switch")] {
7958
mod fixed_width_ints;
8059
pub use fixed_width_ints::*;
8160

8261
mod switch;
8362
pub use switch::*;
63+
64+
prelude!();
8465
} else if #[cfg(target_os = "vxworks")] {
8566
mod fixed_width_ints;
8667
pub use crate::fixed_width_ints::*;
8768

8869
mod vxworks;
8970
pub use crate::vxworks::*;
71+
72+
prelude!();
9073
} else if #[cfg(target_os = "solid_asp3")] {
9174
mod fixed_width_ints;
9275
pub use crate::fixed_width_ints::*;
9376

9477
mod solid;
9578
pub use crate::solid::*;
79+
80+
prelude!();
9681
} else if #[cfg(unix)] {
9782
mod fixed_width_ints;
9883
pub use crate::fixed_width_ints::*;
9984

10085
mod unix;
10186
pub use crate::unix::*;
87+
88+
prelude!();
10289
} else if #[cfg(target_os = "hermit")] {
10390
mod fixed_width_ints;
10491
pub use crate::fixed_width_ints::*;
10592

10693
mod hermit;
10794
pub use crate::hermit::*;
95+
96+
prelude!();
10897
} else if #[cfg(target_os = "teeos")] {
10998
mod fixed_width_ints;
11099
pub use fixed_width_ints::*;
111100

112101
mod teeos;
113102
pub use teeos::*;
103+
104+
prelude!();
114105
} else if #[cfg(target_os = "trusty")] {
115106
mod fixed_width_ints;
116107
pub use crate::fixed_width_ints::*;
117108

118109
mod trusty;
119110
pub use crate::trusty::*;
111+
112+
prelude!();
120113
} else if #[cfg(all(target_env = "sgx", target_vendor = "fortanix"))] {
121114
mod fixed_width_ints;
122115
pub use crate::fixed_width_ints::*;
123116

124117
mod sgx;
125118
pub use crate::sgx::*;
119+
120+
prelude!();
126121
} else if #[cfg(any(target_env = "wasi", target_os = "wasi"))] {
127122
mod fixed_width_ints;
128123
pub use crate::fixed_width_ints::*;
129124

130125
mod wasi;
131126
pub use crate::wasi::*;
127+
128+
prelude!();
132129
} else if #[cfg(target_os = "xous")] {
133130
mod fixed_width_ints;
134131
pub use crate::fixed_width_ints::*;
135132

136133
mod xous;
137134
pub use crate::xous::*;
135+
136+
prelude!();
138137
} else {
139138
// non-supported targets: empty...
140139
}

src/macros.rs

+27
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ macro_rules! cfg_if {
6161
};
6262
}
6363

64+
/// Create an internal crate prelude with `core` reexports and common types.
65+
macro_rules! prelude {
66+
() => {
67+
/// Frequently-used types that are available on all platforms
68+
///
69+
/// We need to reexport the core types so this works with `rust-dep-of-std`.
70+
mod prelude {
71+
// Exports from `core`
72+
#[allow(unused_imports)]
73+
pub(crate) use core::clone::Clone;
74+
#[allow(unused_imports)]
75+
pub(crate) use core::marker::{Copy, Send, Sync};
76+
#[allow(unused_imports)]
77+
pub(crate) use core::option::Option;
78+
#[allow(unused_imports)]
79+
pub(crate) use core::{fmt, hash, iter, mem};
80+
81+
// Commonly used types defined in this crate
82+
#[allow(unused_imports)]
83+
pub(crate) use crate::{
84+
c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar, c_uint,
85+
c_ulong, c_ulonglong, c_ushort, c_void, intptr_t, size_t, ssize_t, uintptr_t,
86+
};
87+
}
88+
};
89+
}
90+
6491
/// Implement `Clone` and `Copy` for a struct, as well as `Debug`, `Eq`, `Hash`, and
6592
/// `PartialEq` if the `extra_traits` feature is enabled.
6693
///

0 commit comments

Comments
 (0)