Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! 32-bit hashing algorithms
2 //!
3 //! # Why?
4 //!
5 //! Because 32-bit architectures are a thing (e.g. ARM Cortex-M) and you don't want your hashing
6 //! function to pull in a bunch of slow 64-bit compiler intrinsics (software implementations of
7 //! 64-bit operations).
8 //!
9 //! # Relationship to `core::hash`
10 //!
11 //! This crate extends [`core::hash`] with a 32-bit version of `Hasher`, which extends
12 //! `core::hash::Hasher`. It requires that the hasher only performs 32-bit operations when computing
13 //! the hash, and adds [`finish32`] to get the hasher's result as a `u32`. The standard `finish`
14 //! method should just zero-extend this result.
15 //!
16 //! Since it extends `core::hash::Hasher`, `Hasher` can be used with any type which implements the
17 //! standard `Hash` trait.
18 //!
19 //! This crate also adds a version of `BuildHasherDefault` with a const constructor, to work around
20 //! the `core` version's lack of one.
21 //!
22 //! [`core::hash`]: https://doc.rust-lang.org/std/hash/index.html
23 //! [`finish32`]: crate::Hasher::finish32
24 //!
25 //! # Hashers
26 //!
27 //! This crate provides implementations of the following 32-bit hashing algorithms:
28 //!
29 //! - [Fowler-Noll-Vo](struct.FnvHasher.html)
30 //! - [MurmurHash3](struct.Murmur3Hasher.html)
31 //!
32 //! # Generic code
33 //!
34 //! In generic code, the trait bound `H: core::hash::Hasher` accepts *both* 64-bit hashers like
35 //! `std::collections::hash_map::DefaultHasher`; and 32-bit hashers like the ones defined in this
36 //! crate (`hash32::FnvHasher` and `hash32::Murmur3Hasher`)
37 //!
38 //! The trait bound `H: hash32::Hasher` is *more* restrictive as it only accepts 32-bit hashers.
39 //!
40 //! The `BuildHasherDefault<H>` type implements the `core::hash::BuildHasher` trait so it can
41 //! construct both 32-bit and 64-bit hashers. To constrain the type to only produce 32-bit hasher
42 //! you can add the trait bound `H::Hasher: hash32::Hasher`
43 //!
44 //! # MSRV
45 //!
46 //! This crate is guaranteed to compile on latest stable Rust. It *might* compile on older
47 //! versions but that may change in any new patch release.
48
49 #![deny(missing_docs)]
50 #![deny(warnings)]
51 #![no_std]
52
53 extern crate byteorderrust-analyzer cargo hash32 0.3.1 byteorder/;
54
55 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
56 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#;
57 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#;
58
59 pub use fnvrust-analyzer cargo hash32 0.3.1 fnv/::Hasherrust-analyzer cargo hash32 0.3.1 fnv/Hasher# as FnvHasherrust-analyzer cargo hash32 0.3.1 fnv/Hasher#;
60 pub use murmur3rust-analyzer cargo hash32 0.3.1 murmur3/::Hasherrust-analyzer cargo hash32 0.3.1 murmur3/Hasher# as Murmur3Hasherrust-analyzer cargo hash32 0.3.1 murmur3/Hasher#;
61
62 mod fnvrust-analyzer cargo hash32 0.3.1 fnv/;
63 mod murmur3rust-analyzer cargo hash32 0.3.1 murmur3/;
64
65 /// A copy of [`core::hash::BuildHasherDefault`][0], but with a const constructor.
66 ///
67 /// This will eventually be deprecated once the version in `core` becomes const-constructible
68 /// (presumably using `const Default`).
69 ///
70 /// [0]: https://doc.rust-lang.org/core/hash/struct.BuildHasherDefault.html
71 pub struct BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
72 _markerrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<H>,
73 }
74
75 impl<H> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
76 fn defaultrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Default]default().() -> Selfrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Default] {
77 BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault# {
78 _markerrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
79 }
80 }
81 }
82
83 impl<H> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
84 fn clonerust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Clone]clone().(&self) -> Selfrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Clone] {
85 BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#::defaultrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Default]default().()
86 }
87 }
88
89 impl<H> PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
90 fn eqrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][`PartialEq<Self>`]eq().(&self, _other: &BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H>) -> bool {
91 true
92 }
93 }
94
95 impl<H> Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {}
96
97 impl<H> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
98 fn fmtrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
99 f.padrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]pad().("BuildHasherDefault")
100 }
101 }
102
103 impl<H> BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H> {
104 /// `const` constructor
105 pub const fn newrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`]new().() -> Selfrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`] {
106 BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault# {
107 _markerrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
108 }
109 }
110 }
111
112 impl<H> BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# for BuildHasherDefaultrust-analyzer cargo hash32 0.3.1 BuildHasherDefault#<H>
113 where
114 H: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#,
115 {
116 type Hasherrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][BuildHasher]Hasher# = H;
117
118 fn build_hasherrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][BuildHasher]build_hasher().(&self) -> Selfrust-analyzer cargo hash32 0.3.1 impl#[`BuildHasherDefault<H>`][BuildHasher]::Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#Hasher# {
119 H::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#default().()
120 }
121 }
122
123 /// An extension of [core::hash::Hasher][0] for hashers which use 32 bits.
124 ///
125 /// For hashers which implement this trait, the standard `finish` method should just return a
126 /// zero-extended version of the result of `finish32`.
127 ///
128 /// [0]: https://doc.rust-lang.org/core/hash/trait.Hasher.html
129 ///
130 /// # Contract
131 ///
132 /// Implementers of this trait must *not* perform any 64-bit (or 128-bit) operation while computing
133 /// the hash.
134 pub trait Hasherrust-analyzer cargo hash32 0.3.1 Hasher#: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher# {
135 /// The equivalent of [`core::hash::Hasher.finish`][0] for 32-bit hashers.
136 ///
137 /// This returns the hash directly; `finish` zero-extends it to 64 bits for compatibility.
138 ///
139 /// [0]: https://doc.rust-lang.org/std/hash/trait.Hasher.html#tymethod.finish
140 fn finish32rust-analyzer cargo hash32 0.3.1 Hasher#finish32().(&self) -> u32;
141 }