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

31 lines
10 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="Types which are safe to use with `NoHashHasher`."><title>IsEnabled in nohash_hasher - 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="nohash_hasher" 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="../nohash_hasher/index.html">nohash_<wbr>hasher</a><span class="version">0.2.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">IsEnabled</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#example" title="Example">Example</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-IsEnabled-for-i16" title="i16">i16</a></li><li><a href="#impl-IsEnabled-for-i32" title="i32">i32</a></li><li><a href="#impl-IsEnabled-for-i64" title="i64">i64</a></li><li><a href="#impl-IsEnabled-for-i8" title="i8">i8</a></li><li><a href="#impl-IsEnabled-for-isize" title="isize">isize</a></li><li><a href="#impl-IsEnabled-for-u16" title="u16">u16</a></li><li><a href="#impl-IsEnabled-for-u32" title="u32">u32</a></li><li><a href="#impl-IsEnabled-for-u64" title="u64">u64</a></li><li><a href="#impl-IsEnabled-for-u8" title="u8">u8</a></li><li><a href="#impl-IsEnabled-for-usize" title="usize">usize</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 nohash_<wbr>hasher</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">nohash_hasher</a></span><h1>Trait <span class="trait">IsEnabled</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/nohash_hasher/lib.rs.html#185">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IsEnabled { }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Types which are safe to use with <code>NoHashHasher</code>.</p>
<p>This marker trait is an option for types to enable themselves for use
with <code>NoHashHasher</code>. In order to be safe, the <code>Hash</code> impl needs to
satisfy the following constraint:</p>
<blockquote>
<p><strong>One of the <code>Hasher::write_{u8,u16,u32,u64,usize,i8,i16,i32,i64,isize}</code>
methods is invoked exactly once.</strong></p>
</blockquote>
<p>The best way to ensure this is to write a custom <code>Hash</code> impl even when
deriving <code>Hash</code> for a simple newtype of a single type which itself
implements <code>IsEnabled</code> may work as well.</p>
<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="attr">#[derive(PartialEq, Eq)]
</span><span class="kw">struct </span>SomeType(u32);
<span class="kw">impl </span>std::hash::Hash <span class="kw">for </span>SomeType {
<span class="kw">fn </span>hash&lt;H: std::hash::Hasher&gt;(<span class="kw-2">&amp;</span><span class="self">self</span>, hasher: <span class="kw-2">&amp;mut </span>H) {
hasher.write_u32(<span class="self">self</span>.<span class="number">0</span>)
}
}
<span class="kw">impl </span>nohash_hasher::IsEnabled <span class="kw">for </span>SomeType {}
<span class="kw">let </span><span class="kw-2">mut </span>m = nohash_hasher::IntMap::default();
m.insert(SomeType(<span class="number">1</span>), <span class="string">'t'</span>);
m.insert(SomeType(<span class="number">0</span>), <span class="string">'f'</span>);
<span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="string">'t'</span>), m.get(<span class="kw-2">&amp;</span>SomeType(<span class="number">1</span>)));
<span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="string">'f'</span>), m.get(<span class="kw-2">&amp;</span>SomeType(<span class="number">0</span>)));</code></pre></div>
</div></details><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><section id="impl-IsEnabled-for-i8" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#192">Source</a><a href="#impl-IsEnabled-for-i8" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.i8.html">i8</a></h3></section><section id="impl-IsEnabled-for-i16" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#193">Source</a><a href="#impl-IsEnabled-for-i16" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.i16.html">i16</a></h3></section><section id="impl-IsEnabled-for-i32" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#194">Source</a><a href="#impl-IsEnabled-for-i32" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.i32.html">i32</a></h3></section><section id="impl-IsEnabled-for-i64" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#195">Source</a><a href="#impl-IsEnabled-for-i64" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.i64.html">i64</a></h3></section><section id="impl-IsEnabled-for-isize" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#196">Source</a><a href="#impl-IsEnabled-for-isize" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.isize.html">isize</a></h3></section><section id="impl-IsEnabled-for-u8" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#187">Source</a><a href="#impl-IsEnabled-for-u8" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u8.html">u8</a></h3></section><section id="impl-IsEnabled-for-u16" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#188">Source</a><a href="#impl-IsEnabled-for-u16" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u16.html">u16</a></h3></section><section id="impl-IsEnabled-for-u32" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#189">Source</a><a href="#impl-IsEnabled-for-u32" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u32.html">u32</a></h3></section><section id="impl-IsEnabled-for-u64" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#190">Source</a><a href="#impl-IsEnabled-for-u64" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.u64.html">u64</a></h3></section><section id="impl-IsEnabled-for-usize" class="impl"><a class="src rightside" href="../src/nohash_hasher/lib.rs.html#191">Source</a><a href="#impl-IsEnabled-for-usize" class="anchor">§</a><h3 class="code-header">impl <a class="trait" href="trait.IsEnabled.html" title="trait nohash_hasher::IsEnabled">IsEnabled</a> for <a class="primitive" href="https://doc.rust-lang.org/1.84.0/std/primitive.usize.html">usize</a></h3></section><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../trait.impl/nohash_hasher/trait.IsEnabled.js" data-ignore-extern-crates="std" async></script></section></div></main></body></html>