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
use types::{Array, Str}; use value::{FromValue, ToValue, Value}; macro_rules! value_i { ($t:ty) => { impl ToValue for $t { fn to_value(&self) -> $crate::Value { $crate::Value::i64(self.clone() as i64) } } impl FromValue for $t { fn from_value(v: $crate::Value) -> $t { v.i64_val() as $t } } }; ($($t:ty),*) => { $(value_i!($t);)* } } macro_rules! value_f { ($t:ty) => { impl ToValue for $t { fn to_value(&self) -> $crate::Value { $crate::Value::f64(self.clone().into()) } } impl FromValue for $t { fn from_value(v: $crate::Value) -> $t { v.f64_val() as $t } } }; ($($t:ty),*) => { $(value_f!($t);)* } } value_i!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize); value_f!(f32, f64); impl ToValue for String { fn to_value(&self) -> Value { let s = Str::from(self.as_str()); Value::from(s) } } impl FromValue for String { fn from_value(v: Value) -> String { let s = Str::from(v); String::from(s.as_str()) } } impl<V: ToValue> ToValue for Vec<V> { fn to_value(&self) -> Value { let tmp: Vec<Value> = self.iter().map(|x| x.to_value()).collect(); Array::from(tmp).into() } } impl<V: FromValue> FromValue for Vec<V> { fn from_value(v: Value) -> Vec<V> { let arr = Array::from(v); let mut dst = Vec::with_capacity(arr.len()); for i in 0..arr.len() { dst.push(V::from_value(arr.get(i).unwrap())) } dst } }