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
use crate::raw_types;
use crate::runtime::Runtime;
use crate::string;
use crate::List;
use crate::Value;
use std::collections::HashMap;
use std::convert::TryFrom;
impl From<f32> for Value {
fn from(num: f32) -> Self {
unsafe {
Value::new(
raw_types::values::ValueTag::Number,
raw_types::values::ValueData { number: num },
)
}
}
}
impl From<i32> for Value {
fn from(num: i32) -> Self {
unsafe {
Value::new(
raw_types::values::ValueTag::Number,
raw_types::values::ValueData { number: num as f32 },
)
}
}
}
impl From<u32> for Value {
fn from(num: u32) -> Self {
unsafe {
Value::new(
raw_types::values::ValueTag::Number,
raw_types::values::ValueData { number: num as f32 },
)
}
}
}
impl From<bool> for Value {
fn from(b: bool) -> Self {
unsafe {
Value::new(
raw_types::values::ValueTag::Number,
raw_types::values::ValueData {
number: if b { 1.0 } else { 0.0 },
},
)
}
}
}
impl From<&Value> for Value {
fn from(val: &Value) -> Self {
val.to_owned()
}
}
impl<T: Into<Value> + Clone> TryFrom<&HashMap<String, T>> for Value {
type Error = Runtime;
fn try_from(hashmap: &HashMap<String, T>) -> Result<Self, Self::Error> {
let res = List::new();
for (k, v) in hashmap {
let string = string::StringRef::new(k)?;
res.set(string, v.clone())?;
}
Ok(res.into())
}
}
impl<A: Into<Value> + Clone, B: Into<Value> + Clone> TryFrom<&HashMap<A, B>> for Value {
type Error = Runtime;
fn try_from(hashmap: &HashMap<A, B>) -> Result<Self, Self::Error> {
let res = List::new();
for (k, v) in hashmap {
res.set(k.clone(), v.clone())?;
}
Ok(res.into())
}
}
impl<T: Into<Value> + Clone> From<&Vec<T>> for Value {
fn from(vec: &Vec<T>) -> Self {
let res = List::new();
for val in vec {
res.append(val.clone());
}
res.into()
}
}