bilrost 0.1014.2

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use crate::buf::ReverseBuf;
use crate::encoding::{
    Capped, DecodeContext, RestrictedDecodeContext, TagMeasurer, TagRevWriter, TagWriter, WireType,
};
use crate::{Canonicity, DecodeError};
use alloc::boxed::Box;
use bytes::{Buf, BufMut};

/// Trait to be implemented by (or more commonly derived for) oneofs, which have knowledge of their
/// variants' tags and encoding.
///
/// `Oneof` values can be represented in messages because they have an "empty"  state (typically a
/// dedicated empty enum variant or Option::None). When `Oneof` is derived for an enum that does not
/// have a unit variant, the trait that is actually derived is `NonEmptyOneof`, which has no empty
/// states and must be wrapped in `Option` at some point to be used.
///
/// In addition to decoding into the variants of the oneof, implementations of the maybe-empty
/// `Oneof` traits need to be able to return and attach useful details to the appropriate errors for
/// collisions (when they decode a field but they already contain values) or when decoding a value
/// for the oneof otherwise encounters an error. For this reason there are the following differences
/// between `Oneof` and `NonEmptyOneof`:
///
/// * `Oneof::oneof_current_tag` returns `Option<u32>` instead of `u32`
/// * `Oneof::oneof_decode_field` accepts a `value: &mut Self` argument, while `NonEmptyOneof` does
///   not; the `Oneof` version of this function returns `Result<(), DecodeError>`, and the
///   `NonEmptyOneof` version returns `Result<Self, DecodeError>` directly.
/// * `Oneof::oneof_decode_field` is responsible for attaching error detail information when a
///   decoding error occurs, while `NonEmptyOneof` does not need to do that.
///
/// There are implementations provided, like `impl<T> Oneof for Option<T> where T: NonEmptyOneof`
/// for all relevant oneof decoder traits (see the `generic_oneof_grant_empty_state_impls` mod).
/// These implementations take care of the above contract boundary as well.
///
/// Other than that: Both empty and non-empty oneofs can be `Box`ed, as there are also wrapper impls
/// to cover that.
pub trait Oneof {
    const FIELD_TAGS: &'static [u32];

    /// Returns a new empty oneof.
    fn empty() -> Self;

    /// Returns whether the oneof is in the empty variant.
    fn is_empty(&self) -> bool;

    /// Resets the oneof to the empty variant.
    fn clear(&mut self);

    /// Encodes the fields of the oneof into the given buffer.
    fn oneof_encode<B: BufMut + ?Sized>(&self, buf: &mut B, tw: &mut TagWriter);

    /// Prepends the fields of the oneof into the given buffer.
    fn oneof_prepend<B: ReverseBuf + ?Sized>(&self, buf: &mut B, tw: &mut TagRevWriter);

    /// Measures the number of bytes that would encode this oneof.
    fn oneof_encoded_len(&self, tm: &mut impl TagMeasurer) -> usize;

    /// Returns the current tag of the oneof, if any.
    fn oneof_current_tag(&self) -> Option<u32>;

    /// Returns the diagnostic name of the variant with the given tag. The first returned value is
    /// the name of the oneof enum, and the second is the name of the field.
    fn oneof_variant_name(tag: u32) -> (&'static str, &'static str);
}

/// Relaxed owned decoding trait for oneofs.
pub trait OneofDecoder: Oneof {
    /// Decodes from the given buffer.
    fn oneof_decode_field<B: Buf + ?Sized>(
        value: &mut Self,
        tag: u32,
        wire_type: WireType,
        buf: Capped<B>,
        ctx: DecodeContext,
    ) -> Result<(), DecodeError>;
}

/// Distinguished owned decoding trait for oneofs.
pub trait DistinguishedOneofDecoder: Oneof {
    /// Decodes from the given buffer in distinguished mode.
    fn oneof_decode_field_distinguished<B: Buf + ?Sized>(
        value: &mut Self,
        tag: u32,
        wire_type: WireType,
        buf: Capped<B>,
        ctx: RestrictedDecodeContext,
    ) -> Result<Canonicity, DecodeError>;
}

