intid_core/uint/
sealed.rs1pub trait PrivateUnsignedInt: Sized {
2 const BITS: u32;
3 const ZERO: Self;
4 const ONE: Self;
5 const MAX: Self;
6 fn checked_cast<U: super::UnsignedPrimInt>(self) -> Option<U>;
7 const TYPE_NAME: &'static str;
9 fn checked_add(self, other: Self) -> Option<Self>;
10 fn checked_sub(self, other: Self) -> Option<Self>;
11 fn trailing_zeros(self) -> u32;
12 fn leading_zeros(self) -> u32;
13 fn count_ones(self) -> u32;
14 fn from_usize_checked(val: usize) -> Option<Self>;
15 fn from_usize_wrapping(val: usize) -> Self;
16 #[allow(clippy::wrong_self_convention)]
17 fn to_usize_wrapping(this: Self) -> usize;
18 #[allow(clippy::wrong_self_convention)]
19 fn to_usize_checked(this: Self) -> Option<usize>;
20}
21macro_rules! impl_primint {
22 ($($target:ident),*) => ($(
23 impl super::UnsignedPrimInt for $target {}
24 impl super::ConvertPrimInts for $target {}
25 #[deny(unconditional_recursion)]
26 impl PrivateUnsignedInt for $target {
27 const TYPE_NAME: &'static str = stringify!($target);
28 const ZERO: Self = {
29 assert!($target::MIN == 0, "signed integer");
30 0
31 };
32 const BITS: u32 = $target::BITS;
33 const ONE: Self = 1;
34 const MAX: Self = $target::MAX;
35 #[inline]
36 fn checked_cast<U: super::UnsignedPrimInt>(self) -> Option<U> {
37 U::try_from(self).ok()
38 }
39 #[inline]
40 fn checked_add(self, other: Self) -> Option<Self> {
41 <$target>::checked_add(self, other)
42 }
43 #[inline]
44 fn checked_sub(self, other: Self) -> Option<Self> {
45 <$target>::checked_sub(self, other)
46 }
47 #[inline]
48 fn count_ones(self) -> u32 {
49 <$target>::count_ones(self)
50 }
51 #[inline]
52 fn leading_zeros(self) -> u32 {
53 <$target>::leading_zeros(self)
54 }
55 #[inline]
56 fn trailing_zeros(self) -> u32 {
57 <$target>::trailing_zeros(self)
58 }
59 #[inline]
60 fn from_usize_checked(val: usize) -> Option<Self> {
61 <$target>::try_from(val).ok()
62 }
63 #[inline]
64 #[allow(clippy::cast_possible_truncation)] fn from_usize_wrapping(val: usize) -> Self {
66 val as $target
67 }
68 #[inline]
69 #[allow(clippy::cast_possible_truncation)] fn to_usize_wrapping(this: Self) -> usize {
71 this as usize
72 }
73 #[inline]
74 fn to_usize_checked(this: Self) -> Option<usize> {
75 usize::try_from(this).ok()
76 }
77 }
78 )*);
79}
80impl_primint!(u8, u16, u32, u64, u128, usize);