simd_json/value/borrowed/
from.rs

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