/// Relaxed borrowed decoding trait for oneofs.
pub trait OneofBorrowDecoder<'a>: Oneof {
    fn oneof_borrow_decode_field(
        value: &mut Self,
        tag: u32,
        wire_type: WireType,
        buf: Capped<&'a [u8]>,
        ctx: DecodeContext,
    ) -> Result<(), DecodeError>;
}

/// Distinguished borrowed decoding trait for oneofs.
pub trait DistinguishedOneofBorrowDecoder<'a>: Oneof {
    fn oneof_borrow_decode_field_distinguished(
        value: &mut Self,
        tag: u32,
        wire_type: WireType,
        buf: Capped<&'a [u8]>,
        ctx: RestrictedDecodeContext,
    ) -> Result<Canonicity, DecodeError>;
}

/// Underlying trait for a oneof that has no inherent "empty" variant, opting instead to be wrapped
/// in an `Option`. This is the real trait that is derived for `enum` types that don't have a
/// natural unit variant. Like the other `Oneof` traits, this is not intended for use by library
/// users.
pub trait NonEmptyOneof {
    const FIELD_TAGS: &'static [u32];

    /// Encodes the fields of the oneof into the given buffer.
    fn oneof_encode<B: BufMut + ?Sized>(&self, buf: &mut B, tw: &mut TagWriter);

    /// Prepends the fields of the oneof into the given buffer.
    fn oneof_prepend<B: ReverseBuf + ?Sized>(&self, buf: &mut B, tw: &mut TagRevWriter);

    /// Measures the number of bytes that would encode this oneof.
    fn oneof_encoded_len(&self, tm: &mut impl TagMeasurer) -> usize;

    /// Returns the current tag of the oneof.
    fn oneof_current_tag(&self) -> u32;

    /// Returns the diagnostic name of the variant with the given tag. The first returned value is
    /// the name of the oneof enum, and the second is the name of the field.
    fn oneof_variant_name(tag: u32) -> (&'static str, &'static str);
}

/// Relaxed owned decoding trait for non-empty oneofs.
pub trait NonEmptyOneofDecoder: NonEmptyOneof + Sized {
    /// Decodes from the given buffer.
    fn oneof_decode_field<B: Buf + ?Sized>(
        tag: u32,
        wire_type: WireType,
        buf: Capped<B>,
        ctx: DecodeContext,
    ) -> Result<Self, DecodeError>;
}

/// Distinguished owned decoding trait for non-empty oneofs.
pub trait NonEmptyDistinguishedOneofDecoder: NonEmptyOneof + Sized {
    /// Decodes from the given buffer.
    fn oneof_decode_field_distinguished<B: Buf + ?Sized>(
        tag: u32,
        wire_type: WireType,
        buf: Capped<B>,
        ctx: RestrictedDecodeContext,
    ) -> Result<(Self, Canonicity), DecodeError>;
}

/// Relaxed borrowed decoding trait for non-empty oneofs.
pub trait NonEmptyOneofBorrowDecoder<'a>: NonEmptyOneof + Sized {
    fn oneof_borrow_decode_field(
        tag: u32,
        wire_type: WireType,
        buf: Capped<&'a [u8]>,
        ctx: DecodeContext,
    ) -> Result<Self, DecodeError>;
}

/// Distinguished borrowed decoding trait for non-empty oneofs.
pub trait NonEmptyDistinguishedOneofBorrowDecoder<'a>: NonEmptyOneof + Sized {
    fn oneof_borrow_decode_field_distinguished(
        tag: u32,
        wire_type: WireType,
        buf: Capped<&'a [u8]>,
        ctx: RestrictedDecodeContext,
    ) -> Result<(Self, Canonicity), DecodeError>;
}

