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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
extern crate alloc;
/// A trait for types that can be encoded into a specific encoder.
///
/// Defines a generic interface for encoding data structures into
/// an encoder.
///
/// ## A note about purity
///
/// Implementations of `Encodable` **must be pure**. That means:
///
/// - **No side effects**: Implementations must not modify global or external
/// state.
/// - **Deterministic**: Given the same input and encoder, the output must
/// always be the same.
/// - **No panics**: Panicking inside an `encode` implementation is considered a
/// bug.
///
/// Ignoring these rules may lead to logic errors.
///
/// ## Encoder Specialization
///
/// On nightly Rust, you can use [trait specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html)
/// to implement optimized encodings for specific encoders, such as
/// [`SizeEncoder`](encoders::SizeEncoder). For example, you may want to use
/// this if your encoder runs computationally expensive operations for obtaining
/// the size of the encoded form.
///
/// ## Errors
///
/// Implementations must return an appropriate error if encoding fails. Errors
/// can occur if:
/// - The encoder encounters an internal error (e.g., out of space).
/// - The encoded output would be invalid.
///
/// Control flow may depend on these errors (unlike [`core::fmt::Write`]).
/// A trait that defines common types and operations for encoders.
///
/// An encoder is a type that can gather encoded data into a specific output
/// format. These are typically buffers, but can also be other types that
/// perform some operation on the encoded result. An example type is the
/// [`SizeEncoder`] type, which counts how many bytes would be encoded and is
/// often used for sizing the output buffer before encoding.
///
/// The `BaseEncoder` trait is the foundation for all encoders, providing a
/// common interface for encoding operations. It's also the main building block
/// for all [`combinators`].
///
/// [`SizeEncoder`]: encoders::SizeEncoder
/// A trait for encoders that can handle UTF-8 encodables.
///
/// This trait extends [`BaseEncoder`] to include a types and methods
/// specifically used for handling UTF-8 encodables. Encoders may use this trait
/// as bounds for generic encodables to signal that the output will be UTF-8
/// encoded. Doing so allows encoders such as [`String`] to be used.
/// A trait for encoders that can handle byte encodables.
///
/// This trait extends [`BaseEncoder`] to include types and methods specifically
/// used for handling any kind of byte encodables. Encoders may use this
/// trait as bounds for generic encodables to signal that the output will be any
/// kind of byte steam.
///
/// Note that all [`ByteEncoder`]s also implement [`StrEncoder`].
/// An extension trait for types that can compute the size of their encoded form
/// using a [`SizeEncoder`].
///
/// Use this trait to pre-compute buffer sizes or perform validations before
/// full encoding.
///
/// This trait is automatically implemented for all types that implement
/// [`Encodable`] for [`SizeEncoder`].
///
/// ## Errors
///
/// Returns an error if the [`Encodable`] fails to encode.
///
/// [`SizeEncoder`]: encoders::SizeEncoder