Installation
Don't depend on pack-io-derive directly. Depend on pack-io with the derive feature:
[]
= { = "2", = ["derive"] }
What the macros do
Three derives, all working on structs (named-field, tuple, unit) and enums (any variant shape), generic over type parameters:
use ;
Attributes
Everything under #[pack_io(...)] is validated: unknown, duplicated or misplaced attributes are compile errors pointing at the attribute.
| Where | Attribute | Effect |
|---|---|---|
| struct | version = N |
versioned frame varint(N) ++ varint(body_len) ++ body; N >= 1 |
| field of a versioned struct | since = N |
field added at version N (N <= version) |
| field of a versioned struct | retired = N |
from version N the slot carries the default and readers ignore it; safe with every reader (N <= version; canonical decoders check the slot, so the type must also be serializable) |
| field of a versioned struct | removed = N |
field's bytes leave the wire at version N (the 1.x deprecated encoding; roll out readers first, which a reader may announce with N = version + 1) |
| any field | skip |
never encoded; decoded as its default |
| any field | default / default = "path" |
value used when the field is absent (with skip, since, retired or removed) |
| any field | with = "module" |
encode / decode through module::serialize / module::deserialize (and module::deserialize_view for views) |
| struct / enum | crate = "path" |
path to pack_io in generated code, for crates that re-export it |
| enum variant | index = N |
explicit variant tag; later variants continue from N + 1 |
| Derive | Implements | Notes |
|---|---|---|
Serialize |
pack_io::Serialize |
Generic over the pack_io::Encode behaviour trait. |
Deserialize |
pack_io::Deserialize |
Generic over the pack_io::Decode behaviour trait. Owning decode. |
DeserializeView |
pack_io::DeserializeView<'a> |
Zero-copy. At most one lifetime parameter; lifetime-less types work for every lifetime. |
Generated code is generic over Encode / Decode, so the same impl drives both the in-memory codec and the streaming IoEncoder<W> / IoDecoder<R> in the parent crate.
Field order in the source code is the encoded byte order. For enums, a varint(variant_index) prefix is emitted first, in source-declaration order starting at 0 unless pinned with index. Rust discriminants (A = 5) are not encoded; one that differs from the variant's tag is a compile error unless the variant has index. Append new variants to the end of an enum declaration (or pin indices) to keep the wire shape backward-compatible — inserting in the middle shifts every later variant's index.
Generated code uses only ::core and the pack_io path, so it compiles in #![no_std] crates. Every derived decoder counts one level against Config::max_depth, which is what keeps hostile input for recursive types (for example next: Option<Box<Self>>) from overflowing the stack.
1.x's deprecated = N is a compile error since 2.0: write removed = N to keep the exact 1.x bytes, or retired = N for the encoding that stays safe with readers that predate the change. See the migration guide.
See the full wire-format specification and the API reference in the parent repository.
Compatibility
- MSRV: Rust 1.85 (2024 edition).
- Version pinning:
pack-iodepends onpack-io-derivewith an exact=X.Y.Zconstraint so mismatched derive output cannot leak acrosspack-iorevisions. The two crates are always released together with the same version.