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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! This crate provides a thin "library layer" used by the code generated by `micropb-gen`. Its
//! main purpose is to provide traits and APIs for encoding and decoding Protobuf messages.
//!
//! **For info on the generated code, see [`micropb-gen`](https://docs.rs/micropb-gen/latest/micropb_gen/).**
//!
//! # Decoder and Encoder
//!
//! `micropb` does not force a specific implementation for Protobuf data streams. Instead, streams
//! are represented as the [`PbRead`] and [`PbWrite`] traits, similar to
//! [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) and
//! [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html) from the standard library.
//!
//! In addition, `micropb` provides [`PbDecoder`] and [`PbEncoder`], which wrap input and output
//! streams respectively. Their job is to read and write the generated Rust types into Protobuf
//! data streams.
//!
//! ## Message traits
//!
//! Rust message structs generated by `micropb-gen` all implement [`MessageDecode`] and
//! [`MessageEncode`], which provide logic to read or write the message onto the data stream.
//!
//! ```rust
//! use micropb::{PbRead, PbDecoder, PbWrite, PbEncoder, MessageEncode, MessageDecode};
//! # use heapless_0_9 as heapless;
//!
//! # #[derive(Default)]
//! # struct ProtoMessage;
//! # impl MessageDecode for ProtoMessage {
//! # fn decode<R: PbRead>(
//! # &mut self,
//! # decoder: &mut PbDecoder<R>,
//! # len: usize,
//! # ) -> Result<(), micropb::DecodeError<R::Error>> {
//! # Ok(())
//! # }
//! # }
//! # impl MessageEncode for ProtoMessage {
//! # const MAX_SIZE: Result<usize, &str> = Ok(0);
//! #
//! # fn encode<W: PbWrite>(&self, encoder: &mut PbEncoder<W>) -> Result<(), W::Error> {
//! # Ok(())
//! # }
//! #
//! # fn compute_size(&self) -> usize {
//! # 0
//! # }
//! # }
//!
//! // ProtoMessage was generated by micropb
//! let mut message = ProtoMessage::default();
//!
//! // Create decoder out of a byte slice, which is our input data stream
//! let data = [0x08, 0x96, 0x01, /* additional bytes */];
//! let mut decoder = PbDecoder::new(data.as_slice());
//! // Decode an instance of `ProtoMessage` from the decoder
//! message.decode(&mut decoder, data.len()).unwrap();
//! // Or, decode directly from the bytes
//! message.decode_from_bytes(data.as_slice()).unwrap();
//!
//! // Use heapless::Vec as the output stream and build an encoder around it
//! let mut output = heapless::Vec::<u8, 16>::new();
//! let mut encoder = PbEncoder::new(&mut output);
//! // Encode a `ProtoMessage` to the encoder
//! message.encode(&mut encoder).unwrap();
//! ```
//!
//! # Container Traits
//!
//! `micropb` provides several options out of the box for handling "collection fields" that have
//! multiple elements, such as strings and repeated fields. These include containers from
//! [`heapless`](https://docs.rs/heapless/latest/heapless) and
//! [`arrayvec`](https://docs.rs/arrayvec/latest/arrayvec), as well as the ol' reliable [`Vec`],
//! [`String`], and [`Cow`](std::borrow::Cow) for environments with allocators.
//!
//! However, if you need to use your own container types, you'll have to implement the traits in
//! the [`container`] module yourself. For more info on configuring container types, refer to the
//! [`micropb-gen` docs](https://docs.rs/micropb-gen/latest/micropb_gen/#repeated-map-string-and-bytes-fields).
//!
//! # Feature Flags
//!
//! - **encode**: Enable support for encoding and computing the size of messages. If disabled, the
//! generator should be configured to not generate encoding logic via
//! [`Generator::encode_decode`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.encode_decode).
//! Enabled by default.
//!
//! - **decode**: Enable support for decoding messages. If disabled, the generator should be
//! configured to not generate decoding logic via
//! [`Generator::encode_decode`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.encode_decode).
//! Enabled by default.
//!
//! - **enable-64bit**: Enable 64-bit integer operations. If disabled, then 64-bit fields such as
//! `int64` or `sint64` should have
//! [`Config::int_size`](https://docs.rs/micropb-gen/latest/micropb_gen/config/struct.Config.html#method.int_size)
//! set to 32 bits or less. Has no effect on `double` fields. Enabled by default.
//!
//! - **alloc**: Implements container traits on `Vec`, `String`, and `BTreeMap` from
//! [`alloc`](https://doc.rust-lang.org/alloc), allowing them to be used as container fields.
//! Corresponds with
//! [`Generator::use_container_alloc`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.use_container_alloc)
//! from `micropb-gen`.
//!
//! - **std**: Enables standard library and implements [`PbMap`] on `HashMap`.
//! Corresponds with
//! [`Generator::use_container_std`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.use_container_std)
//! from `micropb-gen`.
//!
//! - **container-heapless-0-9**: Implements container traits on `Vec`, `String`, and `IndexMap` from
//! [`heapless`](https://docs.rs/heapless/latest/heapless) v0.9, allowing them to be used as container
//! fields. Corresponds with
//! [`Generator::use_container_heapless`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.use_container_heapless)
//! from `micropb-gen`.
//!
//! - **container-heapless-0-8**: Implements container traits on types from `heapless` v0.8. Corresponds with
//! [`Generator::use_container_heapless`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.use_container_heapless)
//! from `micropb-gen`.
//!
//! - **container-arrayvec-0-7**: Implements container traits on `ArrayVec` and `ArrayString` from
//! [`arrayvec`](https://docs.rs/arrayvec/latest/arrayvec) v0.7, allowing them to be used as container
//! fields. Corresponds with
//! [`Generator::use_container_arrayvec`](https://docs.rs/micropb-gen/latest/micropb_gen/struct.Generator.html#method.use_container_arrayvec)
//! from `micropb-gen`.
extern crate alloc;
pub use FixedLenString;
pub use ;
pub use StdReader;
pub use ;
pub use StdWriter;
pub use ;
pub use FieldDecode;
pub use FieldEncode;
pub use MessageDecode;
pub use ;
/// Protobuf wire type for varints.
pub const WIRE_TYPE_VARINT: u8 = 0;
/// Protobuf wire type for fixed 64-bit values.
pub const WIRE_TYPE_I64: u8 = 1;
/// Protobuf wire type for length-delimited records.
pub const WIRE_TYPE_LEN: u8 = 2;
/// Protobuf wire type for fixed 32-bit values.
pub const WIRE_TYPE_I32: u8 = 5;
/// Protobuf tag, consisting of the field number and wire type.
;
/// Field presence discipline