diff options
Diffstat (limited to 'src/internal.rs')
-rw-r--r-- | src/internal.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/internal.rs b/src/internal.rs new file mode 100644 index 0000000..166e25f --- /dev/null +++ b/src/internal.rs @@ -0,0 +1,47 @@ +use std::fmt; +use std::ops::{Add, Sub, AddAssign, SubAssign, Mul, MulAssign, Div, DivAssign}; + +pub trait Internal: + Copy + Clone + PartialEq + fmt::Debug + fmt::Display + PartialOrd + + Add<Output=Self> + Sub<Output=Self> + AddAssign + SubAssign + + Mul<Output=Self> + MulAssign + Div<Output=Self> + DivAssign + +{ + const ZERO: Self; + + fn as_usize(&self) -> usize; + + fn as_f64(&self) -> f64; +} + +macro_rules! impl_for_type { + ($z:expr, $t:ty) => { + impl Internal for $t { + const ZERO: Self = $z; + + fn as_usize(&self) -> usize { + *self as usize + } + + fn as_f64(&self) -> f64 { + *self as f64 + } + } + }; +} + +impl_for_type!(0, u8); +impl_for_type!(0, u16); +impl_for_type!(0, u32); +impl_for_type!(0, u64); +impl_for_type!(0, u128); +impl_for_type!(0, usize); + +impl_for_type!(0, i8); +impl_for_type!(0, i16); +impl_for_type!(0, i32); +impl_for_type!(0, i64); +impl_for_type!(0, i128); +impl_for_type!(0, isize); + +impl_for_type!(0.0, f32); +impl_for_type!(0.0, f64); |