pack-io 2.0.0

Compact binary wire format with schema evolution and zero-copy deserialization for Rust. The serialization substrate under network-protocol and Hive DB.
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
<h1 align="center">
    <img width="99" alt="Rust logo" src="https://raw.githubusercontent.com/jamesgober/rust-collection/72baabd71f00e14aa9184efcb16fa3deddda3a0a/assets/rust-logo.svg">
    <br><b>pack-io</b><br>
    <sub><sup>WIRE FORMAT — NORMATIVE SPECIFICATION</sup></sub>
</h1>
<div align="center">
    <sup>
        <a href="../README.md" title="Project Home"><b>HOME</b></a>
        <span>&nbsp;│&nbsp;</span>
        <a href="../CHANGELOG.md" title="Changelog"><b>CHANGELOG</b></a>
        <span>&nbsp;│&nbsp;</span>
        <a href="./API.md" title="API Reference"><b>API</b></a>
        <span>&nbsp;│&nbsp;</span>
        <span>WIRE FORMAT</span>
    </sup>
</div>
<br>

> **Status: stable; spec version 1.3 (pack-io v2.0.0).** This document is
> the normative specification for the pack-io wire format. It is written
> so that a reader who has never seen the source code can implement a
> compatible encoder or decoder.
>
> The format has been stable since pack-io 1.0: a pack-io 2.x decoder
> reads every encoding a 1.x or 2.x encoder produced for valid data, and
> every encoder produces the same bytes for the same value. pack-io 2.0
> is a new major version of the *library* (decoder limits, derive
> validation), not of the format. Revisions of this document only add
> encodings for new types, add opt-in encodings, and tighten what
> decoders must reject (see §7); a change that breaks an existing
> encoding requires a new major version of both.

---

## 1. Conventions

