]>
git.proxmox.com Git - rustc.git/blob - vendor/serde_yaml/src/value/from.rs
1 use crate::{Mapping, Value}
;
3 // Implement a bunch of conversion to make it easier to create YAML values
6 macro_rules
! from_number
{
9 impl From
<$ty
> for Value
{
10 fn from(n
: $ty
) -> Self {
11 Value
::Number(n
.into())
24 impl From
<bool
> for Value
{
25 /// Convert boolean to `Value`
30 /// use serde_yaml::Value;
33 /// let x: Value = b.into();
35 fn from(f
: bool
) -> Self {
40 impl From
<String
> for Value
{
41 /// Convert `String` to `Value`
46 /// use serde_yaml::Value;
48 /// let s: String = "lorem".to_string();
49 /// let x: Value = s.into();
51 fn from(f
: String
) -> Self {
56 impl<'a
> From
<&'a
str> for Value
{
57 /// Convert string slice to `Value`
62 /// use serde_yaml::Value;
64 /// let s: &str = "lorem";
65 /// let x: Value = s.into();
67 fn from(f
: &str) -> Self {
68 Value
::String(f
.to_string())
74 impl<'a
> From
<Cow
<'a
, str>> for Value
{
75 /// Convert copy-on-write string to `Value`
80 /// use serde_yaml::Value;
81 /// use std::borrow::Cow;
83 /// let s: Cow<str> = Cow::Borrowed("lorem");
84 /// let x: Value = s.into();
88 /// use serde_yaml::Value;
89 /// use std::borrow::Cow;
91 /// let s: Cow<str> = Cow::Owned("lorem".to_string());
92 /// let x: Value = s.into();
94 fn from(f
: Cow
<'a
, str>) -> Self {
95 Value
::String(f
.to_string())
99 impl From
<Mapping
> for Value
{
100 /// Convert map (with string keys) to `Value`
105 /// use serde_yaml::{Mapping, Value};
107 /// let mut m = Mapping::new();
108 /// m.insert("Lorem".into(), "ipsum".into());
109 /// let x: Value = m.into();
111 fn from(f
: Mapping
) -> Self {
116 impl<T
: Into
<Value
>> From
<Vec
<T
>> for Value
{
117 /// Convert a `Vec` to `Value`
122 /// use serde_yaml::Value;
124 /// let v = vec!["lorem", "ipsum", "dolor"];
125 /// let x: Value = v.into();
127 fn from(f
: Vec
<T
>) -> Self {
128 Value
::Sequence(f
.into_iter().map(Into
::into
).collect())
132 impl<'a
, T
: Clone
+ Into
<Value
>> From
<&'a
[T
]> for Value
{
133 /// Convert a slice to `Value`
138 /// use serde_yaml::Value;
140 /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
141 /// let x: Value = v.into();
143 fn from(f
: &'a
[T
]) -> Self {
144 Value
::Sequence(f
.iter().cloned().map(Into
::into
).collect())
148 use std
::iter
::FromIterator
;
150 impl<T
: Into
<Value
>> FromIterator
<T
> for Value
{
151 /// Convert an iteratable type to a YAML sequence
156 /// use serde_yaml::Value;
158 /// let v = std::iter::repeat(42).take(5);
159 /// let x: Value = v.collect();
163 /// use serde_yaml::Value;
165 /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
166 /// let x: Value = v.into_iter().collect();
170 /// use std::iter::FromIterator;
171 /// use serde_yaml::Value;
173 /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
175 fn from_iter
<I
: IntoIterator
<Item
= T
>>(iter
: I
) -> Self {
176 let vec
= iter
.into_iter().map(T
::into
).collect();