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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use num;
use super::*;

pub trait TryAsRef<T> {
    fn try_as_ref(&self) -> Option<&T>;
}

impl<T> TryAsRef<T> for T {
    fn try_as_ref(&self) -> Option<&T> {
        Some(self)
    }
}

macro_rules! impl_term_try_as_ref {
    ($to:ident) => {
        impl TryAsRef<$to> for Term {
            fn try_as_ref(&self) -> Option<&$to> {
                match *self {
                    Term::$to(ref x) => Some(x),
                    _ => None,
                }
            }
        }
    }
}
impl_term_try_as_ref!(Atom);
impl_term_try_as_ref!(FixInteger);
impl_term_try_as_ref!(BigInteger);
impl_term_try_as_ref!(Float);
impl_term_try_as_ref!(Pid);
impl_term_try_as_ref!(Port);
impl_term_try_as_ref!(Reference);
impl_term_try_as_ref!(ExternalFun);
impl_term_try_as_ref!(InternalFun);
impl_term_try_as_ref!(Binary);
impl_term_try_as_ref!(BitBinary);
impl_term_try_as_ref!(List);
impl_term_try_as_ref!(ImproperList);
impl_term_try_as_ref!(Tuple);
impl_term_try_as_ref!(Map);

pub trait TryInto<T> {
    fn try_into(self) -> Result<T, Self> where Self: Sized;
}

impl<T> TryInto<T> for T {
    fn try_into(self) -> Result<T, Self>
        where Self: Sized
    {
        Ok(self)
    }
}

macro_rules! impl_term_try_into {
    ($to:ident) => {
        impl TryInto<$to> for Term {
            fn try_into(self) -> Result<$to, Self> where Self: Sized {
                match self {
                    Term::$to(x) => Ok(x),
                    _ => Err(self)
                }
            }
        }
    }
}
impl_term_try_into!(Atom);
impl_term_try_into!(FixInteger);
impl_term_try_into!(BigInteger);
impl_term_try_into!(Float);
impl_term_try_into!(Pid);
impl_term_try_into!(Port);
impl_term_try_into!(Reference);
impl_term_try_into!(ExternalFun);
impl_term_try_into!(InternalFun);
impl_term_try_into!(Binary);
impl_term_try_into!(BitBinary);
impl_term_try_into!(List);
impl_term_try_into!(ImproperList);
impl_term_try_into!(Tuple);
impl_term_try_into!(Map);

pub trait AsOption {
    fn as_option(&self) -> Option<&Self>;
}
impl AsOption for bool {
    fn as_option(&self) -> Option<&Self> {
        if *self {
            Some(self)
        } else {
            None
        }
    }
}

impl num::traits::ToPrimitive for FixInteger {
    fn to_i64(&self) -> Option<i64> {
        Some(self.value as i64)
    }
    fn to_u64(&self) -> Option<u64> {
        Some(self.value as u64)
    }
    fn to_f64(&self) -> Option<f64> {
        Some(self.value as f64)
    }
}
impl num::traits::ToPrimitive for BigInteger {
    fn to_i64(&self) -> Option<i64> {
        self.value.to_i64()
    }
    fn to_u64(&self) -> Option<u64> {
        self.value.to_u64()
    }
    fn to_f64(&self) -> Option<f64> {
        self.value.to_f64()
    }
}
impl num::traits::ToPrimitive for Float {
    fn to_i64(&self) -> Option<i64> {
        None
    }
    fn to_u64(&self) -> Option<u64> {
        None
    }
    fn to_f64(&self) -> Option<f64> {
        Some(self.value)
    }
}
impl num::traits::ToPrimitive for Term {
    fn to_i64(&self) -> Option<i64> {
        match *self {
            Term::FixInteger(ref x) => x.to_i64(),
            Term::BigInteger(ref x) => x.to_i64(),
            _ => None,
        }
    }
    fn to_u64(&self) -> Option<u64> {
        match *self {
            Term::FixInteger(ref x) => x.to_u64(),
            Term::BigInteger(ref x) => x.to_u64(),
            _ => None,
        }

    }
    fn to_f64(&self) -> Option<f64> {
        match *self {
            Term::FixInteger(ref x) => x.to_f64(),
            Term::BigInteger(ref x) => x.to_f64(),
            Term::Float(ref x) => x.to_f64(),
            _ => None,
        }

    }
}

impl num::bigint::ToBigInt for FixInteger {
    fn to_bigint(&self) -> Option<num::bigint::BigInt> {
        Some(BigInteger::from(self).value)
    }
}
impl num::bigint::ToBigInt for BigInteger {
    fn to_bigint(&self) -> Option<num::bigint::BigInt> {
        Some(self.value.clone())
    }
}
impl num::bigint::ToBigInt for Term {
    fn to_bigint(&self) -> Option<num::bigint::BigInt> {
        match *self {
            Term::FixInteger(ref x) => x.to_bigint(),
            Term::BigInteger(ref x) => x.to_bigint(),
            _ => None,
        }
    }
}

impl num::bigint::ToBigUint for FixInteger {
    fn to_biguint(&self) -> Option<num::bigint::BigUint> {
        BigInteger::from(self).value.to_biguint()
    }
}
impl num::bigint::ToBigUint for BigInteger {
    fn to_biguint(&self) -> Option<num::bigint::BigUint> {
        self.value.to_biguint()
    }
}
impl num::bigint::ToBigUint for Term {
    fn to_biguint(&self) -> Option<num::bigint::BigUint> {
        match *self {
            Term::FixInteger(ref x) => x.to_biguint(),
            Term::BigInteger(ref x) => x.to_biguint(),
            _ => None,
        }
    }
}