summaryrefslogtreecommitdiff
path: root/src/internal.rs
blob: 166e25ff8d6b509aa05251726e338a43dd8d94c2 (plain) (blame)
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
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);