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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Unsigned integer.

use core::cmp::Ordering;
use core::ops::*;

use hazardflow_macro::magic;

use super::Array;
use crate::prelude::*;

/// An unsigned integer with bitwidth `N`.
///
/// The lower bits of the integer are represented by the lower index of the array, and vice versa. In other words, the
/// least significant bit of the integer is the 0th element of the array, and the most significant bit is the
/// (`N` - 1)-th element.
pub type U<const N: usize> = Array<bool, N>;

impl<const N: usize> From<U<N>> for u32 {
    #[magic(int::convert)]
    fn from(_value: U<N>) -> Self {
        compiler_magic!()
    }
}

impl<const N: usize> From<U<N>> for u8 {
    #[magic(int::convert)]
    fn from(_value: U<N>) -> Self {
        compiler_magic!()
    }
}

impl<const N: usize> From<i32> for U<N> {
    #[magic(int::convert)]
    fn from(_value: i32) -> U<N> {
        compiler_magic!()
    }
}

impl<const N: usize> From<u32> for U<N> {
    #[magic(int::convert)]
    fn from(_value: u32) -> U<N> {
        compiler_magic!()
    }
}

impl<const N: usize> From<usize> for U<N> {
    #[magic(int::convert)]
    fn from(_value: usize) -> U<N> {
        compiler_magic!()
    }
}

impl<const N: usize> From<u128> for U<N> {
    #[magic(int::convert)]
    fn from(_value: u128) -> U<N> {
        compiler_magic!()
    }
}

impl From<bool> for U<1> {
    #[magic(int::convert)]
    fn from(_value: bool) -> U<1> {
        compiler_magic!()
    }
}

impl<const N: usize> From<U<N>> for bool {
    #[magic(int::convert)]
    fn from(_value: U<N>) -> bool {
        compiler_magic!()
    }
}

impl<const N: usize> Not for U<N> {
    type Output = Self;

    #[magic(int::not)]
    fn not(self) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize, const M: usize> Shr<U<M>> for U<N> {
    type Output = Self;

    #[magic(int::shr)]
    fn shr(self, _rhs: U<M>) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize> Shr<usize> for U<N> {
    type Output = Self;

    #[magic(int::shr)]
    fn shr(self, _rhs: usize) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize, const M: usize> Shl<U<M>> for U<N> {
    type Output = Self;

    #[magic(int::shl)]
    fn shl(self, _lhs: U<M>) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize> Shl<usize> for U<N> {
    type Output = Self;

    #[magic(int::shl)]
    fn shl(self, _lhs: usize) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize> Add<U<N>> for U<N>
where [(); N + 1]:
{
    type Output = U<{ N + 1 }>;

    #[magic(int::add)]
    fn add(self, _rhs: U<N>) -> U<{ N + 1 }> {
        compiler_magic!()
    }
}

#[allow(clippy::identity_op)]
impl<const N: usize> U<N>
where [(); N + 1]:
{
    /// Adds two `U<N>`s and truncate the result to `U<N>`.
    pub fn trunk_add(self, rhs: Self) -> Self {
        (self + rhs).resize()
    }
}

impl<const N: usize> U<N> {
    /// Returns the maximum value of an `N` bit unsigned value. (i.e., 2^`N` - 1)
    pub fn unsigned_max() -> U<N> {
        true.repeat::<N>()
    }
}

impl<const N: usize> Sub<U<N>> for U<N> {
    type Output = U<N>;

    #[magic(int::sub)]
    fn sub(self, _other: U<N>) -> U<N> {
        compiler_magic!()
    }
}

impl<const N: usize, const M: usize> Mul<U<M>> for U<N>
where [(); N + M]:
{
    type Output = U<{ N + M }>;

    #[magic(int::mul)]
    fn mul(self, _other: U<M>) -> Self::Output {
        compiler_magic!()
    }
}

impl<const N: usize> PartialOrd for U<N> {
    fn partial_cmp(&self, _other: &Self) -> Option<Ordering> {
        panic!("placeholder for rust's type system")
    }

    #[magic(int::lt)]
    fn lt(&self, _other: &Self) -> bool {
        compiler_magic!()
    }

    #[magic(int::le)]
    fn le(&self, _other: &Self) -> bool {
        compiler_magic!()
    }

    #[magic(int::gt)]
    fn gt(&self, _other: &Self) -> bool {
        compiler_magic!()
    }

    #[magic(int::ge)]
    fn ge(&self, _other: &Self) -> bool {
        compiler_magic!()
    }
}

/// Trait for converting a type into `U<N>`.
pub trait IntoU {
    /// Converts `self` into `U<N>`.
    fn into_u<const N: usize>(self) -> U<N>;
}

impl IntoU for i32 {
    fn into_u<const N: usize>(self) -> U<N> {
        U::from(self)
    }
}
impl IntoU for usize {
    fn into_u<const N: usize>(self) -> U<N> {
        U::from(self)
    }
}
impl IntoU for u32 {
    fn into_u<const N: usize>(self) -> U<N> {
        U::from(self)
    }
}

impl IntoU for bool {
    fn into_u<const N: usize>(self) -> U<N> {
        U::from(self).resize()
    }
}

impl<const M: usize> IntoU for [bool; M] {
    fn into_u<const N: usize>(self) -> U<N> {
        U::from(self).resize()
    }
}