1 | use std::{collections::HashMap, hash::BuildHasher}; |
2 | |
3 | #[cfg (feature = "gvariant" )] |
4 | use crate::Maybe; |
5 | use crate::{Array, Dict, ObjectPath, Signature, Str, Structure, Type, Value}; |
6 | |
7 | #[cfg (unix)] |
8 | use crate::Fd; |
9 | |
10 | // |
11 | // Conversions from encodable types to `Value` |
12 | |
13 | macro_rules! into_value { |
14 | ($from:ty, $kind:ident) => { |
15 | impl<'a> From<$from> for Value<'a> { |
16 | fn from(v: $from) -> Self { |
17 | Value::$kind(v.into()) |
18 | } |
19 | } |
20 | |
21 | impl<'a> From<&'a $from> for Value<'a> { |
22 | fn from(v: &'a $from) -> Self { |
23 | Value::from(v.clone()) |
24 | } |
25 | } |
26 | }; |
27 | } |
28 | |
29 | into_value!(u8, U8); |
30 | into_value!(i8, I16); |
31 | into_value!(bool, Bool); |
32 | into_value!(u16, U16); |
33 | into_value!(i16, I16); |
34 | into_value!(u32, U32); |
35 | into_value!(i32, I32); |
36 | into_value!(u64, U64); |
37 | into_value!(i64, I64); |
38 | into_value!(f32, F64); |
39 | into_value!(f64, F64); |
40 | #[cfg (unix)] |
41 | into_value!(Fd, Fd); |
42 | |
43 | into_value!(&'a str, Str); |
44 | into_value!(Str<'a>, Str); |
45 | into_value!(Signature<'a>, Signature); |
46 | into_value!(ObjectPath<'a>, ObjectPath); |
47 | into_value!(Array<'a>, Array); |
48 | into_value!(Dict<'a, 'a>, Dict); |
49 | #[cfg (feature = "gvariant" )] |
50 | into_value!(Maybe<'a>, Maybe); |
51 | |
52 | impl From<String> for Value<'static> { |
53 | fn from(v: String) -> Self { |
54 | Value::Str(crate::Str::from(v)) |
55 | } |
56 | } |
57 | |
58 | impl<'v, 's: 'v, T> From<T> for Value<'v> |
59 | where |
60 | T: Into<Structure<'s>>, |
61 | { |
62 | fn from(v: T) -> Value<'v> { |
63 | Value::Structure(v.into()) |
64 | } |
65 | } |
66 | |
67 | impl<'v, V> From<&'v [V]> for Value<'v> |
68 | where |
69 | &'v [V]: Into<Array<'v>>, |
70 | { |
71 | fn from(v: &'v [V]) -> Value<'v> { |
72 | Value::Array(v.into()) |
73 | } |
74 | } |
75 | |
76 | impl<'v, V> From<Vec<V>> for Value<'v> |
77 | where |
78 | Vec<V>: Into<Array<'v>>, |
79 | { |
80 | fn from(v: Vec<V>) -> Value<'v> { |
81 | Value::Array(v.into()) |
82 | } |
83 | } |
84 | |
85 | impl<'v, V> From<&'v Vec<V>> for Value<'v> |
86 | where |
87 | &'v Vec<V>: Into<Array<'v>>, |
88 | { |
89 | fn from(v: &'v Vec<V>) -> Value<'v> { |
90 | Value::Array(v.into()) |
91 | } |
92 | } |
93 | |
94 | impl<'a, 'k, 'v, K, V, H> From<HashMap<K, V, H>> for Value<'a> |
95 | where |
96 | 'k: 'a, |
97 | 'v: 'a, |
98 | K: Type + Into<Value<'k>> + std::hash::Hash + std::cmp::Eq, |
99 | V: Type + Into<Value<'v>>, |
100 | H: BuildHasher + Default, |
101 | { |
102 | fn from(value: HashMap<K, V, H>) -> Self { |
103 | Self::Dict(value.into()) |
104 | } |
105 | } |
106 | |
107 | impl<'v> From<&'v String> for Value<'v> { |
108 | fn from(v: &'v String) -> Value<'v> { |
109 | Value::Str(v.into()) |
110 | } |
111 | } |
112 | |
113 | #[cfg (feature = "gvariant" )] |
114 | impl<'v, V> From<Option<V>> for Value<'v> |
115 | where |
116 | Option<V>: Into<Maybe<'v>>, |
117 | { |
118 | fn from(v: Option<V>) -> Value<'v> { |
119 | Value::Maybe(v.into()) |
120 | } |
121 | } |
122 | |