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
#[macro_export]
macro_rules! bounded_integer_impl {
($ty:ty, $repr:ty, $min:path, $max:path) => {
impl $crate::BoundedInteger for $ty {
type Repr = $repr;
#[allow(unused_comparisons)]
fn from_repr(repr: $repr) -> Option<Self> {
use std::mem;
if repr >= $min as $repr && repr <= $max as $repr {
Some(unsafe { mem::transmute(repr) })
} else {
None
}
}
fn to_repr(self) -> $repr { self as $repr }
fn min_value() -> Self { $min }
fn max_value() -> Self { $max }
}
}
}
#[macro_export]
macro_rules! bounded_integer_into_repr_impl {
($ty:ty) => {
impl Into<<$ty as $crate::BoundedInteger>::Repr> for $ty {
fn into(self) -> <$ty as $crate::BoundedInteger>::Repr {
use $crate::BoundedInteger;
self.to_repr()
}
}
}
}
#[macro_export]
macro_rules! bounded_integer_impls {
($ty:ty, $repr:ty, $min:path, $max:path) => {
bounded_integer_impl!($ty, $repr, $min, $max);
bounded_integer_into_repr_impl!($ty);
bounded_integer_add_self_impls!($ty);
bounded_integer_add_repr_impls!($ty);
bounded_integer_sub_self_impls!($ty);
bounded_integer_sub_repr_impls!($ty);
bounded_integer_mul_self_impls!($ty);
bounded_integer_mul_repr_impls!($ty);
bounded_integer_div_self_impls!($ty);
bounded_integer_div_repr_impls!($ty);
bounded_integer_rem_self_impls!($ty);
bounded_integer_rem_repr_impls!($ty);
bounded_integer_neg_impls!($ty);
}
}
#[macro_use]
mod ops;