google / zerocopy

https://discord.gg/MAvWH2R6zk
Apache License 2.0
1.61k stars 105 forks source link
memory memory-safety performance rust zero-copy

zerocopy

Need more out of zerocopy? Submit a customer request issue!

Fast, safe, <span style="color:red;">compile error. Pick two.

Zerocopy makes zero-cost memory manipulation effortless. We write unsafe so you don't have to.

Thanks for using zerocopy 0.8! For an overview of what changes from 0.7, check out our release notes, which include a step-by-step guide for upgrading from 0.7.

Have questions? Need help? Ask the maintainers on GitHub or on Discord!

Overview

Conversion Traits

Zerocopy provides four derivable traits for zero-cost conversions:

These traits support sized types, slices, and slice DSTs.

Marker Traits

Zerocopy provides three derivable marker traits that do not provide any functionality themselves, but are required to call certain methods provided by the conversion traits:

You should generally derive these marker traits whenever possible.

Conversion Macros

Zerocopy provides six macros for safe casting between types:

These macros perform compile-time size and alignment checks, meaning that unconditional casts have zero cost at runtime. Conditional casts do not need to validate size or alignment runtime, but do need to validate contents.

These macros cannot be used in generic contexts. For generic conversions, use the methods defined by the conversion traits.

Byteorder-Aware Numerics

Zerocopy provides byte-order aware integer types that support these conversions; see the byteorder module. These types are especially useful for network parsing.

Cargo Features

Security Ethos

Zerocopy is expressly designed for use in security-critical contexts. We strive to ensure that that zerocopy code is sound under Rust's current memory model, and any future memory model. We ensure this by:

For more information, see our full soundness policy.

Relationship to Project Safe Transmute

Project Safe Transmute is an official initiative of the Rust Project to develop language-level support for safer transmutation. The Project consults with crates like zerocopy to identify aspects of safer transmutation that would benefit from compiler support, and has developed an experimental, compiler-supported analysis which determines whether, for a given type, any value of that type may be soundly transmuted into another type. Once this functionality is sufficiently mature, zerocopy intends to replace its internal transmutability analysis (implemented by our custom derives) with the compiler-supported one. This change will likely be an implementation detail that is invisible to zerocopy's users.

Project Safe Transmute will not replace the need for most of zerocopy's higher-level abstractions. The experimental compiler analysis is a tool for checking the soundness of unsafe code, not a tool to avoid writing unsafe code altogether. For the foreseeable future, crates like zerocopy will still be required in order to provide higher-level abstractions on top of the building block provided by Project Safe Transmute.

MSRV

See our MSRV policy.

Changelog

Zerocopy uses GitHub Releases.

Disclaimer

Disclaimer: This is not an officially supported Google product.