#[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 }
}
}
}
#[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()
}
}
}
}
#[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;