/// These are the impls that grant Oneof implementation status with a proper empty state to NonEmpty
/// oneof implementers
mod generic_oneof_grant_empty_state_impls {
    use super::*;
    use crate::DecodeErrorKind::{ConflictingFields, UnexpectedlyRepeated};

    impl<T> Oneof for Option<T>
    where
        T: NonEmptyOneof,
    {
        const FIELD_TAGS: &'static [u32] = T::FIELD_TAGS;

        fn empty() -> Self {
            None
        }

        fn is_empty(&self) -> bool {
            self.is_none()
        }

        fn clear(&mut self) {
            *self = None;
        }

        #[inline]
        fn oneof_encode<B: BufMut + ?Sized>(&self, buf: &mut B, tw: &mut TagWriter) {
            if let Some(value) = self {
                value.oneof_encode(buf, tw);
            }
        }

        #[inline]
        fn oneof_prepend<B: ReverseBuf + ?Sized>(&self, buf: &mut B, tw: &mut TagRevWriter) {
            if let Some(value) = self {
                value.oneof_prepend(buf, tw);
            }
        }

        #[inline]
        fn oneof_encoded_len(&self, tm: &mut impl TagMeasurer) -> usize {
            if let Some(value) = self {
                value.oneof_encoded_len(tm)
            } else {
                0
            }
        }

        #[inline]
        fn oneof_current_tag(&self) -> Option<u32> {
            self.as_ref().map(NonEmptyOneof::oneof_current_tag)
        }

        #[inline]
        fn oneof_variant_name(tag: u32) -> (&'static str, &'static str) {
            T::oneof_variant_name(tag)
        }
    }

    impl<T> OneofDecoder for Option<T>
    where
        T: NonEmptyOneofDecoder,
    {
        #[inline]
        fn oneof_decode_field<B: Buf + ?Sized>(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: DecodeContext,
        ) -> Result<(), DecodeError> {
            if let Some(already) = value {
                Err(DecodeError::new(if already.oneof_current_tag() == tag {
                    UnexpectedlyRepeated
                } else {
                    ConflictingFields
                }))
            } else {
                T::oneof_decode_field(tag, wire_type, buf, ctx)
                    .map(|decoded| *value = Some(decoded))
            }
            .map_err(|mut err| {
                let (msg, field) = T::oneof_variant_name(tag);
                err.push(msg, field);
                err
            })
        }
    }

    impl<T> DistinguishedOneofDecoder for Option<T>
    where
        T: NonEmptyDistinguishedOneofDecoder + NonEmptyOneof,
        Self: Oneof,
    {
        #[inline]
        fn oneof_decode_field_distinguished<B: Buf + ?Sized>(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: RestrictedDecodeContext,
        ) -> Result<Canonicity, DecodeError> {
            if let Some(already) = value {
                Err(DecodeError::new(if already.oneof_current_tag() == tag {
                    UnexpectedlyRepeated
                } else {
                    ConflictingFields
                }))
            } else {
                T::oneof_decode_field_distinguished(tag, wire_type, buf, ctx.clone()).map(
                    |(decoded, canon)| {
                        *value = Some(decoded);
                        canon
                    },
                )
            }
            .map_err(|mut err| {
                let (msg, field) = T::oneof_variant_name(tag);
                err.push(msg, field);
                err
            })
        }
    }

    impl<'a, T> OneofBorrowDecoder<'a> for Option<T>
    where
        T: NonEmptyOneofBorrowDecoder<'a>,
    {
        #[inline]
        fn oneof_borrow_decode_field(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: DecodeContext,
        ) -> Result<(), DecodeError> {
            if let Some(already) = value {
                Err(DecodeError::new(if already.oneof_current_tag() == tag {
                    UnexpectedlyRepeated
                } else {
                    ConflictingFields
                }))
            } else {
                T::oneof_borrow_decode_field(tag, wire_type, buf, ctx)
                    .map(|decoded| *value = Some(decoded))
            }
            .map_err(|mut err| {
                let (msg, field) = T::oneof_variant_name(tag);
                err.push(msg, field);
                err
            })
        }
    }

