Crate zvariant

Source
Expand description

§zvariant

This crate provides API for encoding/decoding of data to/from D-Bus wire format. This binary wire format is simple and very efficient and hence useful outside of D-Bus context as well. A modified form of this format, GVariant is very commonly used for efficient storage of arbitrary data and is also supported by this crate.

Since version 2.0, the API is serde-based and hence you’ll find it very intuitive if you’re already familiar with serde. If you’re not familiar with serde, you may want to first read its tutorial before learning further about this crate.

Status: Stable.

§Example code

Serialization and deserialization is achieved through the toplevel functions:

use std::collections::HashMap;
use zvariant::{EncodingContext as Context, from_slice, to_bytes, Type};
use serde::{Deserialize, Serialize};
use byteorder::LE;

// All serialization and deserialization API, needs a context.
let ctxt = Context::<LE>::new_dbus(0);
// You can also use the more efficient GVariant format:
// let ctxt = Context::<LE>::new_gvariant(0);

// i16
let encoded = to_bytes(ctxt, &42i16).unwrap();
let decoded: i16 = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, 42);

// strings
let encoded = to_bytes(ctxt, &"hello").unwrap();
let decoded: &str = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, "hello");

// tuples
let t = ("hello", 42i32, true);
let encoded = to_bytes(ctxt, &t).unwrap();
let decoded: (&str, i32, bool) = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, t);

// Vec
let v = vec!["hello", "world!"];
let encoded = to_bytes(ctxt, &v).unwrap();
let decoded: Vec<&str> = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, v);

// Dictionary
let mut map: HashMap<i64, &str> = HashMap::new();
map.insert(1, "123");
map.insert(2, "456");
let encoded = to_bytes(ctxt, &map).unwrap();
let decoded: HashMap<i64, &str> = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded[&1], "123");
assert_eq!(decoded[&2], "456");

// derive macros to handle custom types.
#[derive(Deserialize, Serialize, Type, PartialEq, Debug)]
struct Struct<'s> {
    field1: u16,
    field2: i64,
    field3: &'s str,
}

assert_eq!(Struct::signature(), "(qxs)");
let s = Struct {
    field1: 42,
    field2: i64::max_value(),
    field3: "hello",
};
let ctxt = Context::<LE>::new_dbus(0);
let encoded = to_bytes(ctxt, &s).unwrap();
let decoded: Struct = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, s);

