reallyme-buffa 0.1.1

ReallyMe fork: A pure Rust Protocol Buffers implementation with first-class editions support
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
// Modified by ReallyMe Technologies Limited.
//! The decode pass: [`merge_to_limit`] and its per-kind arms.
//!
//! Decoding runs over one contiguous `&[u8]`, so every arm reads through a
//! non-generic function compiled in this crate.

use super::map::merge_map;
use super::scalar::Sc;
use super::{
    Bool, Double, Entry, EnumVt, Fixed32, Fixed64, Float, Int32, Int64, Kind, MessageTable,
    OneofVt, Sfixed32, Sfixed64, Sint32, Sint64, Uint32, Uint64, IMPLICIT, NO_UNKNOWN, OPTIONAL,
    PACKED, REPEATED, REQUIRED,
};
use crate::alloc::{string::String, vec::Vec};
use crate::bytes::Buf;
use crate::encoding::{
    check_wire_type, decode_unknown_field, decode_varint, wire_type_mismatch, Tag, WireType,
};
use crate::message::MAX_MESSAGE_BYTES;
use crate::{types, DecodeContext, DecodeError, UnknownField, UnknownFieldData, UnknownFields};

/// Decode into the message at `base` until `buf` has `limit` bytes remaining.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
pub(super) unsafe fn merge_to_limit<B: Buf>(
    table: &MessageTable,
    base: *mut u8,
    buf: &mut B,
    ctx: DecodeContext<'_>,
    limit: usize,
) -> Result<(), DecodeError> {
    let n = buf.remaining().saturating_sub(limit);
    let chunk = buf.chunk();
    if chunk.len() >= n {
        let mut payload = &chunk[..n];
        // SAFETY: forwarded from the caller.
        unsafe { merge_slice(table, base, &mut payload, ctx)? };
        buf.advance(n);
        Ok(())
    } else {
        let gathered = buf.copy_to_bytes(n);
        let mut payload = &gathered[..];
        // SAFETY: forwarded from the caller.
        unsafe { merge_slice(table, base, &mut payload, ctx) }
    }
}

/// Decode a length-prefixed message into the message at `base`.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
pub(super) unsafe fn merge_length_delimited<B: Buf>(
    table: &MessageTable,
    base: *mut u8,
    buf: &mut B,
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let ctx = ctx.descend()?;
    let len = decode_varint(buf)?;
    if len > u64::from(MAX_MESSAGE_BYTES) {
        return Err(DecodeError::MessageTooLarge);
    }
    let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
    if buf.remaining() < len {
        return Err(DecodeError::UnexpectedEof);
    }
    let limit = buf.remaining() - len;
    // SAFETY: forwarded from the caller.
    unsafe { merge_to_limit(table, base, buf, ctx, limit) }
}

/// Decode one field, whose `tag` has been read, into the message at `base`.
/// The rest of `buf` must be one chunk.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
pub(super) unsafe fn merge_field<B: Buf>(
    table: &MessageTable,
    base: *mut u8,
    tag: Tag,
    buf: &mut B,
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let remaining = buf.remaining();
    let chunk = buf.chunk();
    if chunk.len() < remaining {
        return Err(DecodeError::UnexpectedEof);
    }
    let mut payload = chunk;
    // SAFETY: forwarded from the caller.
    unsafe { merge_one(table, base, tag, &mut payload, ctx)? };
    let consumed = remaining - payload.len();
    buf.advance(consumed);
    Ok(())
}

/// Decode every field in `buf` into the message at `base`.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
unsafe fn merge_slice(
    table: &MessageTable,
    base: *mut u8,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    while !buf.is_empty() {
        let tag = Tag::decode(buf)?;
        // SAFETY: forwarded from the caller.
        unsafe { merge_one(table, base, tag, buf, ctx)? };
    }
    Ok(())
}

/// Decode a length-prefixed sub-message into the message at `base`.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
pub(super) unsafe fn merge_sub(
    table: &MessageTable,
    base: *mut u8,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let ctx = ctx.descend()?;
    let len = decode_varint(buf)?;
    if len > u64::from(MAX_MESSAGE_BYTES) {
        return Err(DecodeError::MessageTooLarge);
    }
    let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
    if buf.len() < len {
        return Err(DecodeError::UnexpectedEof);
    }
    let (mut payload, rest) = buf.split_at(len);
    *buf = rest;
    // SAFETY: forwarded from the caller.
    unsafe { merge_slice(table, base, &mut payload, ctx) }
}

