From e8422f28fff93752da24b5a9e26a5214225e78a0 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 18 Jan 2026 13:16:22 -0700 Subject: [PATCH] cpubits: shorthand for `|` and more combinations - Allows omitting the `16` in `16 | 32` - Adds `16` vs `32 | 64` as an option, where `64` can be omitted --- Cargo.lock | 2 +- cpubits/Cargo.toml | 2 +- cpubits/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d50e91f5..c8f557ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,7 @@ version = "0.1.0" [[package]] name = "cpubits" -version = "0.1.0-pre.0" +version = "0.1.0-pre.1" [[package]] name = "cpufeatures" diff --git a/cpubits/Cargo.toml b/cpubits/Cargo.toml index 2149a77b..573748f7 100644 --- a/cpubits/Cargo.toml +++ b/cpubits/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cpubits" -version = "0.1.0-pre.0" +version = "0.1.0-pre.1" authors = ["RustCrypto Developers"] license = "MIT OR Apache-2.0" documentation = "https://docs.rs/cpubits" diff --git a/cpubits/src/lib.rs b/cpubits/src/lib.rs index 7be2ca36..c94093ff 100644 --- a/cpubits/src/lib.rs +++ b/cpubits/src/lib.rs @@ -25,9 +25,53 @@ //! 64 => { pub type Word = u64; } //! } //! ``` +//! +//! You can also use the shortened form of the above syntax which implicitly promotes 16-bit +//! platforms to 32-bit ones: +//! +//! ``` +//! cpubits::cpubits! { +//! 32 => { pub type Word = u32; } +//! 64 => { pub type Word = u64; } +//! } +//! ``` +//! +//! 32-bit and 64-bit platforms can also be combined to differentiate them from 16-bit ones, e.g. +//! `16 => { ... }, 32 | 64 => { ... }`, and `32 | 64` can be shortened to just `32` in such a case. #[macro_export] macro_rules! cpubits { + ( + 16 => { $( $tokens16:tt )* } + 32 => { $( $tokens32:tt )* } + ) => { + $crate::cpubits! { + 16 => { $( $tokens16 )* } + 32 | 64 => { $( $tokens32 )* } + } + }; + + ( + 32 => { $( $tokens32:tt )* } + 64 => { $( $tokens64:tt )* } + ) => { + $crate::cpubits! { + 16 | 32 => { $( $tokens32 )* } + 64 => { $( $tokens64 )* } + } + }; + + ( + 16 => { $( $tokens16:tt )* } + 32 | 64 => { $( $tokens32:tt )* } + ) => { + $crate::cpubits! { + 16 => { $( $tokens16 )* } + 32 => { $( $tokens32 )* } + 64 => { $( $tokens32 )* } + } + }; + ( 16 | 32 => { $( $tokens32:tt )* } 64 => { $( $tokens64:tt )* }