// It can handle enums too, just that all variants must have the same number and types of fields.
// Names of fields don't matter though. You can make use of `Value` or `OwnedValue` if you want to
// encode different data in different fields.
#[derive(Deserialize, Serialize, Type, PartialEq, Debug)]
enum Enum<'s> {
    Variant1 { field1: u16, field2: i64, field3: &'s str },
    Variant2(u16, i64, &'s str),
    Variant3 { f1: u16, f2: i64, f3: &'s str },
}

// Enum encoding uses a `u32` to denote the variant index. For unit-type enums that's all that's
// needed so the signature is just `u` but complex enums are encoded as a structure whose first
// field is the variant index and the second one is the field(s).
assert_eq!(Enum::signature(), "(u(qxs))");
let e = Enum::Variant3 {
    f1: 42,
    f2: i64::max_value(),
    f3: "hello",
};
let encoded = to_bytes(ctxt, &e).unwrap();
let decoded: Enum = from_slice(&encoded, ctxt).unwrap();
assert_eq!(decoded, e);

#[derive(Deserialize, Serialize, Type, PartialEq, Debug)]
// W/o `repr` spec, `u32` is assumed.
#[repr(u8)]
enum UnitEnum {
    Variant1,
    Variant2,
    Variant3,
}

assert_eq!(UnitEnum::signature(), "y");
let encoded = to_bytes(ctxt, &UnitEnum::Variant2).unwrap();
let e: UnitEnum = from_slice(&encoded, ctxt).unwrap();
assert_eq!(e, UnitEnum::Variant2);

// Unit enums can also be (de)serialized as strings.
#[derive(Deserialize, Serialize, Type, PartialEq, Debug)]
#[zvariant(signature = "s")]
enum StrEnum {
    Variant1,
    Variant2,
    Variant3,
}

assert_eq!(StrEnum::signature(), "s");

Apart from the obvious requirement of EncodingContext instance by the main serialization and deserialization API, the type being serialized or deserialized must also implement Type trait in addition to Serialize or Deserialize, respectively. Please refer to Type module documentation for more details.

Most of the basic types of D-Bus match 1-1 with all the primitive Rust types. The only two exceptions being, Signature and ObjectPath, which are really just strings. These types are covered by the Basic trait.

Similarly, most of the container types also map nicely to the usual Rust types and collections (as can be seen in the example code above). The only note worthy exception being ARRAY type. As arrays in Rust are fixed-sized, serde treats them as tuples and so does this crate. This means they are encoded as STRUCT type of D-Bus. If you need to serialize to, or deserialize from a D-Bus array, you’ll need to use a slice (array can easily be converted to a slice), a Vec or an arrayvec::ArrayVec.

D-Bus string types, including Signature and ObjectPath, require one additional restriction that strings in Rust do not. They must not contain any interior null bytes ('\0'). Encoding/Decoding strings that contain this character will return an error.

The generic D-Bus type, VARIANT is represented by Value, an enum that holds exactly one value of any of the other types. Please refer to Value module documentation for examples.

§no-std

While std is currently a hard requirement, optional no-std support is planned in the future. On the other hand, noalloc support is not planned as it will be extremely difficult to accomplish. However, community contribution can change that. 😊

§Optional features

FeatureDescription
arrayvecImplement Type for arrayvec::ArrayVec and arrayvec::ArrayString
enumflags2Implement Type for enumflags2::BitFlags<F>

Modules§

dbus

Structs§

Array
A helper type to wrap arrays in a Value.
ArraySeed
Use this to deserialize an Array.
DeserializeValue
A wrapper to deserialize a value to T: Type + Deserialize.
Dict
A helper type to wrap dictionaries in a Value.
EncodingContext
The encoding context to use with the serialization and deserialization API.
Fd
A RawFd wrapper.
ObjectPath
String that identifies objects at a given destination on the D-Bus bus.
Optional
An optional value.
OwnedFd
An owned RawFd wrapper.
OwnedObjectPath
Owned ObjectPath
OwnedSignature
Owned Signature
OwnedValue
Owned Value
SerializeValue
A wrapper to serialize T: Type + Serialize as a value.
Signature
String that identifies the type of an encoded value.
Str
A string wrapper.
Structure
A helper type to wrap structs in Value.
StructureBuilder
Use this to efficiently build a Structure.
StructureSeed
Use this to deserialize a Structure.

Enums§

Deserializer
Our deserialization implementation.
EncodingFormat
The encoding format.
Error
Error type used by zvariant API.
MaxDepthExceeded
Enum representing the max depth exceeded error.
Serializer
Our serialization implementation.
Value
A generic container, in the form of an enum that holds exactly one value of any of the other types.

Constants§

ARRAY_SIGNATURE_CHAR
The prefix of ARRAY type signature, as a character. Provided for manual signature creation.
ARRAY_SIGNATURE_STR
The prefix of ARRAY type signature, as a string. Provided for manual signature creation.
DICT_ENTRY_SIG_END_CHAR
The closing character of DICT_ENTRY type signature. Provided for manual signature creation.
DICT_ENTRY_SIG_END_STR
The closing character of DICT_ENTRY type signature, as a string. Provided for manual signature creation.
DICT_ENTRY_SIG_START_CHAR
The opening character of DICT_ENTRY type signature. Provided for manual signature creation.
DICT_ENTRY_SIG_START_STR
The opening character of DICT_ENTRY type signature, as a string. Provided for manual signature creation.
STRUCT_SIG_END_CHAR
The closing character of STRUCT type signature. Provided for manual signature creation.
STRUCT_SIG_END_STR
The closing character of STRUCT type signature, as a string. Provided for manual signature creation.
STRUCT_SIG_START_CHAR
The opening character of STRUCT type signature. Provided for manual signature creation.
STRUCT_SIG_START_STR
The opening character of STRUCT type signature, as a string. Provided for manual signature creation.
VARIANT_SIGNATURE_CHAR
The VARIANT type signature. Provided for manual signature creation.
VARIANT_SIGNATURE_STR
The VARIANT type signature, as a string. Provided for manual signature creation.

Traits§

Basic
Trait for basic types.
DynamicDeserialize
Types that deserialize based on dynamic signatures.
DynamicType
Types with dynamic signatures.
NoneValue
Type that uses a special value to be used as none.
Type
Trait implemented by all serializable types.

Functions§

from_slice
Deserialize T from a given slice of bytes.
from_slice_fds
Deserialize T from a given slice of bytes, containing file descriptor indices.
from_slice_fds_for_dynamic_signature
Deserialize T from a given slice of bytes containing file descriptor indices, with the given signature.
from_slice_fds_for_signature
Deserialize T from a given slice of bytes containing file descriptor indices, with the given signature.
from_slice_fds_with_seed
Deserialize T from a given slice of bytes containing file descriptor indices, using the given seed.
from_slice_for_dynamic_signature
Deserialize T from a given slice of bytes containing file descriptor indices, with the given signature.
from_slice_for_signature
Deserialize T from a given slice of bytes with the given signature.
from_slice_with_seed
Deserialize T from a given slice of bytes containing file descriptor indices, using the given seed.
serialized_size
Calculate the serialized size of T.
serialized_size_fds
Calculate the serialized size of T that (potentially) contains FDs.
to_bytes
Serialize T as a byte vector.
to_bytes_fds
Serialize T that (potentially) contains FDs, as a byte vector.
to_bytes_fds_for_signature
Serialize T that (potentially) contains FDs and has the given signature, to a new byte vector.
to_bytes_for_signature
Serialize T that has the given signature, to a new byte vector.
to_writer
Serialize T to the given writer.
to_writer_fds
Serialize T that (potentially) contains FDs, to the given writer.
to_writer_fds_for_signature
Serialize T that (potentially) contains FDs and has the given signature, to the given writer.
to_writer_for_signature
Serialize T that has the given signature, to the given writer.

Type Aliases§

Result
Alias for a Result with the error type zvariant::Error.

Derive Macros§

DeserializeDict
Adds Deserialize implementation to structs to be deserialized from a{sv} type.
OwnedValue
Implements conversions for your type to/from OwnedValue.
SerializeDict
Adds Serialize implementation to structs to be serialized as a{sv} type.
Type
Derive macro to add Type implementation to structs and enums.
TypeDict
Derive macro to add Type implementation to structs serialized as a{sv} type.
Value
Implements conversions for your type to/from Value.