zerocopy
Zero-cost, safe conversions between Rust types and raw byte sequences via derivable traits.
Repository Health
Technical Analysis
zerocopy makes zero-cost memory manipulation safe and effortless in Rust. It provides a family of derivable traits - FromBytes, IntoBytes, FromZeros, TryFromBytes, KnownLayout, Immutable, and Unaligned - that let you reinterpret Rust types as byte slices and vice versa without copying, bounds-checking failures, or undefined behavior. The library encapsulates the unsafe code so your own code stays safe, making it a cornerstone for high-performance parsing, serialization, and systems programming.
What You Get
- Derivable
FromBytesandIntoBytestraits for zero-copy conversion between types and byte slices TryFromBytesfor fallible conversion of types with validity invariants, checked at runtimeFromZerosto safely construct a type from an all-zero byte pattern- Marker traits
KnownLayout,Immutable, andUnalignedthat unlock safe transmutation methods - Support for sized types, slices, and slice dynamically sized types (slice DSTs)
Common Use Cases
- Parsing binary network or file formats without copying or allocating
- Implementing high-performance serialization for systems and embedded code
- Safely casting byte buffers to structs in drivers and protocol implementations
Under The Hood
Architecture
zerocopy is a Cargo workspace whose primary crate lives under zerocopy/, with a companion zerocopy-derive proc-macro crate plus tooling directories (tools, ci, githooks) and internal verification crates like hermes and exocrate. The core exposes conversion and marker traits whose safety preconditions are encoded in the type system; the derive crate generates implementations only after statically proving the target type’s layout satisfies each trait’s invariants. Methods like ref_from_bytes and try_from_bytes produce typed views into byte slices with alignment and validity handled explicitly.
Tech Stack
Pure Rust targeting stable and no-std, with the derive crate built on syn and quote for macro expansion. The workspace carries a large custom CI harness, formal-reasoning tooling, and configurable feature flags. It depends on essentially no third-party runtime crates, keeping the trust surface small for a foundational library.
Code Quality
This is an exceptionally rigorous codebase: over 1,500 Rust files across the workspace with 285 files carrying tests, extensive doc comments (the README is generated from src/lib.rs), fuzzing, and Miri-based undefined-behavior checking. Every unsafe block is justified with a safety comment, and the maintainers document soundness reasoning meticulously, reflecting its use as critical infrastructure across the Rust ecosystem.
API Design
For everyday use the API is simple - add a #[derive(FromBytes, IntoBytes)] and call the generated conversion methods - so common cases require little ceremony. The depth comes when modeling types with validity invariants, where understanding the distinction between FromBytes, TryFromBytes, and the marker traits takes study; the thorough docs and release-notes upgrade guides ease that learning curve considerably.
Used by 2 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
CubeSandbox
Developer Tools · Security · AI Agents
Instant, concurrent, hardware-isolated MicroVM sandboxes for AI agents — E2B-API compatible, sub-60ms cold starts, and a built-in zero-trust egress proxy, all self-hostable at scale.