82 lines
12 KiB
HTML
82 lines
12 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="API documentation for the Rust `hash` mod in crate `nom`."><meta name="keywords" content="rust, rustlang, rust-lang, hash"><title>nom::lib::std::hash - Rust</title><link rel="stylesheet" type="text/css" href="../../../../normalize.css"><link rel="stylesheet" type="text/css" href="../../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../../../dark.css"><link rel="stylesheet" type="text/css" href="../../../../light.css" id="themeStyle"><script src="../../../../storage.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="shortcut icon" href="../../../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../../../nom/index.html'><div class='logo-container'><img src='../../../../rust-logo.png' alt='logo'></div></a><p class='location'>Module hash</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'><a href='../../../index.html'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a></p><script>window.sidebarCurrent = {name: 'hash', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../../../settings.html"><img src="../../../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/lib.rs.html#242' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../../../index.html'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a>::<wbr><a class="mod" href=''>hash</a></span></h1><div class='docblock'><p>Generic hashing support.</p>
|
||
<p>This module provides a generic way to compute the hash of a value. The
|
||
simplest way to make a type hashable is to use <code>#[derive(Hash)]</code>:</p>
|
||
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">hash_map</span>::<span class="ident">DefaultHasher</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">hash</span>::{<span class="ident">Hash</span>, <span class="ident">Hasher</span>};
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Hash</span>)]</span>
|
||
<span class="kw">struct</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="ident">u32</span>,
|
||
<span class="ident">name</span>: <span class="ident">String</span>,
|
||
<span class="ident">phone</span>: <span class="ident">u64</span>,
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">person1</span> <span class="op">=</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="number">5</span>,
|
||
<span class="ident">name</span>: <span class="string">"Janet"</span>.<span class="ident">to_string</span>(),
|
||
<span class="ident">phone</span>: <span class="number">555_666_7777</span>,
|
||
};
|
||
<span class="kw">let</span> <span class="ident">person2</span> <span class="op">=</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="number">5</span>,
|
||
<span class="ident">name</span>: <span class="string">"Bob"</span>.<span class="ident">to_string</span>(),
|
||
<span class="ident">phone</span>: <span class="number">555_666_7777</span>,
|
||
};
|
||
|
||
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">calculate_hash</span>(<span class="kw-2">&</span><span class="ident">person1</span>) <span class="op">!</span><span class="op">=</span> <span class="ident">calculate_hash</span>(<span class="kw-2">&</span><span class="ident">person2</span>));
|
||
|
||
<span class="kw">fn</span> <span class="ident">calculate_hash</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">Hash</span><span class="op">></span>(<span class="ident">t</span>: <span class="kw-2">&</span><span class="ident">T</span>) <span class="op">-</span><span class="op">></span> <span class="ident">u64</span> {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">DefaultHasher</span>::<span class="ident">new</span>();
|
||
<span class="ident">t</span>.<span class="ident">hash</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">s</span>);
|
||
<span class="ident">s</span>.<span class="ident">finish</span>()
|
||
}</pre></div>
|
||
<p>If you need more control over how a value is hashed, you need to implement
|
||
the <a href="trait.Hash.html"><code>Hash</code></a> trait:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">hash_map</span>::<span class="ident">DefaultHasher</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">hash</span>::{<span class="ident">Hash</span>, <span class="ident">Hasher</span>};
|
||
|
||
<span class="kw">struct</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="ident">u32</span>,
|
||
<span class="ident">name</span>: <span class="ident">String</span>,
|
||
<span class="ident">phone</span>: <span class="ident">u64</span>,
|
||
}
|
||
|
||
<span class="kw">impl</span> <span class="ident">Hash</span> <span class="kw">for</span> <span class="ident">Person</span> {
|
||
<span class="kw">fn</span> <span class="ident">hash</span><span class="op"><</span><span class="ident">H</span>: <span class="ident">Hasher</span><span class="op">></span>(<span class="kw-2">&</span><span class="self">self</span>, <span class="ident">state</span>: <span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">H</span>) {
|
||
<span class="self">self</span>.<span class="ident">id</span>.<span class="ident">hash</span>(<span class="ident">state</span>);
|
||
<span class="self">self</span>.<span class="ident">phone</span>.<span class="ident">hash</span>(<span class="ident">state</span>);
|
||
}
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">person1</span> <span class="op">=</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="number">5</span>,
|
||
<span class="ident">name</span>: <span class="string">"Janet"</span>.<span class="ident">to_string</span>(),
|
||
<span class="ident">phone</span>: <span class="number">555_666_7777</span>,
|
||
};
|
||
<span class="kw">let</span> <span class="ident">person2</span> <span class="op">=</span> <span class="ident">Person</span> {
|
||
<span class="ident">id</span>: <span class="number">5</span>,
|
||
<span class="ident">name</span>: <span class="string">"Bob"</span>.<span class="ident">to_string</span>(),
|
||
<span class="ident">phone</span>: <span class="number">555_666_7777</span>,
|
||
};
|
||
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">calculate_hash</span>(<span class="kw-2">&</span><span class="ident">person1</span>), <span class="ident">calculate_hash</span>(<span class="kw-2">&</span><span class="ident">person2</span>));
|
||
|
||
<span class="kw">fn</span> <span class="ident">calculate_hash</span><span class="op"><</span><span class="ident">T</span>: <span class="ident">Hash</span><span class="op">></span>(<span class="ident">t</span>: <span class="kw-2">&</span><span class="ident">T</span>) <span class="op">-</span><span class="op">></span> <span class="ident">u64</span> {
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">s</span> <span class="op">=</span> <span class="ident">DefaultHasher</span>::<span class="ident">new</span>();
|
||
<span class="ident">t</span>.<span class="ident">hash</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">s</span>);
|
||
<span class="ident">s</span>.<span class="ident">finish</span>()
|
||
}</pre></div>
|
||
</div><h2 id='macros' class='section-header'><a href="#macros">Macros</a></h2>
|
||
<table><tr class='module-item'><td><a class="macro" href="macro.Hash.html" title='nom::lib::std::hash::Hash macro'>Hash</a></td><td class='docblock-short'><p>Derive macro generating an impl of the trait <code>Hash</code>.</p>
|
||
</td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table><tr class='module-item'><td><a class="struct" href="struct.BuildHasherDefault.html" title='nom::lib::std::hash::BuildHasherDefault struct'>BuildHasherDefault</a></td><td class='docblock-short'><p>Used to create a default <a href="trait.BuildHasher.html"><code>BuildHasher</code></a> instance for types that implement
|
||
<a href="trait.Hasher.html"><code>Hasher</code></a> and <a href="../default/trait.Default.html"><code>Default</code></a>.</p>
|
||
</td></tr><tr class='deprecated module-item'><td><a class="struct" href="struct.SipHasher.html" title='nom::lib::std::hash::SipHasher struct'>SipHasher</a></td><td class='docblock-short'><span class="stab deprecated">Deprecated</span><p>An implementation of SipHash 2-4.</p>
|
||
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
||
<table><tr class='module-item'><td><a class="trait" href="trait.BuildHasher.html" title='nom::lib::std::hash::BuildHasher trait'>BuildHasher</a></td><td class='docblock-short'><p>A trait for creating instances of <a href="trait.Hasher.html"><code>Hasher</code></a>.</p>
|
||
</td></tr><tr class='module-item'><td><a class="trait" href="trait.Hash.html" title='nom::lib::std::hash::Hash trait'>Hash</a></td><td class='docblock-short'><p>A hashable type.</p>
|
||
</td></tr><tr class='module-item'><td><a class="trait" href="trait.Hasher.html" title='nom::lib::std::hash::Hasher trait'>Hasher</a></td><td class='docblock-short'><p>A trait for hashing an arbitrary stream of bytes.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../../../";window.currentCrate = "nom";</script><script src="../../../../aliases.js"></script><script src="../../../../main.js"></script><script defer src="../../../../search-index.js"></script></body></html> |