Skip to content

Commit 2fea419

Browse files
committed
cpubits: split toplevel and macro docs
Makes the toplevel rustdoc give an overview, putting the detailed documentation on the macro itself. That way, if crates re-export the macro, people who click the re-export will go directly to the detailed documentation.
1 parent f6ab29c commit 2fea419

File tree

1 file changed

+72
-46
lines changed

1 file changed

+72
-46
lines changed

cpubits/src/lib.rs

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
//! expected values for `target_pointer_width` are: `16`, `32`, and `64`
1818
//! ```
1919
//!
20-
//! # Usage
20+
//! # Example
2121
//!
22-
//! Use this macro to run code blocks specific to certain CPU word sizes, e.g. to conditionally
23-
//! define types at compile-time based on the word size.
24-
//!
25-
//! The macro doesn't create a new block and supports arbitrary statements in toplevel code, just
26-
//! like the `cfg-if` crate (whose guts it recycles).
27-
//!
28-
//! ## Basic usage
22+
//! See the [`cpubits`] macro itself for more detailed usage examples including other syntax
23+
//! variations.
2924
//!
3025
//! ```
3126
//! cpubits::cpubits! {
@@ -34,39 +29,71 @@
3429
//! 64 => { pub type Word = u64; }
3530
//! }
3631
//! ```
37-
//!
38-
//! ## Grouping multiple bit sizes
39-
//!
40-
//! If you would like to group together 16-bit and 32-bit platforms, you can do so as follows:
41-
//!
42-
//! ```
43-
//! cpubits::cpubits! {
44-
//! 16 | 32 => { pub type Word = u32; }
45-
//! 64 => { pub type Word = u64; }
46-
//! }
47-
//! ```
48-
//!
49-
//! ## Handling single-size cases
50-
//!
51-
//! If you only want a block to run for a specific size, e.g. to know when it's possible to write
52-
//! `impl From<u64> for MyWordNewtype`, you can do the following:
53-
//!
54-
//! ```
55-
//! # type Word = u64;
56-
//! pub struct MyWordNewtype(Word);
57-
//!
58-
//! cpubits::cpubits! {
59-
//! 64 => {
60-
//! impl From<u64> for MyWordNewtype {
61-
//! #[inline]
62-
//! fn from(n: u64) -> MyWordNewtype {
63-
//! MyWordNewtype(n)
64-
//! }
65-
//! }
66-
//! }
67-
//! }
68-
//! ```
6932
33+
// End of toplevel rustdoc, beginning of macro documentation. We put the detailed docs on the macro
34+
// itself so we can re-export it, and people can easily get to these docs from the re-exported
35+
// version.
36+
37+
/// # Usage
38+
///
39+
/// The macro works sort of like a `match` expression that takes an implicit number of CPU bits,
40+
/// which is one of `16`, `32`, or `64`.
41+
///
42+
/// Use this macro to conditionally emit code specific to certain CPU word sizes, e.g. defining
43+
/// types at compile-time based on the word size.
44+
///
45+
/// The macro doesn't create a new block and supports arbitrary statements in toplevel code, just
46+
/// like the `cfg-if` crate (whose guts it recycles).
47+
///
48+
/// ## Basic usage
49+
///
50+
/// ```
51+
/// cpubits::cpubits! {
52+
/// 16 => { pub type Word = u16; }
53+
/// 32 => { pub type Word = u32; }
54+
/// 64 => { pub type Word = u64; }
55+
/// }
56+
/// ```
57+
///
58+
/// ## Grouping multiple bit sizes
59+
///
60+
/// If you would like to group together 16-bit and 32-bit platforms, you can do so as follows:
61+
///
62+
/// ```
63+
/// cpubits::cpubits! {
64+
/// 16 | 32 => { pub type Word = u32; }
65+
/// 64 => { pub type Word = u64; }
66+
/// }
67+
/// ```
68+
///
69+
/// ## Handling single-size cases
70+
///
71+
/// If you only want a block to run for a specific size, e.g. to know when it's possible to write
72+
/// `impl From<u64> for MyWordNewtype`, you can do the following:
73+
///
74+
/// ```
75+
/// # type Word = u64;
76+
/// pub struct MyWordNewtype(Word);
77+
///
78+
/// cpubits::cpubits! {
79+
/// 64 => {
80+
/// impl From<u64> for MyWordNewtype {
81+
/// #[inline]
82+
/// fn from(n: u64) -> MyWordNewtype {
83+
/// MyWordNewtype(n)
84+
/// }
85+
/// }
86+
/// }
87+
/// }
88+
/// ```
89+
///
90+
/// # Selection rules
91+
///
92+
/// The macro augments `target_pointer_width`-based selection with specific overrides which promote
93+
/// certain targets from 32-bit to 64-bit ones.
94+
///
95+
/// This 64-bit promotion occurs if `any` of the following `cfg`s are true:
96+
/// - `target_family = "wasm"`
7097
#[macro_export]
7198
macro_rules! cpubits {
7299
// Only run the given block if we have selected a 16-bit word size, i.e. the code will be
@@ -146,10 +173,10 @@ macro_rules! cpubits {
146173
64 => { $( $tokens64:tt )* }
147174
) => {
148175
$crate::cpubits! {
149-
#[enable_64bit(
176+
#[cfg(enable_64bit(
150177
// `cfg` selector for 64-bit targets (implicitly `any`)
151178
target_family = "wasm",
152-
)]
179+
))]
153180
16 => { $( $tokens32 )* }
154181
32 => { $( $tokens32 )* }
155182
64 => { $( $tokens64 )* }
@@ -159,7 +186,7 @@ macro_rules! cpubits {
159186
// Same API as immediately above, but with a pseudo-attribute we use to pass the `cfg` overrides
160187
// for `target_pointer_width` that promote a 32-bit target into a 64-bit one.
161188
(
162-
#[enable_64bit( $($enable_64bit:tt)+ )]
189+
#[cfg(enable_64bit( $($enable_64bit:tt)+ ))]
163190
16 => { $( $tokens16:tt )* }
164191
32 => { $( $tokens32:tt )* }
165192
64 => { $( $tokens64:tt )* }
@@ -192,9 +219,8 @@ macro_rules! cpubits {
192219
};
193220
}
194221

195-
// Vendored partial copy of the `cfg_if::cfg_if` macro.
196-
// Copyright (c) 2014 Alex Crichton. Dual-licensed Apache 2.0 + MIT.
197-
// TODO(tarcieri): refactor and golf this down to just the parts we use
222+
/// Vendored partial copy of the `cfg_if::cfg_if` macro.
223+
/// Copyright (c) 2014 Alex Crichton. Dual-licensed Apache 2.0 + MIT.
198224
#[doc(hidden)]
199225
#[macro_export]
200226
macro_rules! cfg_if {

0 commit comments

Comments
 (0)