bilrost 0.1007.0

A compact protobuf-like serializer and deserializer for the Rust Language.
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Every tuple type starting at arity 1 and up to arity 12 implements ValueEncoder for the encoding
//! (E1, ... EN) where its elements are encoded with the corresponding sub-encoding. The
//! representation on the wire is exactly the same as if it were a message type that had fields with
//! tags 0 through arity-minus-1.
//!
//! Note again that these tags start at zero, not at 1 as they do by default when deriving `Message`
//! for a struct. This is for similar reasons; struct fields "feel" more natural when numbered
//! ordinally, but tuple types in rust are *indexed* starting at zero so we prefer to maintain
//! parity with that.
//!
//! The arity-zero unit tuple encodes the same way, but it does so independently of this file.
//! Because it has no fields, it has a privileged lack of ambiguity regarding *how* it will encode,
//! where every other tuple type might appear differently when included in a message depending on
//! which encodings are chosen for each member. For this reason, it is the only tuple type that
//! implements `Message` itself, and it stands as the prototype for a message with no defined
//! fields.

use bytes::{Buf, BufMut};

use crate::buf::ReverseBuf;
use crate::encoding::{
    delegate_value_encoding, encode_varint, encoded_len_varint, encoder_where_value_encoder,
    prepend_varint, skip_field, Canonicity, Capped, DecodeContext, DistinguishedEncoder,
    DistinguishedValueEncoder, EmptyState, Encoder, General, TagReader, TagRevWriter, TagWriter,
    TrivialTagMeasurer, ValueEncoder, WireType, Wiretyped,
};
use crate::DecodeError;

