Skip to content

Commit ce1a465

Browse files
authored
Make new UInt definition less verbose (#51)
This PR adds a macro that simplifies the definition of new integer values, and uses it for the integers we need to define here. It allows a new `UInt` type to be defined simply by listing its bits. Unfortunately, because Rust macros only permit recursive matching in one direction, we have to use the little-endian bit order, even though big-endian would be easier to read. Not a huge change, but it at least allows us to remove the `#[rustfmt::skip]` line.
1 parent 3215a05 commit ce1a465

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/sizes.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,34 @@ use typenum::{consts::*, Unsigned};
66
/// Additional typenum size aliases beyond what are normally provided.
77
///
88
/// These are defined using their component bits rather than `Add` to avoid conflicting impls.
9-
#[rustfmt::skip]
109
pub mod extra_sizes {
1110
use typenum::{UInt, UTerm, B0, B1};
1211

13-
pub type U1088 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
14-
pub type U1152 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
15-
pub type U1184 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
16-
pub type U1408 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
17-
pub type U1472 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
18-
pub type U1536 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
19-
pub type U1568 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
20-
pub type U1600 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
21-
pub type U1632 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
22-
pub type U2336 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
23-
pub type U2368 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
24-
pub type U2400 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
25-
pub type U3072 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>, B0>;
26-
pub type U3104 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>;
27-
pub type U3136 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
28-
pub type U3168 = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>, B1>, B1>, B0>, B0>, B0>, B0>, B0>;
12+
// This macro constructs a UInt type from a sequence of bits. The bits are interpreted as the
13+
// little-endian representation of the integer in question. For example, uint!(1 1 0 1 0 0 1) is
14+
// U75 (not U105).
15+
macro_rules! uint {
16+
() => { UTerm };
17+
(0 $($bs:tt)*) => { UInt< uint!($($bs)*), B0 > };
18+
(1 $($bs:tt)*) => { UInt< uint!($($bs)*), B1 > };
19+
}
20+
21+
pub type U1088 = uint!(0 0 0 0 0 0 1 0 0 0 1 );
22+
pub type U1152 = uint!(0 0 0 0 0 0 0 1 0 0 1 );
23+
pub type U1184 = uint!(0 0 0 0 0 1 0 1 0 0 1 );
24+
pub type U1408 = uint!(0 0 0 0 0 0 0 1 1 0 1 );
25+
pub type U1472 = uint!(0 0 0 0 0 0 1 1 1 0 1 );
26+
pub type U1536 = uint!(0 0 0 0 0 0 0 0 0 1 1 );
27+
pub type U1568 = uint!(0 0 0 0 0 1 0 0 0 1 1 );
28+
pub type U1600 = uint!(0 0 0 0 0 0 1 0 0 1 1 );
29+
pub type U1632 = uint!(0 0 0 0 0 1 1 0 0 1 1 );
30+
pub type U2336 = uint!(0 0 0 0 0 1 0 0 1 0 0 1);
31+
pub type U2368 = uint!(0 0 0 0 0 0 1 0 1 0 0 1);
32+
pub type U2400 = uint!(0 0 0 0 0 1 1 0 1 0 0 1);
33+
pub type U3072 = uint!(0 0 0 0 0 0 0 0 0 0 1 1);
34+
pub type U3104 = uint!(0 0 0 0 0 1 0 0 0 0 1 1);
35+
pub type U3136 = uint!(0 0 0 0 0 0 1 0 0 0 1 1);
36+
pub type U3168 = uint!(0 0 0 0 0 1 1 0 0 0 1 1);
2937
}
3038

3139
pub use extra_sizes::*;

0 commit comments

Comments
 (0)