/// # Safety
///
/// `base` points to a live message of the type `table` describes.
#[inline]
unsafe fn merge_one(
    table: &MessageTable,
    base: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let Some(e) = table.find(tag.field_number()) else {
        // SAFETY: forwarded from the caller.
        return unsafe { merge_unknown(table, base, tag, buf, ctx) };
    };
    // SAFETY: the offset is within the message, per the table's contract.
    let slot = unsafe { base.add(e.offset as usize) };
    // SAFETY: `slot` is the field `e` describes.
    unsafe {
        if e.kind == Kind::Map {
            merge_map(table, e, base, slot, tag, buf, ctx)
        } else {
            merge_kind(table, e, base, slot, tag, buf, ctx)
        }
    }
}

/// # Safety
///
/// `base` points to a live message of the type `table` describes.
#[cold]
unsafe fn merge_unknown(
    table: &MessageTable,
    base: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    if table.unknown == NO_UNKNOWN {
        return crate::encoding::skip_unknown_field(tag, buf, ctx);
    }
    let field = decode_unknown_field(tag, buf, ctx)?;
    // SAFETY: `unknown` is the offset of the message's `UnknownFields`.
    unsafe { (*base.add(table.unknown as usize).cast::<UnknownFields>()).push(field) };
    Ok(())
}

// The arm for a map is unreachable: `merge_one`, which `merge_slice` calls for
// each field, tests for a map before it calls this. `map::merge_entry` also
// calls this function, for a key and a value, which are never maps. The
// reason is in the module documentation of `map.rs`.
macro_rules! merge_dispatch {
    ($($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
        /// # Safety
        ///
        /// `slot` points to the field `e` describes, inside the live message
        /// at `base` of the type `table` describes. `base` may be null if
        /// `table` has no unknown fields, which is how a map entry, whose
        /// fields are not in a message, calls this.
        #[inline]
        pub(super) unsafe fn merge_kind(
            table: &MessageTable,
            e: &Entry,
            base: *mut u8,
            slot: *mut u8,
            tag: Tag,
            buf: &mut &[u8],
            ctx: DecodeContext<'_>,
        ) -> Result<(), DecodeError> {
            // SAFETY: each arm writes the slot as the type its kind names.
            unsafe {
                match e.kind {
                    $(Kind::$name => merge_dispatch!(@arm $fam $ty $card table e base slot tag buf ctx),)*
                }
            }
        }
    };
    (@arm Scalar $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_scalar::<$ty, $card>($slot, $tag, $buf)
    };
    (@arm Str $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_str::<$card>($slot, $tag, $buf, $ctx)
    };
    (@arm Bytes $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_bytes::<$card>($slot, $tag, $buf, $ctx)
    };
    (@arm Enum $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_enum::<$card>($table, $e, $base, $slot, $tag, $buf, $ctx)
    };
    (@arm Msg $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_msg::<$card>($table, $e, $slot, $tag, $buf, $ctx)
    };
    (@arm Oneof $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof($table, $e, $base, $slot, $tag, $buf, $ctx)
    };
    (@arm Map $ty:ident $card:ident $table:ident $e:ident $base:ident $slot:ident $tag:ident $buf:ident $ctx:ident) => {
        unreachable!("a map field is merged by `merge_one`")
    };
}

kind_table!(merge_dispatch);

/// Decode the member of a oneof that the entry `e` describes into the oneof.
///
/// # Safety
///
/// `slot` points to the `Option` of the oneof enum that `e`'s group describes,
/// inside the live message at `base` of the type `table` describes.
#[inline(never)]
unsafe fn merge_oneof(
    table: &MessageTable,
    e: &Entry,
    base: *mut u8,
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let m = table.member(e);
    let payload_entry = Entry {
        kind: m.kind,
        aux: m.aux,
        ..*e
    };
    let oneof = OneofSlot {
        group: table.group(m),
        slot,
    };
    // SAFETY: forwarded from the caller.
    unsafe { merge_payload(table, &payload_entry, &oneof, base, tag, buf, ctx) }
}

/// A oneof inside the message that is being decoded: the `Option` of its enum
/// and its descriptor.
struct OneofSlot<'a> {
    group: &'a OneofVt,
    slot: *mut u8,
}