macro_rules! impl_tuple {
    (
        $arity:expr,
        $name:expr,
        $test_mod_name:ident,
        ($($numbers:tt),*),
        ($($numbers_desc:tt),*),
        ($($letters:ident),*),
        ($($letters_desc:ident),*),
        ($($encodings:ident),*),
        ($($tees:ident),*),
    ) => {
        // All tuple types encode as nested messages, so all of them implement ValueEncoder and
        // should therefore implement Encoder in terms of that.
        encoder_where_value_encoder!(
            ($($encodings,)*),
            with generics ($($encodings),*)
        );

        impl<$($letters,)* $($encodings,)*> Wiretyped<($($encodings,)*)> for ($($letters,)*) {
            const WIRE_TYPE: WireType = WireType::LengthDelimited;
        }

        impl<$($letters,)* $($encodings,)*> ValueEncoder<($($encodings,)*)> for ($($letters,)*)
        where
            $($letters: EmptyState + Encoder<$encodings>,)*
        {
            #[inline]
            fn encode_value<__B: BufMut + ?Sized>(value: &Self, buf: &mut __B) {
                // Because we do not implement tuples with more than arity 32, we can always use
                // the trivial tag measurer implementation.
                let tm = &mut TrivialTagMeasurer::new();
                let message_len = 0usize $(+ $letters::encoded_len($numbers, &value.$numbers, tm))*;
                encode_varint(message_len as u64, buf);
                let tw = &mut TagWriter::new();
                $($letters::encode($numbers, &value.$numbers, buf, tw);)*
            }

            #[inline]
            fn prepend_value<__B: ReverseBuf + ?Sized>(
                value: &Self,
                buf: &mut __B,
            ) {
                let end = buf.remaining();
                let tw = &mut TagRevWriter::new();
                $($letters_desc::prepend_encode($numbers_desc, &value.$numbers_desc, buf, tw);)*
                tw.finalize(buf);
                prepend_varint((buf.remaining() - end) as u64, buf);
            }

            #[inline]
            fn value_encoded_len(value: &Self) -> usize {
                // Because we do not implement tuples with more than arity 32, we can always use
                // the trivial tag measurer implementation.
                let tm = &mut TrivialTagMeasurer::new();
                let message_len = 0usize $(+ $letters::encoded_len($numbers, &value.$numbers, tm))*;
                encoded_len_varint(message_len as u64) + message_len
            }

            #[inline]
            fn decode_value<__B: Buf + ?Sized>(
                value: &mut Self,
                mut buf: Capped<__B>,
                ctx: DecodeContext,
            ) -> Result<(), DecodeError> {
                let mut buf = buf.take_length_delimited()?;
                ctx.limit_reached()?;
                let ctx = ctx.enter_recursion();
                let tr = &mut TagReader::new();
                let mut last_tag = None::<u32>;
                while buf.has_remaining()? {
                    let (tag, wire_type) = tr.decode_key(buf.lend())?;
                    let duplicated = last_tag == Some(tag);
                    last_tag = Some(tag);
                    // Decode the field. Each tuple field has a tag corresponding to its index.
                    match tag {
                        $($numbers => {
                            $letters::decode(
                                wire_type,
                                duplicated,
                                &mut value.$numbers,
                                buf.lend(),
                                ctx.clone(),
                            ).map_err(|mut error| {
                                error.push($name, stringify!($numbers));
                                error
                            })?
                        })*
                        _ => skip_field(wire_type, buf.lend())?,
                    }
                }
                Ok(())
            }
        }

        impl<$($letters,)* $($encodings,)*> DistinguishedValueEncoder<($($encodings,)*)>
        for ($($letters,)*)
        where
            Self: Eq,
            $($letters: Eq + EmptyState + DistinguishedEncoder<$encodings>,)*
        {
            #[inline]
            fn decode_value_distinguished<const ALLOW_EMPTY: bool>(
                value: &mut Self,
                mut buf: Capped<impl Buf + ?Sized>,
                ctx: DecodeContext,
            ) -> Result<Canonicity, DecodeError>
            where
                Self: Sized,
            {
                let mut buf = buf.take_length_delimited()?;
                // Since tuples emulate messages, empty values always encode and decode from zero
                // bytes. It is far cheaper to check here than to check after the value has been
                // decoded and checking the value's `is_empty()`.
                if !ALLOW_EMPTY && buf.remaining_before_cap() == 0 {
                    return Ok(Canonicity::NotCanonical);
                }
                ctx.limit_reached()?;
                let mut canon = Canonicity::Canonical;
                let ctx = ctx.enter_recursion();
                let tr = &mut TagReader::new();
                let mut last_tag = None::<u32>;
                while buf.has_remaining()? {
                    let (tag, wire_type) = tr.decode_key(buf.lend())?;
                    let duplicated = last_tag == Some(tag);
                    last_tag = Some(tag);
                    // Decode the field. Each tuple field has a tag corresponding to its index.
                    match tag {
                        $($numbers => {
                            canon.update($letters::decode_distinguished(
                                wire_type,
                                duplicated,
                                &mut value.$numbers,
                                buf.lend(),
                                ctx.clone(),
                            ).map_err(|mut error| {
                                error.push($name, stringify!($numbers));
                                error
                            })?);
                        })*
                        _ => {
                            skip_field(wire_type, buf.lend())?;
                            canon.update(Canonicity::HasExtensions);
                        },
                    }
                }
                Ok(canon)
            }
        }

        #[cfg(test)]
        mod $test_mod_name {
            // MSRV: impl From<[T; N]> for (T, ...{N}) is new in rust 1.71
            fn array_to_tuple<T: Clone>(arr: [T; $arity]) -> ($($tees,)*) {
                ($(arr[$numbers].clone(),)*)
            }

            mod delegated_bools {
                use crate::encoding::General;
                use crate::encoding::test::check_type_test;
                type T = bool;

                check_type_test!(
                    General,
                    expedient,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
                check_type_test!(
                    General,
                    distinguished,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
            }
            mod varint_bools {
                use crate::encoding::test::check_type_test;
                type T = bool;
                $(type $encodings = crate::encoding::Varint;)*

                check_type_test!(
                    ($($encodings,)*),
                    expedient,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
                check_type_test!(
                    ($($encodings,)*),
                    distinguished,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
            }
            mod fixed_floats {
                use crate::encoding::test::check_type_test;
                type T = f32;
                $(type $encodings = crate::encoding::Fixed;)*

                check_type_test!(
                    ($($encodings,)*),
                    expedient,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
            }
            mod small_arrays {
                use crate::encoding::test::check_type_test;
                type T = [u8; 1];
                $(type $encodings = crate::encoding::PlainBytes;)*

                check_type_test!(
                    ($($encodings,)*),
                    expedient,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
                check_type_test!(
                    ($($encodings,)*),
                    distinguished,
                    from [T; $arity],
                    into ($($tees,)*),
                    converter(value) { super::super::array_to_tuple(value) },
                    WireType::LengthDelimited
                );
            }
        }
    }
}

impl_tuple!(
    1,             //
    "(1-tuple)",   //
    tuple_arity_1, //
    (0),           //
    (0),           //
    (A),           //
    (A),           //
    (Ae),          //
    (T),           //
);
impl_tuple!(
    2,             //
    "(2-tuple)",   //
    tuple_arity_2, //
    (0, 1),        //
    (1, 0),        //
    (A, B),        //
    (B, A),        //
    (Ae, Be),      //
    (T, T),        //
);
impl_tuple!(
    3,             //
    "(3-tuple)",   //
    tuple_arity_3, //
    (0, 1, 2),     //
    (2, 1, 0),     //
    (A, B, C),     //
    (C, B, A),     //
    (Ae, Be, Ce),  //
    (T, T, T),     //
);
impl_tuple!(
    4,                //
    "(4-tuple)",      //
    tuple_arity_4,    //
    (0, 1, 2, 3),     //
    (3, 2, 1, 0),     //
    (A, B, C, D),     //
    (D, C, B, A),     //
    (Ae, Be, Ce, De), //
    (T, T, T, T),     //
);
impl_tuple!(
    5,                    //
    "(5-tuple)",          //
    tuple_arity_5,        //
    (0, 1, 2, 3, 4),      //
    (4, 3, 2, 1, 0),      //
    (A, B, C, D, E),      //
    (E, D, C, B, A),      //
    (Ae, Be, Ce, De, Ee), //
    (T, T, T, T, T),      //
);
impl_tuple!(
    6,                        //
    "(6-tuple)",              //
    tuple_arity_6,            //
    (0, 1, 2, 3, 4, 5),       //
    (5, 4, 3, 2, 1, 0),       //
    (A, B, C, D, E, F),       //
    (F, E, D, C, B, A),       //
    (Ae, Be, Ce, De, Ee, Fe), //
    (T, T, T, T, T, T),       //
);
impl_tuple!(
    7,                            //
    "(7-tuple)",                  //
    tuple_arity_7,                //
    (0, 1, 2, 3, 4, 5, 6),        //
    (6, 5, 4, 3, 2, 1, 0),        //
    (A, B, C, D, E, F, G),        //
    (G, F, E, D, C, B, A),        //
    (Ae, Be, Ce, De, Ee, Fe, Ge), //
    (T, T, T, T, T, T, T),        //
);
impl_tuple!(
    8,                                //
    "(8-tuple)",                      //
    tuple_arity_8,                    //
    (0, 1, 2, 3, 4, 5, 6, 7),         //
    (7, 6, 5, 4, 3, 2, 1, 0),         //
    (A, B, C, D, E, F, G, H),         //
    (H, G, F, E, D, C, B, A),         //
    (Ae, Be, Ce, De, Ee, Fe, Ge, He), //
    (T, T, T, T, T, T, T, T),         //
);
impl_tuple!(
    9,                                    //
    "(9-tuple)",                          //
    tuple_arity_9,                        //
    (0, 1, 2, 3, 4, 5, 6, 7, 8),          //
    (8, 7, 6, 5, 4, 3, 2, 1, 0),          //
    (A, B, C, D, E, F, G, H, I),          //
    (I, H, G, F, E, D, C, B, A),          //
    (Ae, Be, Ce, De, Ee, Fe, Ge, He, Ie), //
    (T, T, T, T, T, T, T, T, T),          //
);
impl_tuple!(
    10,                                       //
    "(10-tuple)",                             //
    tuple_arity_10,                           //
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9),           //
    (9, 8, 7, 6, 5, 4, 3, 2, 1, 0),           //
    (A, B, C, D, E, F, G, H, I, J),           //
    (J, I, H, G, F, E, D, C, B, A),           //
    (Ae, Be, Ce, De, Ee, Fe, Ge, He, Ie, Je), //
    (T, T, T, T, T, T, T, T, T, T),           //
);
impl_tuple!(
    11,                                           //
    "(11-tuple)",                                 //
    tuple_arity_11,                               //
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),           //
    (10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0),           //
    (A, B, C, D, E, F, G, H, I, J, K),            //
    (K, J, I, H, G, F, E, D, C, B, A),            //
    (Ae, Be, Ce, De, Ee, Fe, Ge, He, Ie, Je, Ke), //
    (T, T, T, T, T, T, T, T, T, T, T),            //
);
impl_tuple!(
    12,                                               //
    "(12-tuple)",                                     //
    tuple_arity_12,                                   //
    (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11),           //
    (11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0),           //
    (A, B, C, D, E, F, G, H, I, J, K, L),             //
    (L, K, J, I, H, G, F, E, D, C, B, A),             //
    (Ae, Be, Ce, De, Ee, Fe, Ge, He, Ie, Je, Ke, Le), //
    (T, T, T, T, T, T, T, T, T, T, T, T),             //
);

delegate_value_encoding!(
    delegate from (General) to ((General,))
    for type ((A,)) including distinguished
    with generics (A)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General))
    for type ((A, B)) including distinguished
    with generics (A, B)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General))
    for type ((A, B, C)) including distinguished
    with generics (A, B, C)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General))
    for type ((A, B, C, D)) including distinguished
    with generics (A, B, C, D)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General))
    for type ((A, B, C, D, E)) including distinguished
    with generics (A, B, C, D, E)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General))
    for type ((A, B, C, D, E, F)) including distinguished
    with generics (A, B, C, D, E, F)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General))
    for type ((A, B, C, D, E, F, G)) including distinguished
    with generics (A, B, C, D, E, F, G)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General, General))
    for type ((A, B, C, D, E, F, G, H)) including distinguished
    with generics (A, B, C, D, E, F, G, H)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General, General, General))
    for type ((A, B, C, D, E, F, G, H, I)) including distinguished
    with generics (A, B, C, D, E, F, G, H, I)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General, General, General, General))
    for type ((A, B, C, D, E, F, G, H, I, J)) including distinguished
    with generics (A, B, C, D, E, F, G, H, I, J)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General, General, General, General, General))
    for type ((A, B, C, D, E, F, G, H, I, J, K)) including distinguished
    with generics (A, B, C, D, E, F, G, H, I, J, K)
);
delegate_value_encoding!(
    delegate from (General) to ((General, General, General, General, General, General,
                                 General, General, General, General, General, General))
    for type ((A, B, C, D, E, F, G, H, I, J, K, L)) including distinguished
    with generics (A, B, C, D, E, F, G, H, I, J, K, L)
);