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
use super::IntegerOrFloat::{self, *};
use crate::f_iof;

use num_traits;

mod float;

#[cfg(feature = "num-traits")]
impl num_traits::cast::ToPrimitive for IntegerOrFloat {
    fn to_u64(&self) -> Option<u64> {
        match self {
            Integer(i) => i.to_u64(),
            Float(f) => f.to_u64(),
        }
    }
    fn to_i64(&self) -> Option<i64> {
        match self {
            Integer(i) => i.to_i64(),
            Float(f) => f.to_i64(),
        }
    }
}

impl num_traits::Zero for IntegerOrFloat {
    fn zero() -> Self {
        Integer(0)
    }
    fn is_zero(&self) -> bool {
        *self == Integer(0)
    }
}

impl num_traits::One for IntegerOrFloat {
    fn one() -> Self {
        Integer(1)
    }
    fn is_one(&self) -> bool {
        *self == Integer(1)
    }
}

impl num_traits::NumCast for IntegerOrFloat {
    fn from<N: num_traits::ToPrimitive>(num: N) -> Option<IntegerOrFloat> {
        num.to_f64().map(|n| IntegerOrFloat::Float(n as f_iof))
    }
}