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
/// Implements [`BoundedInteger`](trait.BoundedInteger.html) for a C-like enum with contiguous
/// variants.
///
/// Uses `std::mem::transmute` to implement `BoundedInteger::from_repr` and `as` to implement
/// `BoundedInteger::to_repr`.
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate bounded_integer;
/// #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// #[repr(u8)]
/// enum TwoBit { Z0, P1, P2, P3 }
/// bounded_integer_impl!(TwoBit, u8, TwoBit::Z0, TwoBit::P3);
/// # fn main() { }
/// ```
#[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 }
        }
    }
}

/// Implements `Into<Self::Repr>` for a [`BoundedInteger`](trait.BoundedInteger.html).
#[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()
            }
        }
    }
}

/// Implements all bounded integer traits for a C-like enum with contiguous variants.
///
/// - [`BoundedInteger`](trait.BoundedInteger.html)
/// - `Into<Self::Repr>`
/// - `Add<Self>`
/// - `Add<Self::Repr>`
/// - `Sub<Self>`
/// - `Sub<Self::Repr>`
/// - `Mul<Self>`
/// - `Mul<Self::Repr>`
/// - `Div<Self>`
/// - `Div<Self::Repr>`
/// - `Rem<Self>`
/// - `Rem<Self::Repr>`
/// - `Neg`
///
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate bounded_integer;
/// #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// #[repr(u8)]
/// enum TwoBit { Z0, P1, P2, P3 }
/// bounded_integer_impls!(TwoBit, u8, TwoBit::Z0, TwoBit::P3);
/// # fn main() { }
/// ```
#[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;