1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::ops::{Add, Sub, Mul, Div};
use std::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use std::cmp::{PartialEq, PartialOrd};
use std::mem::size_of;
pub trait Int
: Sized
+ Copy
+ Add<Output=Self>
+ Sub<Output=Self>
+ Mul<Output=Self>
+ Div<Output=Self>
+ Not<Output=Self>
+ BitAnd<Output=Self>
+ BitOr<Output=Self>
+ BitXor<Output=Self>
+ Shr<Self, Output=Self>
+ Shl<Self, Output=Self>
+ PartialEq + PartialOrd
{
fn one() -> Self;
fn zero() -> Self;
fn byte_size() -> Self;
fn bit_size() -> Self;
fn count_ones(self) -> Self;
fn count_zeros(self) -> Self;
fn leading_zeros(self) -> Self;
fn trailing_zeros(self) -> Self;
fn wrapping_neg(self) -> Self;
fn wrapping_add(self, Self) -> Self;
fn wrapping_sub(self, Self) -> Self;
fn to_u32(self) -> u32;
fn to_u64(self) -> u64;
fn from_u16(u16) -> Self;
fn from_u32(u32) -> Self;
fn from_u64(u64) -> Self;
fn rotate_left(self, u32) -> Self;
fn rotate_right(self, u32) -> Self;
}
macro_rules! int_impl {
($T:ty) => (
impl Int for $T {
fn one() -> Self { 1 as Self }
fn zero() -> Self { 0 as Self }
fn byte_size() -> Self {
size_of::<Self>() as Self
}
fn bit_size() -> Self {
(Self::byte_size() * 8) as Self
}
fn count_ones(self) -> $T {
self.count_ones() as $T
}
fn count_zeros(self) -> $T {
self.count_zeros() as $T
}
fn leading_zeros(self) -> $T {
self.leading_zeros() as $T
}
fn trailing_zeros(self) -> $T {
self.trailing_zeros() as $T
}
fn wrapping_neg(self) -> $T {
self.wrapping_neg() as $T
}
fn wrapping_add(self, o: Self) -> $T {
self.wrapping_add(o) as $T
}
fn wrapping_sub(self, o: Self) -> $T {
self.wrapping_sub(o) as $T
}
fn to_u32(self) -> u32 { self as u32 }
fn to_u64(self) -> u64 { self as u64 }
fn from_u16(x: u16) -> Self { x as Self }
fn from_u32(x: u32) -> Self { x as Self }
fn from_u64(x: u64) -> Self { x as Self }
fn rotate_left(self, n: u32) -> Self { (self as $T).rotate_left(n) }
fn rotate_right(self, n: u32) -> Self { (self as $T).rotate_right(n) }
}
)
}
int_impl!(u8);
int_impl!(u16);
int_impl!(u32);
int_impl!(u64);
int_impl!(usize);
int_impl!(i8);
int_impl!(i16);
int_impl!(i32);
int_impl!(i64);
int_impl!(isize);
pub trait IntF16T64: Int {}
macro_rules! signed_or_unsigned_from_16_to_64_and_size_impl {
($T:ty) => (impl IntF16T64 for $T {})
}
signed_or_unsigned_from_16_to_64_and_size_impl!(u16);
signed_or_unsigned_from_16_to_64_and_size_impl!(u32);
signed_or_unsigned_from_16_to_64_and_size_impl!(u64);
signed_or_unsigned_from_16_to_64_and_size_impl!(usize);
signed_or_unsigned_from_16_to_64_and_size_impl!(i16);
signed_or_unsigned_from_16_to_64_and_size_impl!(i32);
signed_or_unsigned_from_16_to_64_and_size_impl!(i64);
signed_or_unsigned_from_16_to_64_and_size_impl!(isize);
pub trait IntF32T64: Int {}
macro_rules! signed_or_unsigned_from_32_to_64_and_size_impl {
($T:ty) => (impl IntF32T64 for $T {})
}
signed_or_unsigned_from_32_to_64_and_size_impl!(u32);
signed_or_unsigned_from_32_to_64_and_size_impl!(u64);
signed_or_unsigned_from_32_to_64_and_size_impl!(usize);
signed_or_unsigned_from_32_to_64_and_size_impl!(i32);
signed_or_unsigned_from_32_to_64_and_size_impl!(i64);
signed_or_unsigned_from_32_to_64_and_size_impl!(isize);