Debian Codegraph @

third_party/rust/zerocopy/README.md

raw
1 <!-- Copyright 2022 The Fuchsia Authors
2
3 Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 This file may not be copied, modified, or distributed except according to
7 those terms.
8
9 WARNING: DO NOT EDIT THIS FILE. It is generated automatically. Edits should be
10 made in the doc comment on `src/lib.rs` or in `generate-readme.sh`.
11 -->
12
13 # zerocopy
14
15 *<span style="font-size: 100%; color:grey;">Want to help improve zerocopy?
16 Fill out our [user survey][user-survey]!</span>*
17
18 ***<span style="font-size: 140%">Fast, safe, <span
19 style="color:red;">compile error</span>. Pick two.</span>***
20
21 Zerocopy makes zero-cost memory manipulation effortless. We write `unsafe`
22 so you don't have to.
23
24 ## Overview
25
26 Zerocopy provides four core marker traits, each of which can be derived
27 (e.g., `#[derive(FromZeroes)]`):
28 - `FromZeroes` indicates that a sequence of zero bytes represents a valid
29 instance of a type
30 - `FromBytes` indicates that a type may safely be converted from an
31 arbitrary byte sequence
32 - `AsBytes` indicates that a type may safely be converted *to* a byte
33 sequence
34 - `Unaligned` indicates that a type's alignment requirement is 1
35
36 Types which implement a subset of these traits can then be converted to/from
37 byte sequences with little to no runtime overhead.
38
39 Zerocopy also provides byte-order aware integer types that support these
40 conversions; see the `byteorder` module. These types are especially useful
41 for network parsing.
42
43 [user-survey]: https://docs.google.com/forms/d/e/1FAIpQLSdzBNTN9tzwsmtyZxRFNL02K36IWCdHWW2ZBckyQS2xiO3i8Q/viewform?usp=published_options
44
45 ## Cargo Features
46
47 - **`alloc`**
48 By default, `zerocopy` is `no_std`. When the `alloc` feature is enabled,
49 the `alloc` crate is added as a dependency, and some allocation-related
50 functionality is added.
51
52 - **`byteorder`** (enabled by default)
53 Adds the `byteorder` module and a dependency on the `byteorder` crate.
54 The `byteorder` module provides byte order-aware equivalents of the
55 multi-byte primitive numerical types. Unlike their primitive equivalents,
56 the types in this module have no alignment requirement and support byte
57 order conversions. This can be useful in handling file formats, network
58 packet layouts, etc which don't provide alignment guarantees and which may
59 use a byte order different from that of the execution platform.
60
61 - **`derive`**
62 Provides derives for the core marker traits via the `zerocopy-derive`
63 crate. These derives are re-exported from `zerocopy`, so it is not
64 necessary to depend on `zerocopy-derive` directly.
65
66 However, you may experience better compile times if you instead directly
67 depend on both `zerocopy` and `zerocopy-derive` in your `Cargo.toml`,
68 since doing so will allow Rust to compile these crates in parallel. To do
69 so, do *not* enable the `derive` feature, and list both dependencies in
70 your `Cargo.toml` with the same leading non-zero version number; e.g:
71
72 ```toml
73 [dependencies]
74 zerocopy = "0.X"
75 zerocopy-derive = "0.X"
76 ```
77
78 - **`simd`**
79 When the `simd` feature is enabled, `FromZeroes`, `FromBytes`, and
80 `AsBytes` impls are emitted for all stable SIMD types which exist on the
81 target platform. Note that the layout of SIMD types is not yet stabilized,
82 so these impls may be removed in the future if layout changes make them
83 invalid. For more information, see the Unsafe Code Guidelines Reference
84 page on the [layout of packed SIMD vectors][simd-layout].
85
86 - **`simd-nightly`**
87 Enables the `simd` feature and adds support for SIMD types which are only
88 available on nightly. Since these types are unstable, support for any type
89 may be removed at any point in the future.
90
91 [simd-layout]: https://rust-lang.github.io/unsafe-code-guidelines/layout/packed-simd-vectors.html
92
93 ## Security Ethos
94
95 Zerocopy is expressly designed for use in security-critical contexts. We
96 strive to ensure that that zerocopy code is sound under Rust's current
97 memory model, and *any future memory model*. We ensure this by:
98 - **...not 'guessing' about Rust's semantics.**
99 We annotate `unsafe` code with a precise rationale for its soundness that
100 cites a relevant section of Rust's official documentation. When Rust's
101 documented semantics are unclear, we work with the Rust Operational
102 Semantics Team to clarify Rust's documentation.
103 - **...rigorously testing our implementation.**
104 We run tests using [Miri], ensuring that zerocopy is sound across a wide
105 array of supported target platforms of varying endianness and pointer
106 width, and across both current and experimental memory models of Rust.
107 - **...formally proving the correctness of our implementation.**
108 We apply formal verification tools like [Kani][kani] to prove zerocopy's
109 correctness.
110
111 For more information, see our full [soundness policy].
112
113 [Miri]: https://github.com/rust-lang/miri
114 [Kani]: https://github.com/model-checking/kani
115 [soundness policy]: https://github.com/google/zerocopy/blob/main/POLICIES.md#soundness
116
117 ## Relationship to Project Safe Transmute
118
119 [Project Safe Transmute] is an official initiative of the Rust Project to
120 develop language-level support for safer transmutation. The Project consults
121 with crates like zerocopy to identify aspects of safer transmutation that
122 would benefit from compiler support, and has developed an [experimental,
123 compiler-supported analysis][mcp-transmutability] which determines whether,
124 for a given type, any value of that type may be soundly transmuted into
125 another type. Once this functionality is sufficiently mature, zerocopy
126 intends to replace its internal transmutability analysis (implemented by our
127 custom derives) with the compiler-supported one. This change will likely be
128 an implementation detail that is invisible to zerocopy's users.
129
130 Project Safe Transmute will not replace the need for most of zerocopy's
131 higher-level abstractions. The experimental compiler analysis is a tool for
132 checking the soundness of `unsafe` code, not a tool to avoid writing
133 `unsafe` code altogether. For the foreseeable future, crates like zerocopy
134 will still be required in order to provide higher-level abstractions on top
135 of the building block provided by Project Safe Transmute.
136
137 [Project Safe Transmute]: https://rust-lang.github.io/rfcs/2835-project-safe-transmute.html
138 [mcp-transmutability]: https://github.com/rust-lang/compiler-team/issues/411
139
140 ## MSRV
141
142 See our [MSRV policy].
143
144 [MSRV policy]: https://github.com/google/zerocopy/blob/main/POLICIES.md#msrv
145
146 ## Changelog
147
148 Zerocopy uses [GitHub Releases].
149
150 [GitHub Releases]: https://github.com/google/zerocopy/releases
151
152 ## Disclaimer
153
154 Disclaimer: Zerocopy is not an officially supported Google product.