zerocopy

Zero-cost, safe conversions between Rust types and raw byte sequences via derivable traits.

Library
Cargo
v0.8.56
2,550stars
BSD-2-Clause OR Apache-2.0 OR MIT

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
88/100Excellent
Development Activity100
Maintenance100
Community60
Maturity52
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
82/100Excellent
Architecture90
Code Quality92
Innovation88
Learning Curve60

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 FromBytes and IntoBytes traits for zero-copy conversion between types and byte slices
  • TryFromBytes for fallible conversion of types with validity invariants, checked at runtime
  • FromZeros to safely construct a type from an all-zero byte pattern
  • Marker traits KnownLayout, Immutable, and Unaligned that 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search