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