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
use std::collections::HashMap;
use crate::{Float, HashablePrimitive, HashableValue, Primitive, Value};
pub trait RustyValue {
fn into_rusty_value(self) -> Value;
}
pub trait HashableRustyValue {
fn into_hashable_rusty_value(self) -> HashableValue;
}
impl HashableRustyValue for usize {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::USize(self)))
}
}
impl HashableRustyValue for isize {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::ISize(self)))
}
}
impl HashableRustyValue for u8 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U8(self)))
}
}
impl HashableRustyValue for i8 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I8(self)))
}
}
impl HashableRustyValue for u16 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U16(self)))
}
}
impl HashableRustyValue for i16 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I16(self)))
}
}
impl HashableRustyValue for u32 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U32(self)))
}
}
impl HashableRustyValue for i32 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I32(self)))
}
}
impl HashableRustyValue for u64 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U64(self)))
}
}
impl HashableRustyValue for i64 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I64(self)))
}
}
impl HashableRustyValue for u128 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::U128(self)))
}
}
impl HashableRustyValue for i128 {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Integer(crate::Integer::I128(self)))
}
}
impl HashableRustyValue for String {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::String(self))
}
}
impl<'a> HashableRustyValue for &'a str {
fn into_hashable_rusty_value(self) -> HashableValue {
self.to_string().into_hashable_rusty_value()
}
}
impl HashableRustyValue for bool {
#[inline]
fn into_hashable_rusty_value(self) -> HashableValue {
HashableValue::Primitive(HashablePrimitive::Bool(self))
}
}
impl RustyValue for HashableValue {
fn into_rusty_value(self) -> Value {
match self {
HashableValue::Primitive(p) => match p {
HashablePrimitive::Integer(i) => Value::Primitive(Primitive::Integer(i)),
HashablePrimitive::String(s) => Value::Primitive(Primitive::String(s)),
HashablePrimitive::Char(c) => Value::Primitive(Primitive::Char(c)),
HashablePrimitive::Bool(b) => Value::Primitive(Primitive::Bool(b)),
},
HashableValue::List(l) => {
Value::List(l.into_iter().map(|v| v.into_rusty_value()).collect())
}
}
}
}
impl<H: HashableRustyValue> RustyValue for H {
#[inline]
fn into_rusty_value(self) -> Value {
self.into_hashable_rusty_value().into_rusty_value()
}
}
impl RustyValue for f32 {
#[inline]
fn into_rusty_value(self) -> Value {
Value::Primitive(Primitive::Float(Float::F32(self)))
}
}
impl RustyValue for f64 {
#[inline]
fn into_rusty_value(self) -> Value {
Value::Primitive(Primitive::Float(Float::F64(self)))
}
}
impl<R: RustyValue> RustyValue for Vec<R> {
fn into_rusty_value(self) -> Value {
let value_vec = self
.into_iter()
.map(|v| v.into_rusty_value())
.collect::<Vec<_>>();
Value::List(value_vec)
}
}
impl<R: RustyValue, H: HashableRustyValue> RustyValue for HashMap<H, R> {
fn into_rusty_value(self) -> Value {
let map = self
.into_iter()
.map(|(k, v)| (k.into_hashable_rusty_value(), v.into_rusty_value()))
.collect::<HashMap<_, _>>();
Value::Map(map)
}
}