Files
phy/bytecheck/trait.Verify.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

50 lines
7.9 KiB
HTML

<!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="A type that can check whether its invariants are upheld."><title>Verify in bytecheck - 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="bytecheck" 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 trait"><!--[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="../bytecheck/index.html">bytecheck</a><span class="version">0.8.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Verify</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#safety" title="Safety">Safety</a></li><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.verify" title="verify">verify</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate bytecheck</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">bytecheck</a></span><h1>Trait <span class="trait">Verify</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/bytecheck/lib.rs.html#221-224">Source</a> </span></div><pre class="rust item-decl"><code>pub unsafe trait Verify&lt;C: <a class="trait" href="../rancor/trait.Fallible.html" title="trait rancor::Fallible">Fallible</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; {
// Required method
fn <a href="#tymethod.verify" class="fn">verify</a>(&amp;self, context: <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.reference.html">&amp;mut C</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>, C::<a class="associatedtype" href="../rancor/trait.Fallible.html#associatedtype.Error" title="type rancor::Fallible::Error">Error</a>&gt;;
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A type that can check whether its invariants are upheld.</p>
<p>When using <a href="derive.CheckBytes.html" title="derive bytecheck::CheckBytes">the derive</a>, adding <code>#[bytecheck(verify)]</code>
allows implementing <code>Verify</code> for the derived type. <a href="trait.Verify.html#tymethod.verify" title="method bytecheck::Verify::verify"><code>Verify::verify</code></a> will
be called after the type is checked and all fields are known to be valid.</p>
<h2 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h2>
<ul>
<li><code>verify</code> must only return <code>Ok</code> if all of the invariants of this type are
upheld by <code>self</code>.</li>
<li><code>verify</code> may not assume that its type invariants are upheld by the given
<code>self</code> (the invariants of each field are guaranteed to be upheld).</li>
</ul>
<h2 id="example"><a class="doc-anchor" href="#example">§</a>Example</h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>core::{error::Error, fmt};
<span class="kw">use </span>bytecheck::{CheckBytes, Verify};
<span class="kw">use </span>rancor::{fail, Fallible, Source};
<span class="attr">#[derive(CheckBytes)]
#[bytecheck(verify)]
#[repr(C, align(<span class="number">4</span>))]
</span><span class="kw">pub struct </span>NonMaxU32(u32);
<span class="kw">unsafe impl</span>&lt;C: Fallible + <span class="question-mark">?</span>Sized&gt; Verify&lt;C&gt; <span class="kw">for </span>NonMaxU32
<span class="kw">where
</span>C::Error: Source,
{
<span class="kw">fn </span>verify(<span class="kw-2">&amp;</span><span class="self">self</span>, context: <span class="kw-2">&amp;mut </span>C) -&gt; <span class="prelude-ty">Result</span>&lt;(), C::Error&gt; {
<span class="attr">#[derive(Debug)]
</span><span class="kw">struct </span>NonMaxCheckError;
<span class="kw">impl </span>fmt::Display <span class="kw">for </span>NonMaxCheckError {
<span class="kw">fn </span>fmt(<span class="kw-2">&amp;</span><span class="self">self</span>, f: <span class="kw-2">&amp;mut </span>fmt::Formatter&lt;<span class="lifetime">'_</span>&gt;) -&gt; fmt::Result {
<span class="macro">write!</span>(f, <span class="string">"non-max u32 was set to u32::MAX"</span>)
}
}
<span class="kw">impl </span>Error <span class="kw">for </span>NonMaxCheckError {}
<span class="kw">if </span><span class="self">self</span>.<span class="number">0 </span>== u32::MAX {
<span class="macro">fail!</span>(NonMaxCheckError);
}
<span class="prelude-val">Ok</span>(())
}
}</code></pre></div>
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.verify" class="method"><a class="src rightside" href="../src/bytecheck/lib.rs.html#223">Source</a><h4 class="code-header">fn <a href="#tymethod.verify" class="fn">verify</a>(&amp;self, context: <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.reference.html">&amp;mut C</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.84.0/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.unit.html">()</a>, C::<a class="associatedtype" href="../rancor/trait.Fallible.html#associatedtype.Error" title="type rancor::Fallible::Error">Error</a>&gt;</h4></section></summary><div class="docblock"><p>Checks whether the invariants of this type are upheld by <code>self</code>.</p>
</div></details></div><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../trait.impl/bytecheck/trait.Verify.js" async></script></section></div></main></body></html>