1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! 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,
//! });
//! # Ok::<_, musli::packed::Error>(())
//! ```
//!
//! [`storage`]: crate::storage
//! [`descriptive`]: crate::descriptive
//! [`wire`]: crate::wire
//! [`derives`]: crate::_help::derives
/// Convenient result alias for use with `musli::storage`.
///
/// # Examples
///
/// ```
/// use musli::packed::{self, Result};
/// use musli::{Encode, Decode};
///
/// #[derive(Debug, PartialEq, Encode, Decode)]
/// struct Item {
/// id: u32,
/// name: String,
/// }
///
/// fn packed_roundtrip(item: &Item) -> Result<Item> {
/// let bytes = packed::to_vec(item)?;
/// packed::from_slice(&bytes)
/// }
///
/// let original = Item {
/// id: 1,
/// name: "Test".to_string(),
/// };
/// let decoded = packed_roundtrip(&original)?;
/// assert_eq!(original, decoded);
/// # Ok::<_, musli::packed::Error>(())
/// ```
pub type Result<T, E = Error> = Result;
pub use to_vec;
pub use to_writer;
pub use ;
pub use ;
pub use Error;