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
//! Native number coercions and comparisons. I think rust already does this with the `Wrapped`
//! traits...
use std;
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;

use num::{ToPrimitive};

use ::system::primitives::{HashId};
use ::system::primitives as rs;


pub fn format_float(float: &rs::Float) -> rs::String {
    format!("{:?}", *float)

}

pub fn format_int(int: &rs::Integer) -> rs::String {
    format!("{}", *int)
}


pub fn hash_int(int: &rs::Integer) -> HashId {
    let mut s = DefaultHasher::new();
    int.hash(&mut s);
    s.finish()
}

// To make int == float not such a pain in the ass
pub struct IntAdapter<'a>(pub &'a rs::Integer);
pub struct FloatAdapter<'a>(pub &'a rs::Float);

impl<'a, 'b> std::cmp::PartialEq<IntAdapter<'b>> for FloatAdapter<'a> {
    fn eq(&self, other: &IntAdapter) -> bool {
        match other.0.to_f64() {
            Some(num)   => *self.0 == num,
            None        => false
        }
    }
}

impl<'a, 'b> std::cmp::PartialEq<FloatAdapter<'b>> for IntAdapter<'a> {
    fn eq(&self, other: &FloatAdapter) -> bool {
        match self.0.to_f64() {
            Some(num)   => num == *other.0,
            None        => false
        }
    }
}

//
//impl<'a, 'b> std::ops::Add<FloatAdapter<'b>> for IntAdapter<'a> {
//    type Output = rs::Float;
//
//    fn add(self, rhs: FloatAdapter) -> Self::Output {
//        match self.0
//    }
//}