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§
Constants§
- OPTIONS
- The default options for the packed encoding.
Functions§
- decode
- Decode the given type
Tfrom the givenReaderusing the defaultEncoding. - encode
- Encode the given value to the given
Writerusing the defaultEncoding. - from_
slice - Decode the given type
Tfrom the given slice using the defaultEncoding. - 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
Encodingand return the number of bytes encoded. - to_vec
- Encode the given value to a
Vecusing the defaultEncoding. - to_
writer - Encode the given value to the given
Writeusing the defaultEncoding.
Type Aliases§
- Result
- Convenient result alias for use with
musli::storage.