impl OneofSlot<'_> {
    /// Make the member `number` the one that is set, and return a pointer to
    /// its value.
    ///
    /// # Safety
    ///
    /// `slot` points to a live `Option` of the enum that `group` describes,
    /// and `number` is a member of it.
    #[inline]
    unsafe fn place(&self, number: u32) -> *mut u8 {
        // SAFETY: forwarded from the caller.
        unsafe { (self.group.place)(self.slot, number) }
    }

    /// Decode into the member `number`: in place if it is the one that is set,
    /// and otherwise into a new default member, which becomes the one that is
    /// set only if `f` succeeds.
    ///
    /// # Safety
    ///
    /// As for [`place`](Self::place).
    #[inline]
    unsafe fn place_with(
        &self,
        number: u32,
        f: &mut dyn FnMut(*mut u8) -> Result<(), DecodeError>,
    ) -> Result<(), DecodeError> {
        // SAFETY: forwarded from the caller.
        unsafe { (self.group.place_with)(self.slot, number, f) }
    }
}

/// Defines `merge_payload`, which decodes a oneof member's value by its
/// payload kind. Every arm decodes the value before it replaces the member
/// that is set, so a value that is rejected, or that a closed enum does not
/// know, leaves the oneof as it was. The exception is a message member that
/// is the one that is set, which is merged into as it is decoded, so a failure
/// part of the way through keeps the fields that had been merged.
macro_rules! merge_payload_dispatch {
    ($fname:ident; $($name:ident: $fam:ident $ty:ident $card:ident;)*) => {
        /// # Safety
        ///
        /// `payload_entry` is the entry of a member of the oneof `oneof`
        /// describes, with its payload's kind and aux index, and the oneof is
        /// inside the live message at `base` of the type `table` describes.
        #[inline]
        unsafe fn $fname(
            table: &MessageTable,
            payload_entry: &Entry,
            oneof: &OneofSlot<'_>,
            base: *mut u8,
            tag: Tag,
            buf: &mut &[u8],
            ctx: DecodeContext<'_>,
        ) -> Result<(), DecodeError> {
            let number = payload_entry.number();
            // SAFETY: each arm stores into the member the entry describes.
            unsafe {
                match payload_entry.kind {
                    $(Kind::$name => merge_payload_dispatch!(
                        @arm $fam $ty table payload_entry oneof number base tag buf ctx
                    ),)*
                    #[allow(unreachable_patterns)]
                    _ => unreachable!("`Table::new` checked the payload kinds"),
                }
            }
        }
    };
    (@arm Scalar $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof_scalar::<$ty>($o, $n, $tag, $buf)
    };
    (@arm Str $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof_str($o, $n, $tag, $buf)
    };
    (@arm Bytes $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof_bytes($o, $n, $tag, $buf)
    };
    (@arm Enum $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof_enum($table, $pe, $o, $base, $tag, $buf, $ctx)
    };
    (@arm Msg $ty:ident $table:ident $pe:ident $o:ident $n:ident $base:ident $tag:ident $buf:ident $ctx:ident) => {
        merge_oneof_msg($table, $pe, $o, $tag, $buf, $ctx)
    };
}

payload_kind_table!(merge_payload_dispatch, merge_payload);

/// # Safety
///
/// `oneof` is a live oneof whose member `number` has the scalar type `S`.
#[inline]
unsafe fn merge_oneof_scalar<S: Sc>(
    oneof: &OneofSlot<'_>,
    number: u32,
    tag: Tag,
    buf: &mut &[u8],
) -> Result<(), DecodeError> {
    check_wire_type(tag, S::WIRE)?;
    let value = S::read(buf)?;
    // SAFETY: the caller's contract gives the member's type.
    unsafe { *oneof.place(number).cast::<S::V>() = value };
    Ok(())
}

/// # Safety
///
/// `oneof` is a live oneof whose member `number` is a `String`.
#[inline]
unsafe fn merge_oneof_str(
    oneof: &OneofSlot<'_>,
    number: u32,
    tag: Tag,
    buf: &mut &[u8],
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    let value = types::decode_string(buf)?;
    // SAFETY: the caller's contract gives the member's type.
    unsafe { *oneof.place(number).cast::<String>() = value };
    Ok(())
}

/// # Safety
///
/// `oneof` is a live oneof whose member `number` is a `Vec<u8>`.
#[inline]
unsafe fn merge_oneof_bytes(
    oneof: &OneofSlot<'_>,
    number: u32,
    tag: Tag,
    buf: &mut &[u8],
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    let value = types::decode_bytes(buf)?;
    // SAFETY: the caller's contract gives the member's type.
    unsafe { *oneof.place(number).cast::<Vec<u8>>() = value };
    Ok(())
}

