-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathlib.rs
More file actions
331 lines (310 loc) · 9.94 KB
/
lib.rs
File metadata and controls
331 lines (310 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#![no_std]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
//! # Supported bit sizes
//!
//! This crate supports the following bit sizes:
//! - `16`
//! - `32`
//! - `64`
//!
//! This matches the available options for `target_pointer_width` in `rustc`:
//! ```text
//! expected values for `target_pointer_width` are: `16`, `32`, and `64`
//! ```
//!
//! # Example
//!
//! See the [`cpubits`] macro itself for more detailed usage examples including other syntax
//! variations.
//!
//! ```
//! cpubits::cpubits! {
//! 16 => { pub type Word = u16; }
//! 32 => { pub type Word = u32; }
//! 64 => { pub type Word = u64; }
//! }
//! ```
// End of toplevel rustdoc, beginning of macro documentation. We put the detailed docs on the macro
// itself so we can re-export it, and people can easily get to these docs from the re-exported
// version.
/// # Usage
///
/// The macro works sort of like a `match` expression that takes an implicit number of CPU bits,
/// which is one of `16`, `32`, or `64`.
///
/// Use this macro to conditionally emit code specific to certain CPU word sizes, e.g. defining
/// types at compile-time based on the word size.
///
/// The macro doesn't create a new block and supports arbitrary statements in toplevel code, just
/// like the `cfg-if` crate (whose guts it recycles).
///
/// ## Basic usage
///
/// ```
/// cpubits::cpubits! {
/// 16 => { pub type Word = u16; }
/// 32 => { pub type Word = u32; }
/// 64 => { pub type Word = u64; }
/// }
/// ```
///
/// ## Grouping multiple bit sizes
///
/// If you would like to group together 16-bit and 32-bit platforms, you can do so as follows:
///
/// ```
/// cpubits::cpubits! {
/// 16 | 32 => { pub type Word = u32; }
/// 64 => { pub type Word = u64; }
/// }
/// ```
///
/// ## Handling single-size cases
///
/// If you only want a block to run for a specific size, e.g. to know when it's possible to write
/// `impl From<u64> for MyWordNewtype`, you can do the following:
///
/// ```
/// # type Word = u64;
/// pub struct MyWordNewtype(Word);
///
/// cpubits::cpubits! {
/// 64 => {
/// impl From<u64> for MyWordNewtype {
/// #[inline]
/// fn from(n: u64) -> MyWordNewtype {
/// MyWordNewtype(n)
/// }
/// }
/// }
/// }
/// ```
///
/// # Selection rules
///
/// The macro augments `target_pointer_width`-based selection with specific overrides which promote
/// certain targets from 32-bit to 64-bit ones.
///
/// This 64-bit promotion occurs if `any` of the following `cfg`s are true:
/// - `target_family = "wasm"`
#[macro_export]
macro_rules! cpubits {
// Only run the given block if we have selected a 16-bit word size, i.e. the code will be
// ignored on 32-bit and 64-bit platforms.
( 16 => { $( $tokens:tt )* } ) => {
$crate::cpubits! {
16 => { $( $tokens )* },
32 | 64 => { }
}
};
// Only run the given block if we have selected a 32-bit word size, i.e. the code will be
// ignored on 32-bit and 64-bit platforms.
( 32 => { $( $tokens:tt )* } ) => {
$crate::cpubits! {
16 => { }
32 => { $( $tokens )* }
64 => { }
}
};
// Only run the given block if we have selected a 64-bit word size, i.e. the code will be
// ignored on 16-bit and 32-bit platforms.
( 64 => { $( $tokens:tt )* } ) => {
$crate::cpubits! {
16 | 32 => { }
64 => { $( $tokens )* }
}
};
// Only run the block on 16-bit and 32-bit targets.
( 16 | 32 => { $( $tokens:tt )* } ) => {
$crate::cpubits! {
16 => { $( $tokens )* }
32 => { $( $tokens )* }
64 => { }
}
};
// Only run the block on 32-bit and 64-bit targets.
( 32 | 64 => { $( $tokens:tt )* } ) => {
$crate::cpubits! {
16 => { }
32 => { $( $tokens )* }
64 => { $( $tokens )* }
}
};
// Select between 16-bit and 32-bit options, where 64-bit will use the 32-bit option
(
16 => { $( $tokens16:tt )* }
32 | 64 => { $( $tokens32:tt )* }
) => {
$crate::cpubits! {
16 => { $( $tokens16 )* }
32 => { $( $tokens32 )* }
64 => { $( $tokens32 )* }
}
};
// Select between 32-bit and 64-bit options, where 16-bit will use the 32-bit option
(
16 | 32 => { $( $tokens32:tt )* }
64 => { $( $tokens64:tt )* }
) => {
$crate::cpubits! {
16 => { $( $tokens32 )* }
32 => { $( $tokens32 )* }
64 => { $( $tokens64 )* }
}
};
// The general API which runs a different block for each possible word size
(
16 => { $( $tokens16:tt )* }
32 => { $( $tokens32:tt )* }
64 => { $( $tokens64:tt )* }
) => {
$crate::cpubits! {
#[cfg(enable_64bit(
// `cfg` selector for 64-bit targets (implicitly `any`)
target_arch = "wasm32"
))]
16 => { $( $tokens32 )* }
32 => { $( $tokens32 )* }
64 => { $( $tokens64 )* }
}
};
// Same API as immediately above, but with a pseudo-attribute we use to pass the `cfg` overrides
// for `target_pointer_width` that promote a 32-bit target into a 64-bit one.
(
#[cfg(enable_64bit( $($enable_64bit:tt)+ ))]
16 => { $( $tokens16:tt )* }
32 => { $( $tokens32:tt )* }
64 => { $( $tokens64:tt )* }
) => {
$crate::cfg_if! {
@__items () ;
(
( target_pointer_width = "16" )
( $( $tokens16 )* )
),
(
(all(
target_pointer_width = "32",
not(any($( $enable_64bit )+))
))
( $( $tokens32 )* )
),
(
(any(
target_pointer_width = "64",
any($( $enable_64bit )+)
))
( $( $tokens64 )* )
),
(
()
( compile_error!("unsupported target pointer width") )
),
}
};
}
/// Vendored partial copy of the `cfg_if::cfg_if` macro.
/// Copyright (c) 2014 Alex Crichton. Dual-licensed Apache 2.0 + MIT.
#[doc(hidden)]
#[macro_export]
macro_rules! cfg_if {
// Internal and recursive macro to emit all the items
//
// Collects all the previous cfgs in a list at the beginning, so they can be
// negated. After the semicolon are all the remaining items.
(@__items ( $( ($($_:tt)*) , )* ) ; ) => {};
(
@__items ( $( ($($no:tt)+) , )* ) ;
(( $( $($yes:tt)+ )? ) ( $( $tokens:tt )* )),
$( $rest:tt , )*
) => {
// Emit all items within one block, applying an appropriate #[cfg]. The
// #[cfg] will require all `$yes` matchers specified and must also negate
// all previous matchers.
#[cfg(all(
$( $($yes)+ , )?
not(any( $( $($no)+ ),* ))
))]
// Subtle: You might think we could put `$( $tokens )*` here. But if
// that contains multiple items then the `#[cfg(all(..))]` above would
// only apply to the first one. By wrapping `$( $tokens )*` in this
// macro call, we temporarily group the items into a single thing (the
// macro call) that will be included/excluded by the `#[cfg(all(..))]`
// as appropriate. If the `#[cfg(all(..))]` succeeds, the macro call
// will be included, and then evaluated, producing `$( $tokens )*`. See
// also the "issue #90" test below.
$crate::cfg_if! { @__temp_group $( $tokens )* }
// Recurse to emit all other items in `$rest`, and when we do so add all
// our `$yes` matchers to the list of `$no` matchers as future emissions
// will have to negate everything we just matched as well.
$crate::cfg_if! {
@__items ( $( ($($no)+) , )* $( ($($yes)+) , )? ) ;
$( $rest , )*
}
};
// See the "Subtle" comment above.
(@__temp_group $( $tokens:tt )* ) => {
$( $tokens )*
};
}
#[cfg(test)]
mod tests {
/// Return an integer that maps to the number of bits `cpubits` detected.
fn detected_bits() -> u32 {
cpubits! {
16 => { 16 }
32 => { 32 }
64 => { 64 }
}
}
/// Return an integer that maps to `target_pointer_width`.
#[allow(dead_code)]
fn detect_pointer_width() -> u32 {
if cfg!(target_pointer_width = "16") {
16
} else if cfg!(target_pointer_width = "32") {
32
} else if cfg!(target_pointer_width = "64") {
64
} else {
unreachable!("rustc only support 16, 32, and 64-bit pointer widths")
}
}
/// Return the expected number of bits for the target.
fn expected_bits() -> u32 {
// Duplicated 64-bit override predicates need to go here
if cfg!(target_arch = "wasm32") {
64
} else {
detect_pointer_width()
}
}
#[test]
fn cpubits_works() {
assert_eq!(detected_bits(), expected_bits());
}
/// Explicit test for WASM so we can see the predicate is working
#[cfg(target_arch = "wasm32")]
#[test]
fn cpubits_on_wasm_is_64bit() {
assert_eq!(detected_bits(), 64);
}
#[test]
fn cpubits_16_or_32_vs_64() {
fn bits32or64() -> u32 {
cpubits! {
16 | 32 => { 32 }
64 => { 64 }
}
}
match expected_bits() {
16 | 32 => assert_eq!(32, bits32or64()),
64 => assert_eq!(64, bits32or64()),
bits => unreachable!("#{bits}-bits should be one of: 16, 32, 64"),
}
}
}