$ show anza-xyz/wincode
wincode is a Rust binary serialization library designed for high-performance, in-place deserialization and direct memory writes. It provides a drop-in, configurable alternative to bincode with the same default wire format, but adds compile-time configuration (endianness, integer encoding, length encoding, tag encoding) through const generics, eliminating runtime overhead. The library exposes `SchemaWrite` and `SchemaRead` traits with derive macros (`SchemaWrite`, `SchemaRead`, `UninitBuilder`) that generate optimized serialization/deserialization code, including `MaybeUninit`-based builders for initializing structs without intermediate copies. The architecture is layered: a `Reader`/`Writer` I/O abstraction with trusted-window optimizations (eliding bounds checks for known-size blocks), a schema layer mapping types to `TypeMeta` (static size and zero-copy eligibility), and a config layer that composes compile-time parameters. Unsafe code is pervasive but carefully isolated in unchecked slice readers/writers, zero-copy lifetimes (`ZeroCopy` trait), and unconditional `UninitBuilder` logic. Fuzzing, proptest-based property testing, Miri validation, and security audits by Neodyme, OtterSec, and Asymmetric Research ensure correctness under adversarial inputs. The library is actively maintained by Anza (Solana) and is used in performance-critical Solana infrastructure. It supports `no_std` + `alloc`, and integrates with external types like `bumpalo`, `bv`, `bytes`, `ecow`, `indexmap`, `smallvec`, and `uuid`. The codebase exhibits strong engineering craftsmanship but requires a high MSRV (1.89) and has a heavy reliance on unsafe that demands rigorous auditing. Overall, it is a well-architected, production-grade serialization library.
The LLM identified 3 files worth highlighting for reuse:
A generic `Cursor<T>` wrapper that implements both `Reader` and `Writer` traits for byte buffers (including `&[u8]`, `&mut [u8]`, `Vec<u8>`). It maintains a read/write position and supports the `as_trusted_for` pattern to create unchecked sub-readers/writers for zero-copy bulk operations (e.g., writing arrays, copying slices). This component is self-contained, operates solely through the `io` module's trait definitions, and can be extracted as a standalone I/O utility for any project requiring efficient, position-aware buffer access.
Defines the `SeqLen` trait hierarchy for configurable length encoding of sequences. Includes `BincodeLen<LIMIT>` (varint or fixint based on config), `FixIntLen<T, LIMIT>` (fixed-size integer like `u16`), and `UseIntLen<T, LIMIT>` (always varint but with a preallocation limit). These types implement `SeqLen` and provide `read_len`/`write_len` methods. This pattern enables field-level overrides of length encoding (e.g., using 2-byte length for a specific vector) and is decoupled from any specific schema, making it reusable for any binary format that needs pluggable length schemes.
Provides adapter types (`FromInto`, `DefaultOnEmptyRead`, `Discard`, `DiscardSeq`) that implement `SchemaWrite`/`SchemaRead` by delegating to inner types with modifications. `FromInto` maps between different Rust types (e.g., a newtype and its inner) using `Into`/`From`. `DefaultOnEmptyRead` returns a default value when no data is present (useful for optional fields). `Discard`/`DiscardSeq` consume bytes during read without storing them, enabling field deprecation without breaking wire format. These components are isolated, require no internal state beyond the wrapped type, and can be reused in any schema-based serialization framework.