/// A closed enum's value with no variant goes to the unknown fields, like an
/// ordinary field, and leaves the member that is set as it is.
///
/// # Safety
///
/// `payload_entry` is the entry of an enum member of the live oneof `oneof`,
/// with its payload's kind and aux index, which is inside the live message at
/// `base` of the type `table` describes.
#[inline]
unsafe fn merge_oneof_enum(
    table: &MessageTable,
    payload_entry: &Entry,
    oneof: &OneofSlot<'_>,
    base: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::Varint)?;
    let raw = types::decode_int32(buf)?;
    let vt = table.enum_vt(payload_entry);
    if !(vt.accepts)(raw) {
        // SAFETY: forwarded from the caller.
        return unsafe { enum_reject(table, payload_entry, base, raw, ctx) };
    }
    // SAFETY: the member is stored in the shape `vt` was built for.
    let stored = unsafe { (vt.set)(oneof.place(payload_entry.number()), raw) };
    debug_assert!(stored, "`accepts` said the enum stores {raw}");
    Ok(())
}

/// # Safety
///
/// `payload_entry` is the entry of a message member of the live oneof `oneof`,
/// with its payload's kind and aux index, whose descriptor is for the
/// message's type and reaches it through a pointer to the message.
#[inline]
unsafe fn merge_oneof_msg(
    table: &MessageTable,
    payload_entry: &Entry,
    oneof: &OneofSlot<'_>,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    let vt = table.msg_vt(payload_entry);
    // SAFETY: the member is a message of the type `vt.child` reaches, through
    // a pointer to it, and `place_with` hands the closure a pointer to a live
    // one. A member that is already set is merged into, as a singular message
    // field is. Any other member is replaced only by a message that decoded,
    // so a failure leaves the oneof as it was.
    unsafe {
        oneof.place_with(payload_entry.number(), &mut |child| {
            vt.child.merge_sub(child, buf, ctx)
        })
    }
}

/// # Safety
///
/// `slot` points to a field of scalar type `S` in the shape `C` names.
#[inline]
unsafe fn merge_scalar<S: Sc, const C: u8>(
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
) -> Result<(), DecodeError> {
    // SAFETY: the caller's contract gives the slot's type.
    unsafe {
        match C {
            IMPLICIT | REQUIRED => {
                check_wire_type(tag, S::WIRE)?;
                *slot.cast::<S::V>() = S::read(buf)?;
            }
            OPTIONAL => {
                check_wire_type(tag, S::WIRE)?;
                *slot.cast::<Option<S::V>>() = Some(S::read(buf)?);
            }
            _ => {
                let out = &mut *slot.cast::<Vec<S::V>>();
                let wire = tag.wire_type();
                if wire == WireType::LengthDelimited {
                    let payload = take_len_delimited(buf)?;
                    S::extend(payload, out)?;
                } else if wire == S::WIRE {
                    out.push(S::read(buf)?);
                } else {
                    return Err(wire_type_mismatch(tag, WireType::LengthDelimited));
                }
            }
        }
    }
    Ok(())
}

/// Split a length-prefixed payload off the front of `buf`.
#[inline]
pub(super) fn take_len_delimited<'a>(buf: &mut &'a [u8]) -> Result<&'a [u8], DecodeError> {
    let len = decode_varint(buf)?;
    let len = usize::try_from(len).map_err(|_| DecodeError::MessageTooLarge)?;
    if buf.len() < len {
        return Err(DecodeError::UnexpectedEof);
    }
    let (payload, rest) = buf.split_at(len);
    *buf = rest;
    Ok(payload)
}

/// # Safety
///
/// `slot` points to a `String` field in the shape `C` names.
#[inline]
unsafe fn merge_str<const C: u8>(
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    // SAFETY: the caller's contract gives the slot's type.
    unsafe {
        match C {
            IMPLICIT | REQUIRED => types::merge_string(&mut *slot.cast::<String>(), buf),
            OPTIONAL => types::merge_string(
                (*slot.cast::<Option<String>>()).get_or_insert_with(String::new),
                buf,
            ),
            _ => {
                let elem = types::decode_string(buf)?;
                ctx.register_element_memory(core::mem::size_of::<String>())?;
                (*slot.cast::<Vec<String>>()).push(elem);
                Ok(())
            }
        }
    }
}

