1use core::{
2 fmt::{Debug, Display},
3 ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Shl, Shr, Sub},
4};
5
6pub trait Integer:
8 Add
9 + Sub
10 + Mul
11 + Div
12 + Shl
13 + Shr
14 + BitAnd
15 + BitOr
16 + BitXor
17 + PartialEq
18 + Eq
19 + PartialOrd
20 + Ord
21 + Clone
22 + Copy
23 + Default
24 + Debug
25 + Display
26{
27 const BITS: usize;
29
30 const MASK: Self;
32
33 fn from_usize(value: usize) -> Self;
35
36 fn to_usize(self) -> usize;
38}
39
40macro_rules! impl_integer {
41 ($type:ty) => {
42 impl Integer for $type {
43 const BITS: usize = Self::BITS as _;
44 const MASK: Self = Self::MAX;
45
46 fn from_usize(value: usize) -> Self {
47 value as _
48 }
49
50 fn to_usize(self) -> usize {
51 self as _
52 }
53 }
54 };
55}
56
57impl_integer!(i8);
58impl_integer!(i16);
59impl_integer!(i32);
60impl_integer!(i64);
61impl_integer!(i128);
62impl_integer!(isize);
63impl_integer!(u8);
64impl_integer!(u16);
65impl_integer!(u32);
66impl_integer!(u64);
67impl_integer!(u128);
68impl_integer!(usize);