Skip to main content

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 [`enum@crate::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/// ```
16pub struct Serialize<'a, T: Type + serde::Serialize>(pub &'a T);
17
18impl<T: Type + serde::Serialize> serde::Serialize for Serialize<'_, T> {
19    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
20    where
21        S: Serializer,
22    {
23        // Serializer implementation needs to ensure padding isn't added for Value.
24        let mut structure = serializer.serialize_struct("Variant", 2)?;
25
26        structure.serialize_field("signature", T::SIGNATURE)?;
27        structure.serialize_field("value", self.0)?;
28
29        structure.end()
30    }
31}
32
33impl<T: Type + serde::Serialize> Type for Serialize<'_, T> {
34    const SIGNATURE: &'static crate::Signature = &crate::Signature::Variant;
35}
36
37/// Serialize a value as a [`enum@zvariant::Value`].
38pub fn serialize<T, S>(value: &T, ser: S) -> std::result::Result<S::Ok, S::Error>
39where
40    S: Serializer,
41    T: Type + serde::Serialize,
42{
43    use serde::Serialize as _;
44
45    Serialize(value).serialize(ser)
46}
47
48/// Serialize an optional value as a [`enum@zvariant::Value`].
49pub fn serialize_optional<T, S>(value: &Option<T>, ser: S) -> std::result::Result<S::Ok, S::Error>
50where
51    S: Serializer,
52    T: Type + serde::Serialize,
53{
54    super::serialize(value.as_ref().unwrap(), ser)
55}