Module packed

Module packed 

Source
Expand description

The most efficient binary storage encoding for Müsli.

The packed encoding is not upgrade safe:

  • ✗ Can not tolerate missing fields.
  • ✗ Cannot skip over extra unrecognized fields.

This means that it’s probably not suitable as a storage format, nor as a wire since it cannot allow clients to upgrade independent of each other.

In order to make full use of the packed format, the data model should use the #[musli(packed)] attribute on the container. This among other things prevents field identifiers from being emitted. See derives for more information. Since the packed format doesn’t use field identifiers, it only supports optional fields at the end of the stream.

See storage or wire or descriptive for formats which are upgrade stable.

Note that this is simply a specialization of the storage format with different options. But it allows for much more efficient encoding.

use musli::{Encode, Decode};

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(packed)]
struct Version1 {
    name: String,
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(packed)]
struct Version2 {
    name: String,
    #[musli(default)]
    age: Option<u32>,
}

let version2 = musli::packed::to_vec(&Version2 {
    name: String::from("Aristotle"),
    age: Some(61),
})?;

let version1 = musli::packed::decode::<_, Version1>(version2.as_slice())?;
assert_eq!(version1.name, "Aristotle");

let version1 = musli::packed::to_vec(&Version1 {
    name: String::from("Aristotle"),
})?;

let version2: Version2 = musli::packed::decode(version1.as_slice())?;

assert_eq!(version2, Version2 {
    name: String::from("Aristotle"),
    age: None,
});

Structs§

Encoding
Setting up encoding with parameters.
Error
Error raised during storage encoding.

Constants§

OPTIONS
The default options for the packed encoding.

Functions§

decode
Decode the given type T from the given Reader using the default Encoding.
encode
Encode the given value to the given Writer using the default Encoding.
from_slice
Decode the given type T from the given slice using the default Encoding.
to_fixed_bytes
Encode the given value to a fixed-size bytes using the default Encoding.
to_slice
Encode the given value to the given slice using the default Encoding and return the number of bytes encoded.
to_vec
Encode the given value to a Vec using the default Encoding.
to_writer
Encode the given value to the given Write using the default Encoding.

Type Aliases§

Result
Convenient result alias for use with musli::storage.