zvariant/as_value/
serialize.rs

1use serde::ser::{SerializeStruct, Serializer};
2
3use crate::Type;
4
5/// A wrapper to serialize `T: Type + serde::Serialize` as a value.
6///
7/// When the type of a value is well-known, you may avoid the cost and complexity of wrapping to a
8/// generic [`Value`] and instead use this wrapper.
9///
10/// ```
11/// # use zvariant::{to_bytes, serialized::Context, as_value::Serialize, LE};
12/// #
13/// # let ctxt = Context::new_dbus(LE, 0);
14/// let _ = to_bytes(ctxt, &Serialize(&[0, 1, 2])).unwrap();
15/// ```
16///
17/// [`Value`]: enum.Value.html
18pub struct Serialize<'a, T: Type + serde::Serialize>(pub &'a T);
19
20impl<T: Type + serde::Serialize> serde::Serialize for Serialize<'_, T> {
21    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
22    where
23        S: Serializer,
24    {
25        // Serializer implementation needs to ensure padding isn't added for Value.
26        let mut structure = serializer.serialize_struct("Variant", 2)?;
27
28        structure.serialize_field("signature", T::SIGNATURE)?;
29        structure.serialize_field("value", self.0)?;
30
31        structure.end()
32    }
33}
34
35impl<T: Type + serde::Serialize> Type for Serialize<'_, T> {
36    const SIGNATURE: &'static crate::Signature = &crate::Signature::Variant;
37}
38
39/// Serialize a value as a [`enum@zvariant::Value`].
40pub fn serialize<T, S>(value: &T, ser: S) -> std::result::Result<S::Ok, S::Error>
41where
42    S: Serializer,
43    T: Type + serde::Serialize,
44{
45    use serde::Serialize as _;
46
47    Serialize(value).serialize(ser)
48}
49
50/// Serialize an optional value as a [`enum@zvariant::Value`].
51pub fn serialize_optional<T, S>(value: &Option<T>, ser: S) -> std::result::Result<S::Ok, S::Error>
52where
53    S: Serializer,
54    T: Type + serde::Serialize,
55{
56    super::serialize(value.as_ref().unwrap(), ser)
57}