musli/packed/
mod.rs

1//! The most efficient binary storage encoding for Müsli.
2//!
3//! The packed encoding is not upgrade safe:
4//!
5//! * ✗ Can not tolerate missing fields.
6//! * ✗ Cannot skip over extra unrecognized fields.
7//!
8//! This means that it's probably not suitable as a storage format, nor as a
9//! wire since it cannot allow clients to upgrade independent of each other.
10//!
11//! In order to make full use of the packed format, the data model should use
12//! the `#[musli(packed)]` attribute on the container. This among other things
13//! prevents field identifiers from being emitted. See [`derives`] for more
14//! information. Since the packed format doesn't use field identifiers, it only
15//! supports optional fields *at the end* of the stream.
16//!
17//! See [`storage`] or [`wire`] or [`descriptive`] for formats which are upgrade
18//! stable.
19//!
20//! Note that this is simply a specialization of the `storage` format with
21//! different options. But it allows for much more efficient encoding.
22//!
23//! ```
24//! use musli::{Encode, Decode};
25//!
26//! #[derive(Debug, PartialEq, Encode, Decode)]
27//! #[musli(packed)]
28//! struct Version1 {
29//!     name: String,
30//! }
31//!
32//! #[derive(Debug, PartialEq, Encode, Decode)]
33//! #[musli(packed)]
34//! struct Version2 {
35//!     name: String,
36//!     #[musli(default)]
37//!     age: Option<u32>,
38//! }
39//!
40//! let version2 = musli::packed::to_vec(&Version2 {
41//!     name: String::from("Aristotle"),
42//!     age: Some(61),
43//! })?;
44//!
45//! let version1 = musli::packed::decode::<_, Version1>(version2.as_slice())?;
46//! assert_eq!(version1.name, "Aristotle");
47//!
48//! let version1 = musli::packed::to_vec(&Version1 {
49//!     name: String::from("Aristotle"),
50//! })?;
51//!
52//! let version2: Version2 = musli::packed::decode(version1.as_slice())?;
53//!
54//! assert_eq!(version2, Version2 {
55//!     name: String::from("Aristotle"),
56//!     age: None,
57//! });
58//! # Ok::<_, musli::packed::Error>(())
59//! ```
60//!
61//! [`storage`]: crate::storage
62//! [`descriptive`]: crate::descriptive
63//! [`wire`]: crate::wire
64//! [`derives`]: crate::_help::derives
65
66#![cfg(any(
67    feature = "storage",
68    feature = "wire",
69    feature = "descriptive",
70    feature = "value"
71))]
72#![cfg_attr(doc_cfg, doc(cfg(feature = "storage")))]
73
74mod encoding;
75mod error;
76
77#[cfg(feature = "test")]
78#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
79#[doc(hidden)]
80pub mod test;
81
82/// Convenient result alias for use with `musli::storage`.
83#[cfg(feature = "alloc")]
84#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
85pub type Result<T, E = Error> = core::result::Result<T, E>;
86
87#[cfg(feature = "alloc")]
88#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
89#[doc(inline)]
90pub use self::encoding::to_vec;
91#[cfg(all(feature = "std", feature = "alloc"))]
92#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "alloc"))))]
93#[doc(inline)]
94pub use self::encoding::to_writer;
95#[cfg(feature = "alloc")]
96#[doc(inline)]
97pub use self::encoding::{decode, encode, from_slice, to_fixed_bytes, to_slice};
98#[doc(inline)]
99pub use self::encoding::{Encoding, OPTIONS};
100#[doc(inline)]
101pub use self::error::Error;