    impl<'a, T> DistinguishedOneofBorrowDecoder<'a> for Option<T>
    where
        T: NonEmptyDistinguishedOneofBorrowDecoder<'a> + NonEmptyOneof,
        Self: Oneof,
    {
        #[inline]
        fn oneof_borrow_decode_field_distinguished(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: RestrictedDecodeContext,
        ) -> Result<Canonicity, DecodeError> {
            if let Some(already) = value {
                Err(DecodeError::new(if already.oneof_current_tag() == tag {
                    UnexpectedlyRepeated
                } else {
                    ConflictingFields
                }))
            } else {
                T::oneof_borrow_decode_field_distinguished(tag, wire_type, buf, ctx.clone()).map(
                    |(decoded, canon)| {
                        *value = Some(decoded);
                        canon
                    },
                )
            }
            .map_err(|mut err| {
                let (msg, field) = T::oneof_variant_name(tag);
                err.push(msg, field);
                err
            })
        }
    }
}

/// These are the impls that make the oneof trait transparent to Box
mod generic_boxed_oneof_impls {
    use super::*;

    impl<T> Oneof for Box<T>
    where
        T: Oneof,
    {
        const FIELD_TAGS: &'static [u32] = <T as Oneof>::FIELD_TAGS;

        #[inline]
        fn empty() -> Self {
            Box::new(T::empty())
        }

        #[inline]
        fn is_empty(&self) -> bool {
            self.as_ref().is_empty()
        }

        #[inline]
        fn clear(&mut self) {
            self.as_mut().clear()
        }

        #[inline]
        fn oneof_encode<B: BufMut + ?Sized>(&self, buf: &mut B, tw: &mut TagWriter) {
            Oneof::oneof_encode(&**self, buf, tw)
        }

        #[inline]
        fn oneof_prepend<B: ReverseBuf + ?Sized>(&self, buf: &mut B, tw: &mut TagRevWriter) {
            Oneof::oneof_prepend(&**self, buf, tw)
        }

        #[inline]
        fn oneof_encoded_len(&self, tm: &mut impl TagMeasurer) -> usize {
            Oneof::oneof_encoded_len(&**self, tm)
        }

        #[inline]
        fn oneof_current_tag(&self) -> Option<u32> {
            Oneof::oneof_current_tag(&**self)
        }

        #[inline]
        fn oneof_variant_name(tag: u32) -> (&'static str, &'static str) {
            T::oneof_variant_name(tag)
        }
    }

    impl<T> OneofDecoder for Box<T>
    where
        T: OneofDecoder,
    {
        #[inline]
        fn oneof_decode_field<B: Buf + ?Sized>(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: DecodeContext,
        ) -> Result<(), DecodeError> {
            OneofDecoder::oneof_decode_field(&mut **value, tag, wire_type, buf, ctx)
        }
    }

    impl<T> DistinguishedOneofDecoder for Box<T>
    where
        T: DistinguishedOneofDecoder,
    {
        #[inline]
        fn oneof_decode_field_distinguished<B: Buf + ?Sized>(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: RestrictedDecodeContext,
        ) -> Result<Canonicity, DecodeError> {
            DistinguishedOneofDecoder::oneof_decode_field_distinguished(
                &mut **value,
                tag,
                wire_type,
                buf,
                ctx,
            )
        }
    }

    impl<'a, T> OneofBorrowDecoder<'a> for Box<T>
    where
        T: OneofBorrowDecoder<'a>,
    {
        #[inline]
        fn oneof_borrow_decode_field(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: DecodeContext,
        ) -> Result<(), DecodeError> {
            OneofBorrowDecoder::oneof_borrow_decode_field(&mut **value, tag, wire_type, buf, ctx)
        }
    }

