Skip to main content

zvariant/
into_value.rs

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