simd_json/value/borrowed/
from.rs

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