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///
84/// # Examples
85///
86/// ```
87/// use musli::packed::{self, Result};
88/// use musli::{Encode, Decode};
89///
90/// #[derive(Debug, PartialEq, Encode, Decode)]
91/// struct Item {
92/// id: u32,
93/// name: String,
94/// }
95///
96/// fn packed_roundtrip(item: &Item) -> Result<Item> {
97/// let bytes = packed::to_vec(item)?;
98/// packed::from_slice(&bytes)
99/// }
100///
101/// let original = Item {
102/// id: 1,
103/// name: "Test".to_string(),
104/// };
105/// let decoded = packed_roundtrip(&original)?;
106/// assert_eq!(original, decoded);
107/// # Ok::<_, musli::packed::Error>(())
108/// ```
109#[cfg(feature = "alloc")]
110#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
111pub type Result<T, E = Error> = core::result::Result<T, E>;
112
113#[cfg(feature = "alloc")]
114#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
115#[doc(inline)]
116pub use self::encoding::to_vec;
117#[cfg(all(feature = "std", feature = "alloc"))]
118#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "alloc"))))]
119#[doc(inline)]
120pub use self::encoding::to_writer;
121#[doc(inline)]
122pub use self::encoding::{Encoding, OPTIONS};
123#[cfg(feature = "alloc")]
124#[doc(inline)]
125pub use self::encoding::{decode, encode, from_slice, to_fixed_bytes, to_slice};
126#[doc(inline)]
127pub use self::error::Error;