- The keywords **MUST**, **MUST NOT**, **SHOULD**, and **MAY** are used in
  the [RFC 2119](https://datatracker.ietf.org/doc/html/rfc2119) sense.
- Byte values are written in hexadecimal with a `0x` prefix.
- Bit positions are numbered with `0` being the least significant.
- All multi-byte numeric fields are **little-endian** unless stated
  otherwise.
- "Concatenated" means the byte sequences are placed back-to-back with no
  framing bytes, padding, or alignment.

---

## 2. Primitive encodings

### 2.1 Unsigned integers

| Type      | Encoding                                            |
|-----------|-----------------------------------------------------|
| `u8`      | 1 byte, value as-is.                                |
| `u16`     | LEB128 unsigned varint (1–3 bytes).                 |
| `u32`     | LEB128 unsigned varint (1–5 bytes).                 |
| `u64`     | LEB128 unsigned varint (1–10 bytes).                |
| `u128`    | LEB128 unsigned varint (1–19 bytes).                |
| `usize`   | Encoded as `u64`. Decoders MUST reject values that exceed the receiver's `usize::MAX` with **`IntegerOutOfRange`**. |

**LEB128 unsigned varint:** little-endian base-128 encoding. Each byte
carries seven bits of payload in its low seven bits and a continuation flag
in bit 7 (`1` = more bytes follow, `0` = this is the last byte).

Encoders MUST emit the shortest possible representation. A varint that
includes redundant zero high bytes (a multi-byte varint whose final byte
is `0x00`, such as `[0x80, 0x00]` for `0`) is **non-canonical**:

- Every decoder MUST reject a varint wider than the legal width (10 bytes
  for `u64`, 19 for `u128`) as **`VarintOverflow`**.
- A decoder in **canonical mode** (§4.3) MUST reject every non-canonical
  varint as **`NonCanonical`**.
- A lenient decoder MAY accept a non-canonical varint that fits the legal
  width and decode it to its numeric value. pack-io decoders are lenient
  by default, as 1.0 decoders were.

For a `u64` varint, the tenth byte MAY only set bit 0 — bits 1–7 of the
tenth byte are required to be zero, otherwise the value would overflow
`u64`. The same restriction applies to byte 19 of a `u128` varint (only
bits 0–1 of byte 19 may be set).

### 2.2 Signed integers

| Type     | Encoding                                                  |
|----------|-----------------------------------------------------------|
| `i8`     | 1 byte, two's-complement value as-is.                     |
| `i16`    | ZigZag → LEB128 (`u16`-sized payload, 1–3 bytes).         |
| `i32`    | ZigZag → LEB128 (`u32`-sized payload, 1–5 bytes).         |
| `i64`    | ZigZag → LEB128 (`u64`-sized payload, 1–10 bytes).        |
| `i128`   | ZigZag → LEB128 (`u128`-sized payload, 1–19 bytes).       |
| `isize`  | Encoded as `i64`. Decoders MUST reject values that exceed the receiver's `isize` range with **`IntegerOutOfRange`**. |

**ZigZag mapping:** maps signed integers `0, -1, 1, -2, 2, …` to unsigned
`0, 1, 2, 3, 4, …` so the absolute magnitude determines the varint length.
For an N-bit signed value:

```
zz(n) = (n << 1) XOR (n >> (N-1))
n     = (zz >> 1) XOR -(zz & 1)
```

### 2.3 Boolean

| Type   | Encoding                                                          |
|--------|-------------------------------------------------------------------|
| `bool` | 1 byte. `0x00` = `false`, `0x01` = `true`. Any other byte is **invalid** and decoders MUST reject it as **`InvalidBool { byte }`**. |

### 2.4 Floating-point

| Type   | Encoding                                                       |
|--------|----------------------------------------------------------------|
| `f32`  | 4 bytes, IEEE 754 binary32 bit pattern, little-endian.         |
| `f64`  | 8 bytes, IEEE 754 binary64 bit pattern, little-endian.         |

The bit pattern is preserved exactly — NaN, ±Inf, subnormals, and signed
zeros all round-trip bit-for-bit. Consumers that need IEEE equality on the
decoded value MUST compare via `to_bits()` (since `NaN != NaN` by spec).

### 2.5 Unit type

| Type   | Encoding                                                       |
|--------|----------------------------------------------------------------|
| `()`   | Zero bytes.                                                    |
| `PhantomData<T>` | Zero bytes.                                          |

### 2.6 Characters

| Type   | Encoding                                                       |
|--------|----------------------------------------------------------------|
| `char` | The Unicode scalar value as a `u32` LEB128 varint (1–3 bytes). Decoders MUST reject surrogates (`0xD800`–`0xDFFF`) and values above `0x10FFFF` as **`InvalidValue`**. |

---

## 3. Length-prefixed and compound types

### 3.1 Strings (`String` / `&str`)

```
String  ::= varint(byte_length) bytes(byte_length)
```

- `byte_length` is the number of UTF-8 bytes that follow (not the codepoint
  count).
- `bytes(byte_length)` MUST be valid UTF-8. Decoders MUST reject invalid
  UTF-8 with **`InvalidUtf8`**.
- The empty string is encoded as `varint(0)` — a single `0x00` byte — with
  no following payload.

### 3.2 Fixed-size arrays (`[T; N]`)

```
[T; N]  ::= T_encoding × N
```

- `N` is part of the type, not the wire shape. There is no length prefix.
- Elements are encoded in array index order (`[0]`, `[1]`, …, `[N-1]`).

### 3.3 Variable-length lists (`Vec<T>` / `&[T]`)

```
Vec<T>  ::= varint(element_count) T_encoding × element_count
```

- `element_count` is the number of `T` values that follow.
- Decoders MUST validate `element_count` against the configured allocation
  cap (see §6.1) before allocating per-element capacity, and MUST reject
  counts that exceed it with **`InvalidLength`**.
- Decoders MUST NOT size an allocation from `element_count` alone: a
  count that passes the cap is not proof the input holds that many
  elements (§6.1).
- An element type whose encoding is empty (a zero-sized type, a `Box` or
  `Rc` of one, a struct whose fields are all skipped) is not backed by
  input at all. Decoders MUST bound the memory such elements occupy
  across the whole decode (§6.1).
- `VecDeque<T>`, `Box<[T]>`, `Rc<[T]>` and `Arc<[T]>` use this encoding,
  front to back.

### 3.4 Tuples

```
(T0, T1, …, Tn)  ::= T0_encoding T1_encoding … Tn_encoding
```

- Fields are encoded in declaration order, concatenated.
- No length prefix, no framing bytes between fields.
- The unit tuple `()` is zero bytes (see §2.5).

### 3.5 Optionals (`Option<T>`)

```
Option<T>  ::= None_tag
             | Some_tag T_encoding

None_tag  ::= 0x00
Some_tag  ::= 0x01
```

- A tag byte outside `{0x00, 0x01}` is **invalid** and decoders MUST
  reject it as **`InvalidTag { kind: "Option", tag }`**.

### 3.6 Results (`Result<T, E>`)

```
Result<T, E>  ::= Ok_tag  T_encoding
                | Err_tag E_encoding

Ok_tag   ::= 0x00
Err_tag  ::= 0x01
```

- A tag byte outside `{0x00, 0x01}` is **invalid** and decoders MUST
  reject it as **`InvalidTag { kind: "Result", tag }`**.

### 3.7 Enums

```
Enum   ::= varint(variant_index) variant_encoding
```



- `variant_index` is an unsigned LEB128 varint that selects the variant
  by its source-code declaration order, starting at `0`. The varint
  representation is the same as for `u64`, but the value MUST fit in
  `u32` (no real-world Rust enum has 4 billion variants).
- `variant_encoding` is the encoding of the variant's fields in source
  declaration order, concatenated, with no inter-field framing. Unit
  variants contribute zero bytes after the tag.
- A `variant_index` that does not correspond to any declared variant of
  the target type is **invalid** and decoders MUST reject it as
  **`UnknownVariant { kind, index }`**, where `kind` is the type's name
  and `index` is the offending value.

The variant index space is **source-order dependent** unless the derive
assigns explicit indices (`#[pack_io(index = N)]`; a variant without one
takes the previous variant's index plus one). Adding a variant in the
middle of an enum declaration without explicit indices is a
**wire-format-breaking change for that enum**, even though it does not
break the codec spec. Append new variants to the end of the declaration,
or pin indices explicitly, to maintain compatibility.

This encoding lands in `v0.4.0` and is the format the
`#[derive(Serialize, Deserialize)]` macros emit for enums.

### 3.8 Versioned structs

```
VersionedStruct  ::= varint(version) varint(body_len) body
body             ::= slot_encoding *  (concatenated, no padding)
```

- `version` is the type's schema version, an unsigned `u32` LEB128 varint.
  Schema versions start at `1`; `0` is reserved and decoders MUST reject
  it (rationale: distinguishes a versioned payload from an accidental
  all-zeros buffer).
- `body_len` is an unsigned `u64` LEB128 varint giving the byte length of
  the body that follows. Decoders MUST validate `body_len` against the
  configured allocation cap (§6.1) **before** reading the body, and MUST
  refuse `body_len > max_alloc`. When the remaining input length is
  known, decoders MUST also refuse `body_len` larger than it.
- The body MUST be decoded under the same allocation cap, depth limit
  and canonical-mode setting as the enclosing value (§6).
- `body` is the concatenated encodings of every field that **has a slot
  at this version**, in source declaration order.

A field *has a slot at version V* iff:

```
slot(field, V)  ⟺  field.since ≤ V  AND  (field.removed is None OR V < field.removed)
```

Where `field.since` defaults to `1` (always present from the first
version) and `field.removed` defaults to `None`. A field's `since` MUST
NOT exceed the type's own version (such a field could never be written;
pack-io's derive rejects it at compile time).

A field may additionally be **retired** at version `R` (`R > since`):
from `R` on the slot is still present, but encoders MUST write the
field's default value into it and decoders reading a payload at `W ≥ R`
MUST decode the slot and then discard the value, yielding the default.
Retirement does not change which slots exist, so it is invisible to
every reader that predates it — they read the default value.

Field *removal* (`removed = N`, the 1.x `deprecated = N` attribute)
deletes the slot from version `N` on. It is the encoding pack-io 1.x
wrote for deprecated fields and remains fully supported so those payloads
keep decoding. Retirement is the opt-in encoding added in spec 1.3;
pack-io 2.0's derive requires every field that leaves the schema to say
which of the two it uses.

**Cross-version decode contract.** A decoder for type `T` at *known
version* `K` reading a payload encoded at *wire version* `W`:

| Relationship | Behaviour                                                                                                  |
|--------------|------------------------------------------------------------------------------------------------------------|
| `W = K`      | Read every field that is live at `W`. Equivalent to the writer's encode.                                   |
| `W < K`      | Read every field that is live at `W`. For each `T` field with `since > W` (added after `W`), construct via `Default::default()`. |
| `W > K`      | Read every field that is live at `W` from the decoder's perspective — i.e. every field of `T` that the decoder knows is live up to `K`. Trailing bytes inside `body` are silently discarded; the length prefix bounds the read so the next message is not consumed. |

The third row is the property that makes append-only schema evolution
work: a newer producer can ship a `T` payload to an older consumer without
the consumer needing to know what was added. As long as new fields are
**only appended** to the end of the struct (and `since = N` is set
correctly), older consumers read what they understand and ignore the
rest.

**Rules for evolving a versioned struct compatibly:**

1. The struct-level `version` MUST be incremented by `1` each time a new
   field is appended, retired, or removed.
2. New fields MUST be **appended** to the end of the struct declaration
   and carry `#[pack_io(since = N)]` where `N` is the new struct version.
3. A field that leaves the schema stays in the struct declaration and
   is marked either `#[pack_io(retired = N)]` or `#[pack_io(removed = N)]`.
   **Retire** it unless saving its bytes matters: a retired slot keeps
   every reader aligned, whatever its version. **Removal** deletes bytes
   from the middle of the body, so it MUST be rolled out **reader-first**:
   every reader of the type must already declare `removed = N` (a reader
   at version `N - 1` may do so) before any writer emits version `N`. A
   reader that predates the removal decodes the next field's bytes into
   the removed field without any error, because nothing on the wire marks
   the removal.
4. Reordering fields, removing fields outright, or changing a field's
   type are all wire-format-breaking changes for that struct and require
   a major bump.
5. Fields with `since > 1` MUST have a `Default` impl — decoders use
   `Default::default()` for fields missing from older payloads.

Non-versioned structs (those without `#[pack_io(version = N)]`) use the
plain field-concatenation encoding from `v0.4.0`; the choice between
versioned and non-versioned is per-type and cannot be changed without
breaking the wire format for that type.

Encoders at struct version `V` MUST NOT emit fields with `removed ≤ V`
(the slot is gone from `V`'s wire shape). Decoders reading a payload
that was encoded at `W < removed` MUST decode the field's value
normally; payloads at `W ≥ removed` produce `Default::default()` (or
the field's declared default) for the field. For a retired field,
encoders at `V ≥ retired` write the default into the slot, and decoders
at `W ≥ retired` decode the slot and produce the default.

A decoder in canonical mode (§4.3) accepts a versioned struct only at
its own version: it MUST reject, with **`NonCanonical`**, a payload whose
`W ≠ K` (an older payload is a second encoding of the defaulted value,
and the newer fields of a `W > K` payload cannot be checked, so any bytes
could follow), unread bytes left in the body, and a retired slot whose
bytes differ from the encoding of the field's default. Lenient decoders
follow the table above for every `W`.

This encoding lands in `v0.5.0` and is the format the
`#[derive(Serialize, Deserialize)]` macros emit for types carrying
`#[pack_io(version = N)]` (since `v2.0.0`, `#[derive(DeserializeView)]`
reads it too). Companion runtime helper:
`pack_io::peek_version(&bytes) -> Result<u32>` reads only the leading
`version` varint, leaving the rest of the buffer untouched. It does not
validate it: a leading `0` peeks as `0`, which every decoder rejects.

### 3.9 Additional standard types

Added in spec 1.3 (pack-io `v2.0.0`). Each is either a new encoding for a
type 1.0 did not support, or a type that shares an existing encoding.

| Type | Encoding |
|------|----------|
| `char` | §2.6 |
| `PhantomData<T>` | zero bytes (§2.5) |
| `NonZeroU8` … `NonZeroU128`, `NonZeroUsize`, `NonZeroI8` … `NonZeroIsize` | identical to the underlying integer; decoders MUST reject `0` as **`InvalidValue`** |
| `Duration` | `varint(secs: u64) ++ varint(subsec_nanos: u32)`; decoders MUST reject `subsec_nanos >= 1_000_000_000` as **`InvalidValue`** |
| `Box<str>`, `Rc<str>`, `Arc<str>`, `Cow<str>` | identical to `String` (§3.1) |
| `Box<[T]>`, `Rc<[T]>`, `Arc<[T]>`, `VecDeque<T>` | identical to `Vec<T>` (§3.3) |
| `Box<T>`, `Rc<T>`, `Arc<T>`, `Cow<T>` | identical to `T` (no framing; sharing is not preserved) |
| `bytes::Bytes`, `bytes::BytesMut` | identical to `Vec<u8>` |

Fields marked `#[pack_io(skip)]` by the derive contribute zero bytes; a
field encoded through `#[pack_io(with = "...")]` contributes whatever the
helper writes. Both are per-type choices like field order: changing them
changes that type's encoding.

---

## 4. Maps and sets

Maps and sets share the same wire shape:

```
Map<K, V>  ::= varint(entry_count) entry × entry_count
entry      ::= K_encoding V_encoding

Set<T>     ::= varint(element_count) T_encoding × element_count
```

### 4.1 Canonical ordering (the determinism contract)

The element / entry sequence MUST be sorted **lexicographically by the
encoded key bytes** (for maps, the key; for sets, the element itself).

**Sort rule:**

1. For each entry / element, compute its encoding into a temporary buffer.
2. Order entries by lexicographic byte comparison of the encoded **key**
   portion (for maps) or the encoded element (for sets). Earlier-differing
   bytes win; a shorter byte sequence whose contents are a prefix of a
   longer one is the smaller value.
3. Emit `varint(count)` followed by the encoded entries in the sorted
   order.

Encoders MUST follow this rule. Decoders MAY accept inputs whose entries
are not in canonical order (lenient decode), but SHOULD reject them in
contexts that depend on byte-determinism (signature verification,
content-addressing).

**Why sort by encoded bytes?** It is the only ordering that gives
`HashMap<K, V>` and `BTreeMap<K, V>` (over the same logical data) the same
wire-format output, regardless of `K`'s `Ord` impl or the absence thereof.
Workflows that hash or sign the encoded payload survive any later switch
between concrete map types.

**Duplicate keys / elements:** the canonical encoding produced by the
sort step contains no duplicates (the source collection already
de-duplicates). A lenient decoder accepts duplicates (last-write-wins for
maps, first-occurrence for sets) without signalling an error. Producers
MUST NOT emit duplicates; a decoder in canonical mode (§4.3) MUST reject
them.

### 4.2 Supported map / set types in the standard library

| Type                    | Sort by                    | Decode collects into            |
|-------------------------|----------------------------|---------------------------------|
| `BTreeMap<K, V>`        | encoded-key bytes          | `BTreeMap<K, V>` (`K: Ord`)     |
| `BTreeSet<T>`           | encoded-element bytes      | `BTreeSet<T>` (`T: Ord`)        |
| `HashMap<K, V, S>`      | encoded-key bytes          | `HashMap<K, V, S>` (`K: Hash + Eq`, `S: BuildHasher + Default`) |
| `HashSet<T, S>`         | encoded-element bytes      | `HashSet<T, S>` (`T: Hash + Eq`, `S: BuildHasher + Default`) |

`HashMap` and `HashSet` encode through the `std` feature (default-on); they
are unavailable in `no_std` builds.

### 4.3 Canonical mode

A decoder in **canonical mode** accepts exactly the byte strings an
encoder following this specification produces for the decoding type at
its own version, so that equal bytes mean equal values and every value
has one accepted encoding. In addition to the checks every decoder
performs, it MUST reject, with **`NonCanonical`**:

1. any non-canonical varint (§2.1), wherever it appears (integers,
   length prefixes, counts, enum indices, versions, body lengths);
2. map entries whose encoded keys are not strictly increasing, and set
   elements whose encodings are not strictly increasing (§4.1) — this
   rejects duplicates;
3. a versioned struct whose wire version `W` differs from the decoding
   type's version `K` (§3.8);
4. unread bytes in a versioned body;
5. a retired slot (§3.8) whose bytes differ from the encoding of the
   field's default.

A canonical decoder therefore reads only payloads written at its own
schema version. Decode older or newer payloads leniently, then re-encode
them if a canonical form is needed.

pack-io decoders are lenient by default and enter canonical mode through
`Config::with_canonical(true)`. Canonical mode needs access to the raw
encoded keys; pack-io supports it on its in-memory decoder. The guarantee
covers every type built from pack-io's own impls and derives; a
hand-written `Deserialize` impl or a `with` helper that accepts several
encodings of one value (say, a number parsed from text that may carry
leading zeros) widens what is accepted, and the `serde` adapter can check
set order only for fields marked `sorted_set` (serde has no set type).

---

## 5. End-of-message handling

The wire format itself does **not** carry an end-of-message marker. Whether
a payload contains a single value or several depends on the surrounding
framing:

- The Tier-1 [`decode`](API.md#decode) entry point requires the input slice
  to be fully consumed; trailing bytes are rejected with
  **`TrailingBytes`**. Use this for single-value payloads.
- The Tier-2 [`Decoder`](API.md#decoder) / [`IoDecoder`](API.md#iodecoder)
  do not enforce full consumption — repeated `read()` calls extract
  successive values from the same buffer. Use this for multi-value
  streams or for length-framed protocols where the framing is handled
  externally.

---

## 6. Error categories

Decoders surface failures via the `SerialError` enum. The taxonomy below is
normative — implementers SHOULD map their own error types onto these
categories for interoperability.

| Category               | When raised                                                                                |
|------------------------|--------------------------------------------------------------------------------------------|
| `UnexpectedEof`        | Decoder needed more bytes than the input contained.                                        |
| `InvalidLength`        | A length prefix exceeded the buffer or the configured allocation cap (§6.1).               |
| `VarintOverflow`       | A LEB128 varint exceeded its target width.                                                 |
| `IntegerOutOfRange`    | A decoded integer did not fit in the requested narrower target.                            |
| `InvalidBool`          | A boolean byte was neither `0x00` nor `0x01`.                                              |
| `InvalidUtf8`          | A length-prefixed byte run was not valid UTF-8 when decoding a `String`.                   |
| `InvalidTag`           | An `Option` / `Result` tag byte was outside `{0x00, 0x01}`.                                |
| `TrailingBytes`        | A strict decode call left bytes unread.                                                    |
| `UnknownVariant`       | An enum variant index did not correspond to any declared variant of the target type (§3.7).|
| `Io` *(std-only)*      | The underlying `Read` / `Write` returned an `std::io::Error`.                              |
| `DepthLimitExceeded`   | Nesting exceeded the configured depth limit (§6.2).                                        |
| `NonCanonical`         | Canonical mode rejected a non-canonical encoding (§4.3).                                   |
| `InvalidVersion`       | A versioned struct carried the reserved version `0` (§3.8).                                |
| `InvalidValue`         | A value violated its type's invariant (`char`, `NonZero*`, `Duration`; §3.9).             |

### 6.1 Allocation cap

Decoders MUST enforce a per-value maximum allocation size (`max_alloc`).
For every length-prefixed value (`String`, `Vec<u8>`, `Vec<T>` and the
element count of a collection), the decoder checks that the declared length
is less than or equal to `max_alloc` before performing any allocation.

A length that exceeds `max_alloc` is rejected with `InvalidLength` —
**before** the decoder allocates a single byte. This is the primary
defence against a hostile `length = u64::MAX` payload.

A declared length that passes the cap is still only a claim. Decoders
MUST NOT allocate more than the input can back:

- when the remaining input length is known (an in-memory buffer), a
  declared byte length larger than it MUST be rejected before allocating,
  and collection pre-allocation MUST be bounded by it;
- when it is not known (a stream), buffers MUST grow in bounded steps as
  bytes actually arrive, never to the declared length up front.

Elements whose encoding is empty (§3.3) need memory no input backs.
Decoders MUST bound it across the whole decode, not per collection, or a
few bytes of nested counts multiply it. pack-io charges each collection
element that consumed no input `size_of` the element (at least one byte)
against `max_alloc`, summed over the decode (versioned bodies included),
and rejects the decode with `InvalidLength` once the total would exceed
it. Heap memory such an element owns itself (an `Rc` control block) is
not counted, so their peak memory is a small multiple of `max_alloc`.

The default value of `max_alloc` is implementation-defined; pack-io ships
`1 GiB` as the default and exposes
[`Config::with_max_alloc`](API.md#config) for tighter caps.

### 6.2 Depth limit

Decoders MUST bound the nesting of recursive types so that hostile input
cannot exhaust the stack, and report the limit as `DepthLimitExceeded`.
The limit is implementation-defined; pack-io counts one level per
derived struct or enum and defaults to `128`
([`Config::with_max_depth`](API.md#config)).

---

## 7. Versioning of this document

| Version | Changes                                                                                        |
|---------|------------------------------------------------------------------------------------------------|
| `1.0`   | Initial freeze, shipped with pack-io `v0.3.0`. All sections above apply.                       |
| `1.1`   | Additive: enums (§3.7) and `UnknownVariant` error (§6). Shipped with pack-io `v0.4.0`. No existing encoding changes — payloads valid under `1.0` remain valid under `1.1`. |
| `1.2`   | Additive: versioned structs (§3.8). Shipped with pack-io `v0.5.0`. Plain (non-versioned) structs encode exactly as before; versioned structs are a new per-type opt-in via `#[pack_io(version = N)]`. |
| `1.3`   | Shipped with pack-io `v2.0.0`. Additive: encodings for `char`, `Duration`, `NonZero*`, `Box<T>` and the types in §3.9; explicit enum indices (§3.7); retired fields, an opt-in alternative to removal that keeps the slot (§3.8); canonical mode, which accepts a versioned struct only at the reader's own version (§3.8, §4.3); a whole-decode bound on elements with an empty encoding (§3.3, §6.1); depth limit (§6.2); `DepthLimitExceeded`, `NonCanonical`, `InvalidVersion`, `InvalidValue` (§6). Clarified: non-canonical varints (§2.1), input-bounded allocation (§3.3, §6.1), body limits and the reader-first rule for removed fields (§3.8). Every byte string an encoder produced under `1.2` is still produced identically and decodes to the same value; the only inputs `1.3` decoders newly reject are ones `1.2` already declared invalid (version `0`) or that exceed a decoder limit. |

Future revisions to this document will be additive — new types, new
optional headers, or new error categories — and MUST preserve the contracts
above. A revision that breaks any of §2 — §4 is a wire-format-breaking
change and requires a major version bump (`2.x`).

---

<sub>Copyright &copy; 2026 <strong>James Gober</strong>. All rights reserved.</sub>