google / zerocopy

https://docs.rs/zerocopy
Apache License 2.0
1.03k stars 79 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.

Overview

Conversion Traits

Zerocopy provides four derivable traits for zero-cost conversions:

This 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 four macros for safe, zero-cost casting between types:

These macros perform compile-time alignment and size checks, but 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: Zerocopy is not an officially supported Google product.