Skip to content

Commit 877b6f6

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. (backport <#4161>) (cherry picked from commit 30bc78b)
1 parent 1abd74b commit 877b6f6

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

src/lib.rs

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

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

6641
cfg_if! {
6742
if #[cfg(windows)] {
@@ -70,78 +45,104 @@ cfg_if! {
7045

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

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

8362
mod switch;
8463
pub use switch::*;
64+
65+
prelude!();
8566
} else if #[cfg(target_os = "psp")] {
8667
mod fixed_width_ints;
8768
pub use crate::fixed_width_ints::*;
8869

8970
mod psp;
9071
pub use crate::psp::*;
72+
73+
prelude!();
9174
} else if #[cfg(target_os = "vxworks")] {
9275
mod fixed_width_ints;
9376
pub use crate::fixed_width_ints::*;
9477

9578
mod vxworks;
9679
pub use crate::vxworks::*;
80+
81+
prelude!();
9782
} else if #[cfg(target_os = "solid_asp3")] {
9883
mod fixed_width_ints;
9984
pub use crate::fixed_width_ints::*;
10085

10186
mod solid;
10287
pub use crate::solid::*;
88+
89+
prelude!();
10390
} else if #[cfg(unix)] {
10491
mod fixed_width_ints;
10592
pub use crate::fixed_width_ints::*;
10693

10794
mod unix;
10895
pub use crate::unix::*;
96+
97+
prelude!();
10998
} else if #[cfg(target_os = "hermit")] {
11099
mod fixed_width_ints;
111100
pub use crate::fixed_width_ints::*;
112101

113102
mod hermit;
114103
pub use crate::hermit::*;
104+
105+
prelude!();
115106
} else if #[cfg(target_os = "teeos")] {
116107
mod fixed_width_ints;
117108
pub use fixed_width_ints::*;
118109

119110
mod teeos;
120111
pub use teeos::*;
112+
113+
prelude!();
121114
} else if #[cfg(target_os = "trusty")] {
122115
mod fixed_width_ints;
123116
pub use crate::fixed_width_ints::*;
124117

125118
mod trusty;
126119
pub use crate::trusty::*;
120+
121+
prelude!();
127122
} else if #[cfg(all(target_env = "sgx", target_vendor = "fortanix"))] {
128123
mod fixed_width_ints;
129124
pub use crate::fixed_width_ints::*;
130125

131126
mod sgx;
132127
pub use crate::sgx::*;
128+
129+
prelude!();
133130
} else if #[cfg(any(target_env = "wasi", target_os = "wasi"))] {
134131
mod fixed_width_ints;
135132
pub use crate::fixed_width_ints::*;
136133

137134
mod wasi;
138135
pub use crate::wasi::*;
136+
137+
prelude!();
139138
} else if #[cfg(target_os = "xous")] {
140139
mod fixed_width_ints;
141140
pub use crate::fixed_width_ints::*;
142141

143142
mod xous;
144143
pub use crate::xous::*;
144+
145+
prelude!();
145146
} else {
146147
// non-supported targets: empty...
147148
}

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)