light_ranged_integers 0.1.3

Ranged integers for stable Rust compiler, zero-dependencies and no unsafe code.
Documentation
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
Copyright © 2024 - Massimo Gismondi

This file is part of light-ranged-integers.

light-ranged-integers is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

light-ranged-integers is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with light-ranged-integers.  If not, see <http://www.gnu.org/licenses/>.
*/

use crate::op_mode;
use std::marker::PhantomData;

macro_rules! gen_ranged_structs {
    ($i:ty, $typename:ident, $const_typename:ident) => {
        #[derive(Debug, Clone, Copy)]
        #[doc = concat!("A ranged integer between MIN and MAX, encapsulating [", stringify!($i), "] type.\n\n")]
        ///
        /// The library offers Range types, declared in this form
        /// ```rust,ignore
        #[doc= concat!(stringify!($typename), "<MIN, MAX, OpMode>")]
        /// ```
        /// where:
        /// - `MIN` is a lower limit of the interval
        /// - `MAX` is the upper limit of the interval
        /// - `OpMode` selects how to behave when a number goes out of range. Have a
        ///    look at [op_mode] for all available modes.
        /// ```rust
        /// use light_ranged_integers::{RangedU16, op_mode::Panic};
        ///
        /// // Creating a Ranged value of 3, in the interval from 1 to 6.
        /// let n1 = RangedU16::<1,6, Panic>::new(3);
        ///
        /// ```
        /// The interval must be valid, as the MIN limit must be smaller than MAX.
        /// This is checked at compile time.
        /// ```rust,compile_fail
        /// # use light_ranged_integers::RangedU8;
        /// RangedU8::<2,1>::new(2);
        /// ```
        ///
        /// It will fail to compile even if the limits are the same
        /// ```rust,compile_fail
        /// # use light_ranged_integers::RangedU8;
        /// RangedU8::<2,2>::new(2);
        /// ```
        ///
        /// Pay attention that the code at compile time will check only the validity
        /// of the interval, but the check of the provided value fitting the interval is done at runtime.
        /// If you desire to move that check at compile time too, have a look at [Self::new_const()](Self::new_const()) constructor.
        pub struct $typename<const MIN: $i = {<$i>::MIN}, const MAX:$i = {<$i>::MAX}, OpMode=op_mode::Panic>($i, PhantomData<OpMode>) where OpMode: op_mode::OpModeExt;



        // Implement methods common for all modes
        impl<OpMode, const MIN: $i, const MAX:$i> $typename<MIN, MAX, OpMode> where OpMode: op_mode::OpModeExt
        {
            const COMPTIME_RANGE_CHECK: () = assert!(MIN < MAX, "INVALID RANGE. MIN must be smaller than MAX");

            #[doc=concat!("Builds a new [",stringify!($typename),"] checking the limits at runtime")]
            ///
            /// The number MUST be inside the interval.
            /// Will panic if the number is outside the interval.
            ///
            /// It will always panic **INDEPENDENTLY** of OpMode
            pub const fn new(v: $i) -> Self
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                Self::check_in_range(v);
                Self(v, PhantomData)
            }

            /// Applies the OpMode to try to bring the
            /// provided value into its intended range
            pub fn new_adjust(v:$i) -> Self
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                let v = OpMode::bring_in_range(v, MIN, MAX);
                Self::check_in_range(v);
                Self(v, PhantomData)
            }

            /// This constructor will check both range validity and that the value is in the range
            /// at Compile time.
            ///
            /// The `V` number MUST be in range to compile correctly.
            ///
            /// For example, this code will compile just fine:
            /// ```
            #[doc=concat!("use light_ranged_integers::",stringify!($typename),";")]
            #[doc=concat!("let m = ",stringify!($typename),"::<1,4>::new_const::<2>();")]
            /// ```
            ///
            /// But this one will not compile, as 5 is outside of \[1,4\]
            ///  ```rust,compile_fail
            #[doc=concat!("use light_ranged_integers::",stringify!($typename),";")]
            #[doc=concat!("let m = ",stringify!($typename),"::<1,4>::new_const::<5>();")]
            /// ```
            pub const fn new_const<const V: $i>() -> $typename::<MIN, MAX, OpMode>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                const_ranged::$const_typename::<MIN, MAX, V>::new().as_ranged::<OpMode>()
            }


            #[doc=concat!("Tries to construct a [",stringify!($typename),"]")]
            ///
            /// Checks the range during initialization.
            /// If the value fits the range, returns a new Ranged value,
            /// otherwise returns None
            pub const fn new_try(value: $i) -> Option<Self>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                if value >= MIN && value <= MAX
                {
                    return Some(Self(value, PhantomData))
                }
                else
                {
                    return None
                }
            }

            /// Returns a tuple containing the (MIN,MAX) interval limits
            pub const fn get_limits(self) -> ($i,$i)
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                (MIN,MAX)
            }

            /// Limits the value to a new minimum and maximum.
            ///
            /// Panics if the value does not fit the new range
            pub fn limit<const NEW_MIN: $i, const NEW_MAX: $i>(self) -> $typename<NEW_MIN, NEW_MAX, OpMode>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                $typename::<NEW_MIN, NEW_MAX, OpMode>::new(self.0)
            }

            /// Limits the value to a new minimum and maximum.
            ///
            /// Adjusts the value by appliying the relevant [OpMode](op_mode::OpModeExt)
            /// if the value does not fit the new range
            pub fn limit_adjust<const NEW_MIN: $i, const NEW_MAX: $i>(self) -> $typename<NEW_MIN, NEW_MAX, OpMode>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                $typename::<NEW_MIN, NEW_MAX, OpMode>::new_adjust(self.0)
            }


            #[doc="Limits the value to a new minimum; keeps the previous maximum value limit."]
            pub fn limit_min<const NEW_MIN:$i>(self) -> $typename<NEW_MIN, MAX, OpMode>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                self.limit::<NEW_MIN, MAX>()
            }

            #[doc="Limits the value to a new maximum; keeps the previous minimum value limit."]
            pub fn limit_max<const NEW_MAX:$i>(self) -> $typename<MIN, NEW_MAX, OpMode>
            {
                let _ = Self::COMPTIME_RANGE_CHECK;
                self.limit::<MIN, NEW_MAX>()
            }

            /// Returns the inner value alone
            pub const fn inner(&self) -> $i
            {
                debug_assert!(self.0>=MIN && self.0<=MAX);
                let _ = Self::COMPTIME_RANGE_CHECK;
                self.0
            }

            /// Create a new Ranged number,
            /// keeping the same interval
            /// but applying the new mode
            pub const fn as_mode<NewOpMode: op_mode::OpModeExt>(self) -> $typename<MIN,MAX,NewOpMode>
            {
                $typename::<MIN,MAX,NewOpMode>(self.0, PhantomData)
            }

            const fn check_in_range(v: $i)
            {
                assert!(v >= MIN && v <= MAX);
            }
        }

        impl<OpMode, const MIN: $i, const MAX:$i> std::cmp::PartialEq<$i> for $typename<MIN, MAX, OpMode> where OpMode: op_mode::OpModeExt
        {
            fn eq(&self, other: &$i) -> bool
            {
                self.0.eq(other)
            }
        }
        impl<OpMode, const MIN: $i, const MAX:$i> std::cmp::PartialEq for $typename<MIN, MAX, OpMode> where OpMode: op_mode::OpModeExt
        {
            fn eq(&self, other: &$typename<MIN, MAX, OpMode>) -> bool
            {
                self.0.eq(&other.0)
            }
        }
        impl<OpMode, const MIN: $i, const MAX:$i> std::cmp::PartialOrd<$i> for $typename<MIN, MAX, OpMode> where OpMode: op_mode::OpModeExt
        {
            fn partial_cmp(&self, other: &$i) -> Option<std::cmp::Ordering>
            {
                self.0.partial_cmp(other)
            }
        }
        impl<OpMode, const MIN: $i, const MAX:$i> std::cmp::PartialOrd for $typename<MIN, MAX, OpMode> where OpMode: op_mode::OpModeExt
        {
            fn partial_cmp(&self, other: &$typename<MIN, MAX, OpMode>) -> Option<std::cmp::Ordering>
            {
                self.0.partial_cmp(&other.0)
            }
        }

        impl<OpMode, const MIN: $i, const MAX:$i> From<$i> for $typename<MIN,MAX,OpMode>
            where OpMode: op_mode::OpModeExt
        {
            fn from(value: $i) -> Self {
                Self::new(value)
            }
        }

        impl<OpMode, const MIN: $i, const MAX:$i> std::fmt::Display for $typename<MIN,MAX,OpMode>
            where OpMode: op_mode::OpModeExt
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!{f, "{}", self.inner()}
            }
        }
    };
    }

