Files
phy/bytemuck/derive.Zeroable.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

89 lines
7.2 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Derive the `Zeroable` trait for a type."><title>Zeroable in bytemuck - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="bytemuck" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc derive"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../bytemuck/index.html">bytemuck</a><span class="version">1.21.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Zeroable</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li><li><a href="#custom-bounds" title="Custom bounds">Custom bounds</a><ul><li><a href="#examples" title="Examples">Examples</a></li></ul></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate bytemuck</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">bytemuck</a></span><h1>Derive Macro <span class="derive">Zeroable</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/bytemuck_derive/lib.rs.html#237-239">Source</a> </span></div><pre class="rust item-decl"><code>#[derive(Zeroable)]
{
<span class="comment">// Attributes available to this derive:</span>
#[bytemuck]
#[zeroable]
}
</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Derive the <code>Zeroable</code> trait for a type.</p>
<p>The macro ensures that the type follows all the the safety requirements
for the <code>Zeroable</code> trait.</p>
<p>The following constraints need to be satisfied for the macro to succeed on a
struct:</p>
<ul>
<li>All fields in the struct must implement <code>Zeroable</code></li>
</ul>
<p>The following constraints need to be satisfied for the macro to succeed on
an enum:</p>
<ul>
<li>The enum has an explicit <code>#[repr(Int)]</code>, <code>#[repr(C)]</code>, or <code>#[repr(C, Int)]</code>.</li>
<li>The enum has a variant with discriminant 0 (explicitly or implicitly).</li>
<li>All fields in the variant with discriminant 0 (if any) must implement
<code>Zeroable</code></li>
</ul>
<p>The macro always succeeds on unions.</p>
<h3 id="example"><a class="doc-anchor" href="#example">§</a>Example</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Copy, Clone, Zeroable)]
#[repr(C)]
</span><span class="kw">struct </span>Test {
a: u16,
b: u16,
}</code></pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Copy, Clone, Zeroable)]
#[repr(i32)]
</span><span class="kw">enum </span>Values {
A = <span class="number">0</span>,
B = <span class="number">1</span>,
C = <span class="number">2</span>,
}
<span class="attr">#[derive(Clone, Zeroable)]
#[repr(C)]
</span><span class="kw">enum </span>Implicit {
A(bool, u8, char),
B(String),
C(std::num::NonZeroU8),
}</code></pre></div>
<h2 id="custom-bounds"><a class="doc-anchor" href="#custom-bounds">§</a>Custom bounds</h2>
<p>Custom bounds for the derived <code>Zeroable</code> impl can be given using the
<code>#[zeroable(bound = "")]</code> helper attribute.</p>
<p>Using this attribute additionally opts-in to “perfect derive” semantics,
where instead of adding bounds for each generic type parameter, bounds are
added for each field’s type.</p>
<h3 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Clone, Zeroable)]
#[zeroable(bound = <span class="string">""</span>)]
</span><span class="kw">struct </span>AlwaysZeroable&lt;T&gt; {
a: PhantomData&lt;T&gt;,
}
AlwaysZeroable::&lt;std::num::NonZeroU8&gt;::zeroed();</code></pre></div>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Copy, Clone, Zeroable)]
#[repr(u8)]
#[zeroable(bound = <span class="string">""</span>)]
</span><span class="kw">enum </span>MyOption&lt;T&gt; {
<span class="prelude-val">None</span>,
<span class="prelude-val">Some</span>(T),
}
<span class="macro">assert!</span>(<span class="macro">matches!</span>(MyOption::&lt;std::num::NonZeroU8&gt;::zeroed(), MyOption::None));</code></pre></div>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Clone, Zeroable)]
#[zeroable(bound = <span class="string">"T: Copy"</span>)]
</span><span class="kw">struct </span>ZeroableWhenTIsCopy&lt;T&gt; {
a: PhantomData&lt;T&gt;,
}
ZeroableWhenTIsCopy::&lt;String&gt;::zeroed();</code></pre></div>
<p>The restriction that all fields must be Zeroable is still applied, and this
is enforced using the mentioned “perfect derive” semantics.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Clone, Zeroable)]
#[zeroable(bound = <span class="string">""</span>)]
</span><span class="kw">struct </span>ZeroableWhenTIsZeroable&lt;T&gt; {
a: T,
}
ZeroableWhenTIsZeroable::&lt;u32&gt;::zeroed();</code></pre></div>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile">ⓘ</a><pre class="rust rust-example-rendered"><code>ZeroableWhenTIsZeroable::&lt;String&gt;::zeroed();</code></pre></div>
</div></details></section></div></main></body></html>