    impl<'a, T> DistinguishedOneofBorrowDecoder<'a> for Box<T>
    where
        T: DistinguishedOneofBorrowDecoder<'a>,
    {
        #[inline]
        fn oneof_borrow_decode_field_distinguished(
            value: &mut Self,
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: RestrictedDecodeContext,
        ) -> Result<Canonicity, DecodeError> {
            DistinguishedOneofBorrowDecoder::oneof_borrow_decode_field_distinguished(
                &mut **value,
                tag,
                wire_type,
                buf,
                ctx,
            )
        }
    }

    impl<T> NonEmptyOneof for Box<T>
    where
        T: NonEmptyOneof,
    {
        const FIELD_TAGS: &'static [u32] = <T as NonEmptyOneof>::FIELD_TAGS;

        #[inline]
        fn oneof_encode<B: BufMut + ?Sized>(&self, buf: &mut B, tw: &mut TagWriter) {
            NonEmptyOneof::oneof_encode(&**self, buf, tw)
        }

        #[inline]
        fn oneof_prepend<B: ReverseBuf + ?Sized>(&self, buf: &mut B, tw: &mut TagRevWriter) {
            NonEmptyOneof::oneof_prepend(&**self, buf, tw)
        }

        #[inline]
        fn oneof_encoded_len(&self, tm: &mut impl TagMeasurer) -> usize {
            NonEmptyOneof::oneof_encoded_len(&**self, tm)
        }

        #[inline]
        fn oneof_current_tag(&self) -> u32 {
            NonEmptyOneof::oneof_current_tag(&**self)
        }

        #[inline]
        fn oneof_variant_name(tag: u32) -> (&'static str, &'static str) {
            T::oneof_variant_name(tag)
        }
    }

    impl<T> NonEmptyOneofDecoder for Box<T>
    where
        T: NonEmptyOneofDecoder,
    {
        #[inline]
        fn oneof_decode_field<B: Buf + ?Sized>(
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: DecodeContext,
        ) -> Result<Self, DecodeError> {
            T::oneof_decode_field(tag, wire_type, buf, ctx).map(Box::new)
        }
    }

    impl<T> NonEmptyDistinguishedOneofDecoder for Box<T>
    where
        T: NonEmptyDistinguishedOneofDecoder,
    {
        #[inline]
        fn oneof_decode_field_distinguished<B: Buf + ?Sized>(
            tag: u32,
            wire_type: WireType,
            buf: Capped<B>,
            ctx: RestrictedDecodeContext,
        ) -> Result<(Self, Canonicity), DecodeError> {
            NonEmptyDistinguishedOneofDecoder::oneof_decode_field_distinguished(
                tag, wire_type, buf, ctx,
            )
            .map(|(val, canon)| (Box::new(val), canon))
        }
    }

    impl<'a, T> NonEmptyOneofBorrowDecoder<'a> for Box<T>
    where
        T: NonEmptyOneofBorrowDecoder<'a>,
    {
        #[inline]
        fn oneof_borrow_decode_field(
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: DecodeContext,
        ) -> Result<Self, DecodeError> {
            NonEmptyOneofBorrowDecoder::oneof_borrow_decode_field(tag, wire_type, buf, ctx)
                .map(Box::new)
        }
    }

    impl<'a, T> NonEmptyDistinguishedOneofBorrowDecoder<'a> for Box<T>
    where
        T: NonEmptyDistinguishedOneofBorrowDecoder<'a>,
    {
        #[inline]
        fn oneof_borrow_decode_field_distinguished(
            tag: u32,
            wire_type: WireType,
            buf: Capped<&'a [u8]>,
            ctx: RestrictedDecodeContext,
        ) -> Result<(Self, Canonicity), DecodeError> {
            NonEmptyDistinguishedOneofBorrowDecoder::oneof_borrow_decode_field_distinguished(
                tag, wire_type, buf, ctx,
            )
            .map(|(val, canon)| (Box::new(val), canon))
        }
    }
}