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
use std::borrow::Cow;
use std::cmp::Ordering;

use super::abc::*;


impl Type for f64 {
    fn iclone(&self) -> BType {
        BType(box *self)
    }
}


impl AsBool for f64 {
    fn to_bool(&self) -> bool {
        *self != 0.0
    }
}


impl AsReal for f64 {
    fn try_as_real(&self) -> Option<f64> {
        Some(*self)
    }

    fn is_real(&self) -> bool {
        true
    }
}


impl AsInt for f64 {
    fn try_as_int(&self) -> Option<i64> {
        Some(*self as i64)
    }
}


impl IPartialEq for f64 {
    fn eq(&self, other: &BType) -> bool {
        other.try_as_real().map(|s| s == *self).unwrap_or(false)
    }
}


impl IPartialOrd for f64 {
    fn partial_cmp(&self, other: &BType) -> Option<Ordering> {
        if other.is_real() || other.is_int() {
            other.try_as_real().and_then(|s| (self as &PartialOrd<f64>).partial_cmp(&s))
        } else {
            None
        }
    }
}


#[cfg_attr(feature = "clippy", allow(boxed_local))]
impl IArithm for f64 {
    // todo Cow::Borrowed for Zero and One cases
    fn try_add<'a>(&self, other: Cow<'a, BType>) -> Option<Cow<'a, BType>> { other.try_as_real().map(|s| { Cow::Owned(ex(*self + s)) }) }
    fn try_sub<'a>(&self, other: Cow<'a, BType>) -> Option<Cow<'a, BType>> { other.try_as_real().map(|s| { Cow::Owned(ex(*self - s)) }) }
    fn try_mul<'a>(&self, other: Cow<'a, BType>) -> Option<Cow<'a, BType>> { other.try_as_real().map(|s| { Cow::Owned(ex(*self * s)) }) }
    fn try_div<'a>(&self, other: Cow<'a, BType>) -> Option<Cow<'a, BType>> { other.try_as_real().map(|s| { Cow::Owned(ex(*self / s)) }) }
}