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