Skip to content

Commit ebe62f8

Browse files
committed
Split structure definition and convenience methods into two macros
1 parent 645abf3 commit ebe62f8

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/lib.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ macro_rules! impl_nonmax_fmt {
118118
};
119119
}
120120

121+
/// Define the basic nonmax wrapper using NonZero inner
121122
macro_rules! nonmax {
122-
( common, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
123+
( $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
123124
/// An integer that is known not to equal its maximum value.
124125
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
125126
#[repr(transparent)]
@@ -153,7 +154,14 @@ macro_rules! nonmax {
153154
pub const fn get(&self) -> $primitive {
154155
self.0.get() ^ $primitive::MAX
155156
}
157+
}
158+
};
159+
}
156160

161+
/// Provide convenience methods over an basic nonmax wrapper with `new_unchecked`` and `get`.
162+
macro_rules! nonmax_impls {
163+
( common, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
164+
impl $nonmax {
157165
/// Gets non-max with the value zero (0)
158166
pub const ZERO: $nonmax = unsafe { Self::new_unchecked(0) };
159167

@@ -343,12 +351,12 @@ macro_rules! nonmax {
343351
};
344352

345353
( signed, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
346-
nonmax!(common, $nonmax, $non_zero, $primitive);
354+
nonmax_impls!(common, $nonmax, $non_zero, $primitive);
347355
// Nothing unique to signed versions (yet)
348356
};
349357

350358
( unsigned, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
351-
nonmax!(common, $nonmax, $non_zero, $primitive);
359+
nonmax_impls!(common, $nonmax, $non_zero, $primitive);
352360

353361
impl core::ops::BitAnd<$nonmax> for $primitive {
354362
type Output = $nonmax;
@@ -381,21 +389,31 @@ macro_rules! nonmax {
381389
}
382390
}
383391
};
392+
393+
( def, signed, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
394+
nonmax!($nonmax, $non_zero, $primitive);
395+
nonmax_impls!(signed, $nonmax, $non_zero, $primitive);
396+
};
397+
398+
( def, unsigned, $nonmax: ident, $non_zero: ident, $primitive: ident ) => {
399+
nonmax!($nonmax, $non_zero, $primitive);
400+
nonmax_impls!(unsigned, $nonmax, $non_zero, $primitive);
401+
};
384402
}
385403

386-
nonmax!(signed, NonMaxI8, NonZeroI8, i8);
387-
nonmax!(signed, NonMaxI16, NonZeroI16, i16);
388-
nonmax!(signed, NonMaxI32, NonZeroI32, i32);
389-
nonmax!(signed, NonMaxI64, NonZeroI64, i64);
390-
nonmax!(signed, NonMaxI128, NonZeroI128, i128);
391-
nonmax!(signed, NonMaxIsize, NonZeroIsize, isize);
392-
393-
nonmax!(unsigned, NonMaxU8, NonZeroU8, u8);
394-
nonmax!(unsigned, NonMaxU16, NonZeroU16, u16);
395-
nonmax!(unsigned, NonMaxU32, NonZeroU32, u32);
396-
nonmax!(unsigned, NonMaxU64, NonZeroU64, u64);
397-
nonmax!(unsigned, NonMaxU128, NonZeroU128, u128);
398-
nonmax!(unsigned, NonMaxUsize, NonZeroUsize, usize);
404+
nonmax_impls!(def, signed, NonMaxI8, NonZeroI8, i8);
405+
nonmax_impls!(def, signed, NonMaxI16, NonZeroI16, i16);
406+
nonmax_impls!(def, signed, NonMaxI32, NonZeroI32, i32);
407+
nonmax_impls!(def, signed, NonMaxI64, NonZeroI64, i64);
408+
nonmax_impls!(def, signed, NonMaxI128, NonZeroI128, i128);
409+
nonmax_impls!(def, signed, NonMaxIsize, NonZeroIsize, isize);
410+
411+
nonmax_impls!(def, unsigned, NonMaxU8, NonZeroU8, u8);
412+
nonmax_impls!(def, unsigned, NonMaxU16, NonZeroU16, u16);
413+
nonmax_impls!(def, unsigned, NonMaxU32, NonZeroU32, u32);
414+
nonmax_impls!(def, unsigned, NonMaxU64, NonZeroU64, u64);
415+
nonmax_impls!(def, unsigned, NonMaxU128, NonZeroU128, u128);
416+
nonmax_impls!(def, unsigned, NonMaxUsize, NonZeroUsize, usize);
399417

400418
// https://doc.rust-lang.org/1.47.0/src/core/convert/num.rs.html#383-407
401419
macro_rules! impl_nonmax_from {

0 commit comments

Comments
 (0)