gen_ranged_structs!(u8, RangedU8, ConstRangedU8);
gen_ranged_structs!(u16, RangedU16, ConstRangedU16);
gen_ranged_structs!(u32, RangedU32, ConstRangedU32);
gen_ranged_structs!(u64, RangedU64, ConstRangedU64);
gen_ranged_structs!(i8, RangedI8, ConstRangedI8);
gen_ranged_structs!(i16, RangedI16, ConstRangedI16);
gen_ranged_structs!(i32, RangedI32, ConstRangedI32);
gen_ranged_structs!(i64, RangedI64, ConstRangedI64);

// OPERATIONS IMPLEMENTATION
mod wrap
{
    use super::*;

    macro_rules! gen_add {
        ($i:ty, $bigger:ty, $typename:ident) => {
            impl<const MIN: $i, const MAX: $i> std::ops::Add<$i>
                for $typename<MIN, MAX, op_mode::Wrap>
            {
                type Output = Self;
                fn add(self, rhs: $i) -> Self::Output
                {
                    // KISS "keep it simple stupid"
                    // there was a mathematically cool but longer implementation
                    // Now I simply use a bigger type to avoid overflows
                    // It still uses less variables than the "cool" version
                    let min: $bigger = MIN as $bigger;
                    let max: $bigger = MAX as $bigger;
                    let interval_size: $bigger = max - min + 1;
                    let rhs: $bigger = (rhs as $bigger).rem_euclid(interval_size);

                    let res: $i = <$i>::try_from(
                        ((((self.inner() as $bigger) - min) + rhs) % interval_size) + min
                    )
                    .unwrap();

                    return $typename(res, PhantomData);
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Sub<$i>
                for $typename<MIN, MAX, op_mode::Wrap>
            {
                type Output = Self;
                fn sub(self, rhs: $i) -> Self::Output
                {
                    // KISS "keep it simple stupid"
                    // there was a mathematically cool but longer implementation
                    // Now I simply use a bigger type to avoid overflows
                    // It still uses less variables than the "cool" version
                    let min: $bigger = MIN as $bigger;
                    let max: $bigger = MAX as $bigger;
                    let interval_size: $bigger = max - min + 1;
                    let rhs: $bigger = (rhs as $bigger).rem_euclid(interval_size);

                    // This is exactly the code for Addition, but
                    // we use the modular arithmetic trick to
                    // convert the Subtraction to an Addition
                    // self - rhs (mod L) ≡ self + (L-rhs) (mod L)
                    let res: $i = <$i>::try_from(
                        ((((self.inner() as $bigger) - min) + (interval_size - rhs))
                            % interval_size)
                            + min
                    )
                    .unwrap();
                    return $typename(res, PhantomData);
                }
            }
        };
    }

    gen_add!(u8, u16, RangedU8);
    gen_add!(u16, u32, RangedU16);
    gen_add!(u32, u64, RangedU32);
    gen_add!(u64, u128, RangedU64);
    gen_add!(i8, i16, RangedI8);
    gen_add!(i16, i32, RangedI16);
    gen_add!(i32, i64, RangedI32);
    gen_add!(i64, i128, RangedI64);
}

mod panic
{
    use super::*;
    macro_rules! gen_panic_impl {
        ($i:ty, $typename:ident) => {
            // Additions
            impl<const MIN: $i, const MAX: $i> std::ops::Add for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn add(self, rhs: $typename<MIN, MAX, op_mode::Panic>) -> Self::Output
                {
                    self + rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Add<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn add(self, rhs: $i) -> Self::Output
                {
                    Self::new(self.inner().checked_add(rhs).unwrap())
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::AddAssign
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn add_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Panic>)
                {
                    *self += rhs.inner();
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::AddAssign<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn add_assign(&mut self, rhs: $i)
                {
                    *self = Self::new(self.inner().checked_add(rhs).unwrap());
                }
            }

            // Subtraction
            impl<const MIN: $i, const MAX: $i> std::ops::Sub for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn sub(self, rhs: $typename<MIN, MAX, op_mode::Panic>) -> Self::Output
                {
                    self - rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Sub<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn sub(self, rhs: $i) -> Self::Output
                {
                    Self::new(self.inner().checked_sub(rhs).unwrap())
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::SubAssign
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn sub_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Panic>)
                {
                    *self -= rhs.inner();
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::SubAssign<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn sub_assign(&mut self, rhs: $i)
                {
                    *self = Self::new(self.inner().checked_sub(rhs).unwrap());
                }
            }

            // Multiplication
            impl<const MIN: $i, const MAX: $i> std::ops::Mul for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn mul(self, rhs: $typename<MIN, MAX, op_mode::Panic>) -> Self::Output
                {
                    self * rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Mul<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn mul(self, rhs: $i) -> Self::Output
                {
                    Self::new(self.inner().checked_mul(rhs).unwrap())
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::MulAssign
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn mul_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Panic>)
                {
                    *self = Self::new(self.inner().checked_mul(rhs.inner()).unwrap());
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::MulAssign<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn mul_assign(&mut self, rhs: $i)
                {
                    *self = Self::new(self.inner().checked_mul(rhs).unwrap());
                }
            }

            // Division
            impl<const MIN: $i, const MAX: $i> std::ops::Div for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn div(self, rhs: $typename<MIN, MAX, op_mode::Panic>) -> Self::Output
                {
                    self / rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Div<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                type Output = Self;
                fn div(self, rhs: $i) -> Self::Output
                {
                    Self::new(self.inner().checked_div(rhs).unwrap())
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::DivAssign
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn div_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Panic>)
                {
                    *self = Self::new(self.inner().checked_div(rhs.inner()).unwrap());
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::DivAssign<$i>
                for $typename<MIN, MAX, op_mode::Panic>
            {
                fn div_assign(&mut self, rhs: $i)
                {
                    *self = Self::new(self.inner().checked_div(rhs).unwrap());
                }
            }
        };
    }

    gen_panic_impl!(u8, RangedU8);
    gen_panic_impl!(u16, RangedU16);
    gen_panic_impl!(u32, RangedU32);
    gen_panic_impl!(u64, RangedU64);
    gen_panic_impl!(i8, RangedI8);
    gen_panic_impl!(i16, RangedI16);
    gen_panic_impl!(i32, RangedI32);
    gen_panic_impl!(i64, RangedI64);
}

mod clamp
{
    use super::*;
    macro_rules! gen_clamp_impl {
        ($i:ty, $typename:ident) => {
            // Additions
            impl<const MIN: $i, const MAX: $i> std::ops::Add for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn add(self, rhs: $typename<MIN, MAX, op_mode::Clamp>) -> Self::Output
                {
                    self + rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Add<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn add(self, rhs: $i) -> Self::Output
                {
                    Self::new_adjust(self.inner().saturating_add(rhs))
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::AddAssign
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn add_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Clamp>)
                {
                    *self = *self + rhs;
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::AddAssign<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn add_assign(&mut self, rhs: $i)
                {
                    *self = Self::new_adjust(self.inner().saturating_add(rhs));
                }
            }

            // Subtraction
            impl<const MIN: $i, const MAX: $i> std::ops::Sub for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn sub(self, rhs: $typename<MIN, MAX, op_mode::Clamp>) -> Self::Output
                {
                    self - rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Sub<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn sub(self, rhs: $i) -> Self::Output
                {
                    Self::new_adjust(self.inner().saturating_sub(rhs))
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::SubAssign
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn sub_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Clamp>)
                {
                    *self -= rhs.inner();
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::SubAssign<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn sub_assign(&mut self, rhs: $i)
                {
                    *self = Self::new_adjust(self.inner().saturating_sub(rhs));
                }
            }

            // Multiplication
            impl<const MIN: $i, const MAX: $i> std::ops::Mul for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn mul(self, rhs: $typename<MIN, MAX, op_mode::Clamp>) -> Self::Output
                {
                    self * rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Mul<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn mul(self, rhs: $i) -> Self::Output
                {
                    Self::new_adjust(self.inner().saturating_mul(rhs))
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::MulAssign
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn mul_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Clamp>)
                {
                    *self = *self * rhs;
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::MulAssign<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn mul_assign(&mut self, rhs: $i)
                {
                    *self = *self * rhs;
                }
            }

            // Division
            impl<const MIN: $i, const MAX: $i> std::ops::Div for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn div(self, rhs: $typename<MIN, MAX, op_mode::Clamp>) -> Self::Output
                {
                    self / rhs.inner()
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::Div<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                type Output = Self;
                fn div(self, rhs: $i) -> Self::Output
                {
                    Self::new_adjust(self.inner().saturating_div(rhs))
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::DivAssign
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn div_assign(&mut self, rhs: $typename<MIN, MAX, op_mode::Clamp>)
                {
                    *self = *self / rhs;
                }
            }
            impl<const MIN: $i, const MAX: $i> std::ops::DivAssign<$i>
                for $typename<MIN, MAX, op_mode::Clamp>
            {
                fn div_assign(&mut self, rhs: $i)
                {
                    *self = *self / rhs;
                }
            }
        };
    }

    gen_clamp_impl!(u8, RangedU8);
    gen_clamp_impl!(u16, RangedU16);
    gen_clamp_impl!(u32, RangedU32);
    gen_clamp_impl!(u64, RangedU64);
    gen_clamp_impl!(i8, RangedI8);
    gen_clamp_impl!(i16, RangedI16);
    gen_clamp_impl!(i32, RangedI32);
    gen_clamp_impl!(i64, RangedI64);
}

/// Compile time checked ranges
mod const_ranged
{
    use super::*;
    use crate::op_mode;
    macro_rules! gen_const_ranged_structs {
        ($i:ty, $typename:ident, $const_typename:ident) => {
            #[derive(Clone, Copy)]
            #[doc = concat!("A compile-time checked [",stringify!($typename),"] struct.\n\n")]
            pub struct $const_typename<const MIN: $i, const MAX: $i, const V: $i>;
            impl<const MIN: $i, const MAX: $i, const V: $i> $const_typename<MIN, MAX, V>
            {
                const RANGE_VALID: () = assert!(
                    MIN < MAX,
                    "The range is not valid. MIN must be smaller than MAX"
                );
                const IS_IN_RANGE: () = assert!(
                    V >= MIN && V <= MAX,
                    "Provided value V is outside of the range"
                );

                /// Creates a new compile-checked range number.
                /// The number MUST be in range to compile correctly,
                /// independently of the [OpMode](op_mode)
                pub const fn new() -> Self
                {
                    let _ = Self::RANGE_VALID;
                    let _ = Self::IS_IN_RANGE;
                    return $const_typename::<MIN, MAX, V>;
                }

                /// Converts to a runtime Ranged value, assigning the provided OpMode
                pub const fn as_ranged<OpMode: op_mode::OpModeExt>(
                    &self
                ) -> $typename<MIN, MAX, OpMode>
                {
                    return $typename::<MIN, MAX, OpMode>(V, PhantomData);
                }
            }
        };
    }

    gen_const_ranged_structs!(u8, RangedU8, ConstRangedU8);
    gen_const_ranged_structs!(u16, RangedU16, ConstRangedU16);
    gen_const_ranged_structs!(u32, RangedU32, ConstRangedU32);
    gen_const_ranged_structs!(u64, RangedU64, ConstRangedU64);
    gen_const_ranged_structs!(i8, RangedI8, ConstRangedI8);
    gen_const_ranged_structs!(i16, RangedI16, ConstRangedI16);
    gen_const_ranged_structs!(i32, RangedI32, ConstRangedI32);
    gen_const_ranged_structs!(i64, RangedI64, ConstRangedI64);
}

// // Shortcuts
// #[doc = concat!("A ranged integer between the provided MIN and [", $i ,"::MAX].\n\n")]
// pub type [<Ranged $i:upper Min>]<const MIN:$i, OpMode=op_mode::Panic> = [<Ranged $i:upper>]<MIN, {$i::MAX}, OpMode>;
// #[doc = "A ranged integer between [" $i "::MIN] and the provided MAX.\n\n"]
// pub type [<Ranged $i:upper Max>]<const MAX:$i, OpMode=op_mode::Panic> = [<Ranged $i:upper>]<{$i::MIN}, MAX, OpMode>;