1use std::{collections::HashMap, hash::BuildHasher};
2
3#[cfg(feature = "gvariant")]
4use crate::Maybe;
5use crate::{Array, Dict, ObjectPath, Signature, Str, Structure, Type, Value};
6
7#[cfg(unix)]
8use crate::Fd;
9
10//
11// Conversions from encodable types to `Value`
12
13macro_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
29into_value!(u8, U8);
30into_value!(i8, I16);
31into_value!(bool, Bool);
32into_value!(u16, U16);
33into_value!(i16, I16);
34into_value!(u32, U32);
35into_value!(i32, I32);
36into_value!(u64, U64);
37into_value!(i64, I64);
38into_value!(f32, F64);
39into_value!(f64, F64);
40#[cfg(unix)]
41into_value!(Fd, Fd);
42
43into_value!(&'a str, Str);
44into_value!(Str<'a>, Str);
45into_value!(Signature<'a>, Signature);
46into_value!(ObjectPath<'a>, ObjectPath);
47into_value!(Array<'a>, Array);
48into_value!(Dict<'a, 'a>, Dict);
49#[cfg(feature = "gvariant")]
50into_value!(Maybe<'a>, Maybe);
51
52impl From<String> for Value<'static> {
53 fn from(v: String) -> Self {
54 Value::Str(crate::Str::from(v))
55 }
56}
57
58impl<'v, 's: 'v, T> From<T> for Value<'v>
59where
60 T: Into<Structure<'s>>,
61{
62 fn from(v: T) -> Value<'v> {
63 Value::Structure(v.into())
64 }
65}
66
67impl<'v, V> From<&'v [V]> for Value<'v>
68where
69 &'v [V]: Into<Array<'v>>,
70{
71 fn from(v: &'v [V]) -> Value<'v> {
72 Value::Array(v.into())
73 }
74}
75
76impl<'v, V> From<Vec<V>> for Value<'v>
77where
78 Vec<V>: Into<Array<'v>>,
79{
80 fn from(v: Vec<V>) -> Value<'v> {
81 Value::Array(v.into())
82 }
83}
84
85impl<'v, V> From<&'v Vec<V>> for Value<'v>
86where
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
94impl<'a, 'k, 'v, K, V, H> From<HashMap<K, V, H>> for Value<'a>
95where
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
107impl<'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")]
114impl<'v, V> From<Option<V>> for Value<'v>
115where
116 Option<V>: Into<Maybe<'v>>,
117{
118 fn from(v: Option<V>) -> Value<'v> {
119 Value::Maybe(v.into())
120 }
121}
122