simd_json/value/owned/
from.rs1use super::{Object, Value};
2use crate::{BorrowedValue, StaticNode};
3
4impl From<crate::BorrowedValue<'_>> for Value {
5 #[cfg_attr(not(feature = "no-inline"), inline)]
6 fn from(b: BorrowedValue<'_>) -> Self {
7 match b {
8 BorrowedValue::Static(s) => Self::from(s),
9 BorrowedValue::String(s) => Self::from(s.to_string()),
10 BorrowedValue::Array(a) => a.into_iter().collect(),
11 BorrowedValue::Object(m) => m.into_iter().collect(),
12 }
13 }
14}
15
16impl<T> From<Option<T>> for Value
17where
18 Value: From<T>,
19{
20 #[cfg_attr(not(feature = "no-inline"), inline)]
21 fn from(s: Option<T>) -> Self {
22 s.map_or(Value::Static(StaticNode::Null), Value::from)
23 }
24}
25
26impl From<StaticNode> for Value {
27 #[cfg_attr(not(feature = "no-inline"), inline)]
28 fn from(s: StaticNode) -> Self {
29 Self::Static(s)
30 }
31}
32impl From<&str> for Value {
35 #[cfg_attr(not(feature = "no-inline"), inline)]
36 fn from(s: &str) -> Self {
37 Self::String(s.to_owned())
38 }
39}
40
41impl<'value> From<std::borrow::Cow<'value, str>> for Value {
42 #[cfg_attr(not(feature = "no-inline"), inline)]
43 fn from(c: std::borrow::Cow<'value, str>) -> Self {
44 Self::String(c.to_string())
45 }
46}
47
48#[cfg(feature = "beef")]
49impl<'value> From<beef::lean::Cow<'value, str>> for Value {
50 #[cfg_attr(not(feature = "no-inline"), inline)]
51 fn from(c: beef::lean::Cow<'value, str>) -> Self {
52 Self::String(c.to_string())
53 }
54}
55
56impl From<String> for Value {
57 #[cfg_attr(not(feature = "no-inline"), inline)]
58 fn from(s: String) -> Self {
59 Self::String(s)
60 }
61}
62
63impl From<&String> for Value {
64 #[cfg_attr(not(feature = "no-inline"), inline)]
65 fn from(s: &String) -> Self {
66 Self::String(s.clone())
67 }
68}
69
70impl From<bool> for Value {
73 #[cfg_attr(not(feature = "no-inline"), inline)]
74 fn from(b: bool) -> Self {
75 Self::Static(StaticNode::Bool(b))
76 }
77}
78
79impl From<()> for Value {
80 #[cfg_attr(not(feature = "no-inline"), inline)]
81 fn from(_b: ()) -> Self {
82 Self::Static(StaticNode::Null)
83 }
84}
85
86impl From<i8> for Value {
88 #[cfg_attr(not(feature = "no-inline"), inline)]
89 fn from(i: i8) -> Self {
90 Self::Static(StaticNode::I64(i64::from(i)))
91 }
92}
93
94impl From<i16> for Value {
95 #[cfg_attr(not(feature = "no-inline"), inline)]
96 fn from(i: i16) -> Self {
97 Self::Static(StaticNode::I64(i64::from(i)))
98 }
99}
100
101impl From<i32> for Value {
102 #[cfg_attr(not(feature = "no-inline"), inline)]
103 fn from(i: i32) -> Self {
104 Self::Static(StaticNode::I64(i64::from(i)))
105 }
106}
107
108impl From<i64> for Value {
109 #[cfg_attr(not(feature = "no-inline"), inline)]
110 fn from(i: i64) -> Self {
111 Self::Static(StaticNode::I64(i))
112 }
113}
114#[cfg(feature = "128bit")]
115impl From<i128> for Value {
116 #[cfg_attr(not(feature = "no-inline"), inline)]
117 fn from(i: i128) -> Self {
118 Self::Static(StaticNode::I128(i))
119 }
120}
121
122impl From<u8> for Value {
124 #[cfg_attr(not(feature = "no-inline"), inline)]
125 fn from(i: u8) -> Self {
126 Self::Static(StaticNode::U64(u64::from(i)))
127 }
128}
129
130impl From<u16> for Value {
131 #[cfg_attr(not(feature = "no-inline"), inline)]
132 fn from(i: u16) -> Self {
133 Self::Static(StaticNode::U64(u64::from(i)))
134 }
135}
136
137impl From<u32> for Value {
138 #[cfg_attr(not(feature = "no-inline"), inline)]
139 fn from(i: u32) -> Self {
140 Self::Static(StaticNode::U64(u64::from(i)))
141 }
142}
143
144impl From<u64> for Value {
145 #[cfg_attr(not(feature = "no-inline"), inline)]
146 #[allow(clippy::cast_possible_wrap)]
147 fn from(i: u64) -> Self {
148 Self::Static(StaticNode::U64(i))
149 }
150}
151
152#[cfg(feature = "128bit")]
153impl From<u128> for Value {
154 #[cfg_attr(not(feature = "no-inline"), inline)]
155 fn from(i: u128) -> Self {
156 Self::Static(StaticNode::U128(i))
157 }
158}
159
160impl From<usize> for Value {
161 #[cfg_attr(not(feature = "no-inline"), inline)]
162 fn from(i: usize) -> Self {
163 Self::Static(StaticNode::U64(i as u64))
164 }
165}
166
167impl From<f32> for Value {
169 #[cfg_attr(not(feature = "no-inline"), inline)]
170 fn from(f: f32) -> Self {
171 Self::Static(StaticNode::from(f64::from(f)))
172 }
173}
174
175impl From<f64> for Value {
176 #[cfg_attr(not(feature = "no-inline"), inline)]
177 fn from(f: f64) -> Self {
178 Self::Static(StaticNode::from(f))
179 }
180}
181
182impl<S> From<Vec<S>> for Value
183where
184 Value: From<S>,
185{
186 #[cfg_attr(not(feature = "no-inline"), inline)]
187 fn from(v: Vec<S>) -> Self {
188 v.into_iter().collect()
189 }
190}
191
192impl<V: Into<Value>> FromIterator<V> for Value {
193 #[cfg_attr(not(feature = "no-inline"), inline)]
194 fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
195 Self::Array(Box::new(iter.into_iter().map(Into::into).collect()))
196 }
197}
198
199impl<K: ToString, V: Into<Value>> FromIterator<(K, V)> for Value {
200 #[cfg_attr(not(feature = "no-inline"), inline)]
201 fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
202 Self::Object(Box::new(
203 iter.into_iter()
204 .map(|(k, v)| (k.to_string(), Into::into(v)))
205 .collect(),
206 ))
207 }
208}
209
210impl From<Object> for Value {
211 #[cfg_attr(not(feature = "no-inline"), inline)]
212 fn from(v: Object) -> Self {
213 Self::Object(Box::new(v))
214 }
215}
216
217impl From<std::collections::HashMap<String, Value>> for Value {
218 #[cfg_attr(not(feature = "no-inline"), inline)]
219 fn from(v: std::collections::HashMap<String, Self>) -> Self {
220 Self::from(v.into_iter().collect::<Object>())
221 }
222}