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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use crate::Error;
macro_rules! bounded_ints {
($($wrapped_name:ident($num_type:ident)),*) => {
$(
/// Bounded type (inclusive) that holds a value that is `MIN <= value <= MAX`
///
/// Has the same representation as the primitive type.
///
/// ### Safety
///
/// Creating an instance with `MIN > MAX` is undefined behavior.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $wrapped_name<const MIN: $num_type, const MAX: $num_type> {
inner: $num_type,
}
impl<const MIN: $num_type, const MAX: $num_type> $wrapped_name<MIN, MAX> {
/// Upper bound.
pub const MAX: $num_type = MAX;
/// Upper bound, represented as the *bounded* type.
pub const MAX_PACKED: Self = Self::pack_const(MAX);
/// Lower bound.
pub const MIN: $num_type = MIN;
/// Lower bound, represented as the *bounded* type.
pub const MIN_PACKED: Self = Self::pack_const(MIN);
/// Returns `Self::MIN`.
pub const fn min(&self) -> $num_type {
MIN
}
/// Returns `Self::MAX`.
pub const fn max(&self) -> $num_type {
MAX
}
fn pack(value: $num_type) -> Self {
Self {
inner: value
}
}
const fn pack_const(value: $num_type) -> Self {
Self {
inner: value
}
}
#[inline]
fn assert() {
assert!(MIN < MAX);
}
/// Create a new value of the type.
///
/// Returns the error if the provided `value` was outside of the defined
/// bounds.
pub fn new<N: Into<$num_type> + PartialOrd<$num_type> + Ord>(
value: N,
) -> Result<Self, Error> {
Self::assert();
if value > MAX {
Err(Error::AboveUpperBound)
} else if value < MIN {
Err(Error::BelowLowerBound)
} else {
Ok(Self::pack(value.into()))
}
}
/// Create a new value of the type without any checks.
///
/// ### Safety
///
/// User is responsible for providing the value that is already between
/// the defined bounds. Creating a value outside of the defined bounds
/// is undefined behavior.
pub unsafe fn new_unchecked<N: Into<$num_type>>(value: N) -> Self {
Self::assert();
Self::pack(value.into())
}
/// Create a new value of the type.
///
/// If `value` is within range, returns a new instance; otherwise,
/// clamps it to the upper or lower bound.
pub fn new_clamped<N: Into<$num_type> + PartialOrd<$num_type> + Ord>(value: N) -> Self {
Self::pack(Self::clamp(value.into()))
}
/// Returns the underlying value.
///
/// This value is guaranteed to be between `Self::MIN` and `Self::MAX`.
pub fn get(self) -> $num_type {
self.inner
}
/// `const`-compatible `Self::get`.
pub const fn get_const(&self) -> $num_type {
self.inner
}
/// Check if abstract value is within bounds.
pub fn is_in_bounds(n: &$num_type) -> bool {
n >= &Self::MIN && n <= &Self::MAX
}
fn filter_map_opt(n: Option<$num_type>) -> Option<Self> {
n.filter(Self::is_in_bounds).map(Self::pack)
}
/// Clamp the provided `n` between the `Self::MIN` and `Self::MAX`.
pub fn clamp(n: $num_type) -> $num_type {
n.clamp(MIN, MAX)
}
/// Checked integer addition. Computes `self + rhs`, returning `None` if
/// either bound overflow or integer overflow occurred.
pub fn checked_add<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_add(rhs.into()))
}
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow
/// cannot occur.
///
/// ### Safety
///
/// This results in undefined behavior if the result overflows the primitive
/// type's bounds or the `[MIN, MAX]` bounds of this type.
pub unsafe fn unchecked_add<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Self {
Self::pack(unsafe { self.inner.unchecked_add(rhs.into()) })
}
/// Checked integer subtraction. Computes `self - rhs`, returning `None` if
/// either bound overflow or integer overflow occurred.
pub fn checked_sub<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_sub(rhs.into()))
}
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
/// cannot occur.
///
/// ### Safety
///
/// This results in undefined behavior if the result overflows the primitive
/// type's bounds or the `[MIN, MAX]` bounds of this type.
pub unsafe fn unchecked_sub<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Self {
Self::pack(unsafe { self.inner.unchecked_sub(rhs.into()) })
}
/// Checked integer multiplication. Computes `self * rhs`, returning `None`
/// if either bound overflow or integer overflow occurred.
pub fn checked_mul<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_mul(rhs.into()))
}
/// Unchecked integer multiplication. Computes `self * rhs`, assuming
/// overflow cannot occur.
///
/// ### Safety
///
/// This results in undefined behavior if the result overflows the primitive
/// type's bounds or the `[MIN, MAX]` bounds of this type.
pub unsafe fn unchecked_mul<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Self {
Self::pack(unsafe { self.inner.unchecked_mul(rhs.into()) })
}
/// Checked integer division. Computes `self / rhs`, returning `None` if
/// either bound overflow or integer overflow occurred.
pub fn checked_div<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_div(rhs.into()))
}
/// Checked integer remainder. Computes `self % rhs`, returning `None` if
/// either bound overflow or integer overflow occurred.
pub fn checked_rem<Rhs: Into<$num_type>>(self, rhs: Rhs) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_rem(rhs.into()))
}
/// Checked negation. Computes `-self`, returning `None` if bound overflow
/// or `self == inner_num_repr::MIN` occurred.
pub fn checked_neg(self) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_neg())
}
/// Checked integer exponentiation. Computes `self.pow()`, returning `None`
/// if bound overflow or `self == inner_num_repr::MIN` occurred.
pub fn checked_pow(self, exp: u32) -> Option<Self> {
Self::filter_map_opt(self.inner.checked_pow(exp))
}
}
impl<const MIN: $num_type, const MAX: $num_type> ::core::fmt::Display
for $wrapped_name<MIN, MAX>
{
/// Uses the formatter of the `inner_num_repr`.
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
self.inner.fmt(f)
}
}
impl<const MIN: $num_type, const MAX: $num_type> ::core::fmt::Debug
for $wrapped_name<MIN, MAX>
{
/// Example of the string representation:
///
/// ```text
/// 5 ∈ [1, 10]
/// ```
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{} ∈ [{MIN}, {MAX}]", self.inner)
}
}
impl<const MIN: $num_type, const MAX: $num_type> PartialEq<$num_type>
for $wrapped_name<MIN, MAX>
{
fn eq(&self, other: &$num_type) -> bool {
self.inner.eq(other)
}
fn ne(&self, other: &$num_type) -> bool {
self.inner.ne(other)
}
}
impl<const MIN: $num_type, const MAX: $num_type> PartialOrd<$num_type> for $wrapped_name<MIN, MAX> {
fn partial_cmp(&self, other: &$num_type) -> Option<::core::cmp::Ordering> {
self.inner.partial_cmp(other)
}
fn lt(&self, other: &$num_type) -> bool {
self.inner.lt(other)
}
fn le(&self, other: &$num_type) -> bool {
self.inner.le(other)
}
fn gt(&self, other: &$num_type) -> bool {
self.inner.gt(other)
}
fn ge(&self, other: &$num_type) -> bool {
self.inner.ge(other)
}
}
impl<const MIN: $num_type, const MAX: $num_type> Into<$num_type>
for $wrapped_name<MIN, MAX>
{
fn into(self) -> $num_type {
self.inner
}
}
impl<const MIN: $num_type, const MAX: $num_type> TryFrom<$num_type>
for $wrapped_name<MIN, MAX>
{
type Error = Error;
fn try_from(value: $num_type) -> Result<Self, Self::Error> {
Self::new(value)
}
}
)*
}
}
bounded_ints! {
BoundedInt8(i8),
BoundedInt16(i16),
BoundedInt32(i32),
BoundedInt64(i64),
BoundedIsize(isize),
BoundedUInt8(u8),
BoundedUInt16(u16),
BoundedUInt32(u32),
BoundedUInt64(u64),
BoundedUsize(usize)
}
macro_rules! impl_from_for_i8 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ i8::MIN as _ }, { i8::MAX as _ }>> for i8 {
fn from(value: $bounded_int_name<-128, 127>) -> Self {
value.inner as i8
}
}
)*
};
}
macro_rules! impl_from_for_u8 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ u8::MIN as _ }, { u8::MAX as _ }>> for u8 {
fn from(value: $bounded_int_name<0, 255>) -> Self {
value.inner as u8
}
}
)*
};
}
macro_rules! impl_from_for_i16 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ i16::MIN as _ }, { i16::MAX as _ }>> for i16 {
fn from(value: $bounded_int_name<-32768, 32767>) -> Self {
value.inner as i16
}
}
)*
};
}
macro_rules! impl_from_for_u16 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ u16::MIN as _ }, { u16::MAX as _ }>> for u16 {
fn from(value: $bounded_int_name<0, 65535>) -> Self {
value.inner as u16
}
}
)*
};
}
macro_rules! impl_from_for_i32 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ i32::MIN as _ }, { i32::MAX as _ }>> for i32 {
fn from(value: $bounded_int_name<-2147483648, 2147483647>) -> Self {
value.inner as i32
}
}
)*
};
}
macro_rules! impl_from_for_u32 {
($($bounded_int_name:ident),*) => {
$(
impl From<$bounded_int_name<{ u32::MIN as _ }, { u32::MAX as _ }>> for u32 {
fn from(value: $bounded_int_name<0, 4294967295>) -> Self {
value.inner as u32
}
}
)*
};
}
impl_from_for_i8!(BoundedInt16, BoundedInt32, BoundedInt64, BoundedIsize);
impl_from_for_u8!(
BoundedUInt16,
BoundedUInt32,
BoundedUInt64,
BoundedUsize,
BoundedInt16,
BoundedInt32,
BoundedInt64,
BoundedIsize
);
impl_from_for_i16!(BoundedInt32, BoundedInt64, BoundedIsize);
impl_from_for_u16!(
BoundedUInt32,
BoundedUInt64,
BoundedUsize,
BoundedInt32,
BoundedInt64,
BoundedIsize
);
impl_from_for_i32!(BoundedInt64, BoundedIsize);
impl_from_for_u32!(BoundedUInt64, BoundedUsize, BoundedInt64);