zvariant/
into_value.rs

1use std::{borrow::Cow, collections::HashMap, hash::BuildHasher, sync::Arc};
2
3#[cfg(feature = "gvariant")]
4use crate::Maybe;
5use crate::{Array, Dict, NoneValue, ObjectPath, Optional, 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}
22
23macro_rules! into_value_from_ref {
24    ($from:ty, $kind:ident) => {
25        impl<'a> From<&'a $from> for Value<'a> {
26            fn from(v: &'a $from) -> Self {
27                Value::$kind(v.clone().into())
28            }
29        }
30    };
31}
32
33macro_rules! into_value_from_both {
34    ($from:ty, $kind:ident) => {
35        into_value!($from, $kind);
36        into_value_from_ref!($from, $kind);
37    };
38}
39
40into_value_from_both!(u8, U8);
41into_value_from_both!(i8, I16);
42into_value_from_both!(bool, Bool);
43into_value_from_both!(u16, U16);
44into_value_from_both!(i16, I16);
45into_value_from_both!(u32, U32);
46into_value_from_both!(i32, I32);
47into_value_from_both!(u64, U64);
48into_value_from_both!(i64, I64);
49into_value_from_both!(f32, F64);
50into_value_from_both!(f64, F64);
51
52into_value!(Arc<str>, Str);
53into_value!(Cow<'a, str>, Str);
54into_value_from_both!(String, Str);
55
56into_value_from_both!(&'a str, Str);
57into_value_from_both!(Str<'a>, Str);
58into_value_from_both!(ObjectPath<'a>, ObjectPath);
59
60macro_rules! try_into_value_from_ref {
61    ($from:ty, $kind:ident) => {
62        impl<'a> TryFrom<&'a $from> for Value<'a> {
63            type Error = crate::Error;
64
65            fn try_from(v: &'a $from) -> crate::Result<Self> {
66                v.try_clone().map(Value::$kind)
67            }
68        }
69    };
70}
71
72into_value!(Array<'a>, Array);
73try_into_value_from_ref!(Array<'a>, Array);
74into_value!(Dict<'a, 'a>, Dict);
75try_into_value_from_ref!(Dict<'a, 'a>, Dict);
76#[cfg(feature = "gvariant")]
77into_value!(Maybe<'a>, Maybe);
78#[cfg(feature = "gvariant")]
79try_into_value_from_ref!(Maybe<'a>, Maybe);
80#[cfg(unix)]
81into_value!(Fd<'a>, Fd);
82#[cfg(unix)]
83try_into_value_from_ref!(Fd<'a>, Fd);
84
85impl<'v, 's: 'v, T> From<T> for Value<'v>
86where
87    T: Into<Structure<'s>>,
88{
89    fn from(v: T) -> Value<'v> {
90        Value::Structure(v.into())
91    }
92}
93
94impl<'b, 'v, V> From<&'b [V]> for Value<'v>
95where
96    &'b [V]: Into<Array<'v>>,
97{
98    fn from(v: &'b [V]) -> Value<'v> {
99        Value::Array(v.into())
100    }
101}
102
103impl<'v, V> From<Vec<V>> for Value<'v>
104where
105    Vec<V>: Into<Array<'v>>,
106{
107    fn from(v: Vec<V>) -> Value<'v> {
108        Value::Array(v.into())
109    }
110}
111
112impl<'b, 'v, V> From<&'b Vec<V>> for Value<'v>
113where
114    &'b Vec<V>: Into<Array<'v>>,
115{
116    fn from(v: &'b Vec<V>) -> Value<'v> {
117        Value::Array(v.into())
118    }
119}
120
121impl<'a, 'k, 'v, K, V, H> From<HashMap<K, V, H>> for Value<'a>
122where
123    'k: 'a,
124    'v: 'a,
125    K: Type + Into<Value<'k>> + std::hash::Hash + std::cmp::Eq,
126    V: Type + Into<Value<'v>>,
127    H: BuildHasher + Default,
128{
129    fn from(value: HashMap<K, V, H>) -> Self {
130        Self::Dict(value.into())
131    }
132}
133
134impl<'v, V> From<Optional<V>> for Value<'v>
135where
136    V: Into<Value<'v>> + NoneValue<NoneType = V>,
137{
138    fn from(v: Optional<V>) -> Value<'v> {
139        Option::<V>::from(v)
140            .unwrap_or_else(|| V::null_value())
141            .into()
142    }
143}
144
145#[cfg(all(feature = "gvariant", not(feature = "option-as-array")))]
146impl<'v, V> From<Option<V>> for Value<'v>
147where
148    Option<V>: Into<Maybe<'v>>,
149{
150    fn from(v: Option<V>) -> Value<'v> {
151        Value::Maybe(v.into())
152    }
153}
154
155#[cfg(feature = "option-as-array")]
156impl<'v, V> From<Option<V>> for Value<'v>
157where
158    V: Into<Value<'v>> + Type,
159{
160    fn from(v: Option<V>) -> Value<'v> {
161        let mut array = Array::new(V::SIGNATURE);
162        if let Some(v) = v {
163            // We got the signature from the `Type` impl, so this should never panic.
164            array.append(v.into()).expect("signature mismatch");
165        }
166
167        array.into()
168    }
169}