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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/// Creates an `Int` from a numeric literal.
#[macro_export]
macro_rules! int {
    ($n:literal) => {
        <$crate::Int as ::core::convert::From<i32>>::from($n)
    };
}

/// Creates a `UInt` from a numeric literal.
#[macro_export]
macro_rules! uint {
    ($n:literal) => {
        <$crate::UInt as ::core::convert::From<u32>>::from($n)
    };
}

macro_rules! fmt_impls {
    ($type:ident) => {
        impl ::core::fmt::Display for $type {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                write!(f, "{}", self.0)
            }
        }

        impl ::core::fmt::Debug for $type {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                write!(f, "{:?}", self.0)
            }
        }
    };
}

macro_rules! convert_impls {
    (
        $type:ident,
        $t8:ident,
        $t16:ident,
        $t32:ident,
        $t64:ident,
        $t128:ident,
        $tsize:ident,
        $ot8:ident,
        $ot16:ident,
        $ot32:ident,
        $otsize:ident
    ) => {
        impl ::core::convert::From<$t8> for $type {
            fn from(val: $t8) -> Self {
                Self($t64::from(val))
            }
        }

        impl ::core::convert::From<$t16> for $type {
            fn from(val: $t16) -> Self {
                Self($t64::from(val))
            }
        }

        impl ::core::convert::From<$t32> for $type {
            fn from(val: $t32) -> Self {
                Self($t64::from(val))
            }
        }

        impl ::core::convert::TryFrom<$t64> for $type {
            type Error = crate::error::TryFromIntError;

            fn try_from(val: $t64) -> Result<Self, crate::error::TryFromIntError> {
                Self::new(val).ok_or_else(crate::error::TryFromIntError::new)
            }
        }

        impl ::core::convert::TryFrom<$t128> for $type {
            type Error = crate::error::TryFromIntError;

            fn try_from(val: $t128) -> Result<Self, crate::error::TryFromIntError> {
                $t64::try_from(val)
                    .map_err(|_| crate::error::TryFromIntError::new())
                    .and_then($type::try_from)
            }
        }

        impl ::core::convert::TryFrom<$tsize> for $type {
            type Error = crate::error::TryFromIntError;

            fn try_from(val: $tsize) -> Result<Self, crate::error::TryFromIntError> {
                $t64::try_from(val)
                    .map_err(|_| crate::error::TryFromIntError::new())
                    .and_then($type::try_from)
            }
        }

        impl ::core::convert::TryFrom<$otsize> for $type {
            type Error = crate::error::TryFromIntError;

            fn try_from(val: $otsize) -> Result<Self, crate::error::TryFromIntError> {
                $t64::try_from(val)
                    .map_err(|_| crate::error::TryFromIntError::new())
                    .and_then($type::try_from)
            }
        }

        impl ::core::convert::TryFrom<$type> for $t8 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $ot8 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $t16 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $ot16 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $t32 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $ot32 {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::From<$type> for $t64 {
            fn from(val: $type) -> Self {
                val.0
            }
        }

        impl ::core::convert::From<$type> for $t128 {
            fn from(val: $type) -> Self {
                $t128::from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $tsize {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::TryFrom<$type> for $otsize {
            type Error = ::core::num::TryFromIntError;

            fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
                Self::try_from(val.0)
            }
        }

        impl ::core::convert::From<$type> for f64 {
            fn from(val: $type) -> Self {
                val.0 as f64
            }
        }
    };
}