/// # Safety
///
/// `slot` points to a `Vec<u8>` field in the shape `C` names.
#[inline]
unsafe fn merge_bytes<const C: u8>(
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    // SAFETY: the caller's contract gives the slot's type.
    unsafe {
        match C {
            IMPLICIT | REQUIRED => types::merge_bytes(&mut *slot.cast::<Vec<u8>>(), buf),
            OPTIONAL => types::merge_bytes(
                (*slot.cast::<Option<Vec<u8>>>()).get_or_insert_with(Vec::new),
                buf,
            ),
            _ => {
                let elem = types::decode_bytes(buf)?;
                ctx.register_element_memory(core::mem::size_of::<Vec<u8>>())?;
                (*slot.cast::<Vec<Vec<u8>>>()).push(elem);
                Ok(())
            }
        }
    }
}

/// # Safety
///
/// `slot` points to the message field `e` describes, in the shape `C` names.
#[inline]
unsafe fn merge_msg<const C: u8>(
    table: &MessageTable,
    e: &Entry,
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    check_wire_type(tag, WireType::LengthDelimited)?;
    // SAFETY: the descriptor was built for the slot's shape and child type.
    unsafe {
        if C == REPEATED {
            let vt = table.rep_vt(e);
            ctx.register_element_memory(vt.size)?;
            let elem = (vt.push)(slot);
            let decoded = vt.child.merge_sub(elem, buf, ctx);
            if decoded.is_err() {
                // Like unrolled code, which decodes into a local and pushes
                // it only on success, leave no partial element behind.
                (vt.pop)(slot);
            }
            decoded
        } else {
            let vt = table.msg_vt(e);
            let child = (vt.place)(slot);
            vt.child.merge_sub(child, buf, ctx)
        }
    }
}

/// # Safety
///
/// `slot` points to the enum field `e` describes, in the shape `C` names,
/// inside the live message at `base`.
#[inline]
unsafe fn merge_enum<const C: u8>(
    table: &MessageTable,
    e: &Entry,
    base: *mut u8,
    slot: *mut u8,
    tag: Tag,
    buf: &mut &[u8],
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    let vt = table.enum_vt(e);
    // SAFETY: `vt` was built for the slot's shape.
    unsafe {
        match C {
            REPEATED | PACKED => {
                let wire = tag.wire_type();
                if wire == WireType::LengthDelimited {
                    let mut payload = take_len_delimited(buf)?;
                    while !payload.is_empty() {
                        let raw = types::decode_int32_packed(&mut payload)?;
                        enum_store(table, e, base, vt, slot, raw, ctx)?;
                    }
                    Ok(())
                } else if wire == WireType::Varint {
                    let raw = types::decode_int32(buf)?;
                    enum_store(table, e, base, vt, slot, raw, ctx)
                } else {
                    Err(wire_type_mismatch(tag, WireType::LengthDelimited))
                }
            }
            _ => {
                check_wire_type(tag, WireType::Varint)?;
                let raw = types::decode_int32(buf)?;
                enum_store(table, e, base, vt, slot, raw, ctx)
            }
        }
    }
}

/// Store `raw` in the enum field at `slot`; a value a closed enum rejects goes
/// to the message's unknown fields, or is dropped if it keeps none.
///
/// # Safety
///
/// `slot` is a live slot of the shape `vt` was built for, inside the live
/// message at `base` of the type `table` describes.
#[inline]
unsafe fn enum_store(
    table: &MessageTable,
    e: &Entry,
    base: *mut u8,
    vt: &EnumVt,
    slot: *mut u8,
    raw: i32,
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    // SAFETY: `slot` matches the shape `vt` was built for.
    if unsafe { (vt.set)(slot, raw) } {
        return Ok(());
    }
    // SAFETY: forwarded from the caller.
    unsafe { enum_reject(table, e, base, raw, ctx) }
}

/// Handle a value that a closed enum has no variant for: keep it as an unknown
/// field, or drop it if the message keeps none.
///
/// # Safety
///
/// `base` points to a live message of the type `table` describes.
#[inline]
unsafe fn enum_reject(
    table: &MessageTable,
    e: &Entry,
    base: *mut u8,
    raw: i32,
    ctx: DecodeContext<'_>,
) -> Result<(), DecodeError> {
    if table.unknown == NO_UNKNOWN {
        return ctx.check_unknown_field();
    }
    ctx.register_unknown_field()?;
    // SAFETY: `unknown` is the offset of the message's `UnknownFields`.
    unsafe {
        (*base.add(table.unknown as usize).cast::<UnknownFields>()).push(UnknownField {
            number: e.tag >> 3,
            data: UnknownFieldData::Varint(raw as u64),
        });
    }
    Ok(())
}