| 1 | //! The foldhash implementation optimized for speed. |
| 2 | |
| 3 | 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#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#}; |
| 4 | |
| 5 | use craterust-analyzer cargo foldhash 0.2.0 crate/::seedrust-analyzer cargo foldhash 0.2.0 seed/::{gen_per_hasher_seedrust-analyzer cargo foldhash 0.2.0 seed/gen_per_hasher_seed()., GlobalSeedrust-analyzer cargo foldhash 0.2.0 seed/global/GlobalSeed#, SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#}; |
| 6 | use craterust-analyzer cargo foldhash 0.2.0 crate/::{folded_multiplyrust-analyzer cargo foldhash 0.2.0 folded_multiply()., hash_bytes_longrust-analyzer cargo foldhash 0.2.0 hash_bytes_long()., hash_bytes_shortrust-analyzer cargo foldhash 0.2.0 hash_bytes_short()., rotate_rightrust-analyzer cargo foldhash 0.2.0 rotate_right()., ARBITRARY3rust-analyzer cargo foldhash 0.2.0 ARBITRARY3.}; |
| 7 | |
| 8 | /// A [`Hasher`] instance implementing foldhash, optimized for speed. |
| 9 | /// |
| 10 | /// While you can create one directly with [`FoldHasher::with_seed`], you |
| 11 | /// most likely want to use [`RandomState`], [`SeedableRandomState`] or |
| 12 | /// [`FixedState`] to create [`FoldHasher`]s. |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 14 | pub struct FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'a> { |
| 15 | accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator.: u64, |
| 16 | spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge.: u128, |
| 17 | sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len.: u8, |
| 18 | seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.: &'a [u64; 6], |
| 19 | } |
| 20 | |
| 21 | impl<'a> FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'a> { |
| 22 | /// Initializes this [`FoldHasher`] with the given per-hasher seed and |
| 23 | /// [`SharedSeed`]. |
| 24 | #[inline] |
| 25 | pub const fn with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]with_seed().(per_hasher_seed: u64, shared_seed: &'a SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#) -> FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'a> { |
| 26 | FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher# { |
| 27 | accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator.: per_hasher_seed, |
| 28 | spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge.: 0, |
| 29 | sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len.: 0, |
| 30 | seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.: &shared_seed.seedsrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#seeds., |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[inline(always)] |
| 35 | fn write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().<T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<u128>>(&mut self, x: T) { |
| 36 | let bits: usize = 8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>(); |
| 37 | if self.sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len. as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().128 { |
| 38 | let lo = self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. as u64; |
| 39 | let hi = (self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().64) as u64; |
| 40 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. = folded_multiplyrust-analyzer cargo foldhash 0.2.0 folded_multiply().(lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., hi rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 41 | self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. = x.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 42 | self.sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len. = bits as u8; |
| 43 | } else { |
| 44 | self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitOrAssign<Self>`]bitor_assign().|=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitOrAssign<Self>`]bitor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitOrAssign<Self>`]bitor_assign().x.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl().self.sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len.; |
| 45 | self.sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`AddAssign<Self>`]add_assign().bits as u8; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl<'a> Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher# for FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'a> { |
| 51 | #[inline(always)] |
| 52 | fn writerust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write().(&mut self, bytes: &[u8]) { |
| 53 | // We perform overlapping reads in the byte hash which could lead to |
| 54 | // trivial length-extension attacks. These should be defeated by |
| 55 | // adding a length-dependent rotation on our unpredictable seed |
| 56 | // which costs only a single cycle (or none if executed with |
| 57 | // instruction-level parallelism). |
| 58 | let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 59 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. = rotate_rightrust-analyzer cargo foldhash 0.2.0 rotate_right().(self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., len as u32); |
| 60 | if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().16 { |
| 61 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. = hash_bytes_shortrust-analyzer cargo foldhash 0.2.0 hash_bytes_short().(bytes, self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., self.seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.); |
| 62 | } else { |
| 63 | unsafe { |
| 64 | // SAFETY: we checked that the length is > 16 bytes. |
| 65 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. = hash_bytes_longrust-analyzer cargo foldhash 0.2.0 hash_bytes_long().(bytes, self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., self.seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | #[inline(always)] |
| 71 | fn write_u8rust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_u8().(&mut self, i: u8) { |
| 72 | self.write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().(i); |
| 73 | } |
| 74 | |
| 75 | #[inline(always)] |
| 76 | fn write_u16rust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_u16().(&mut self, i: u16) { |
| 77 | self.write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().(i); |
| 78 | } |
| 79 | |
| 80 | #[inline(always)] |
| 81 | fn write_u32rust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_u32().(&mut self, i: u32) { |
| 82 | self.write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().(i); |
| 83 | } |
| 84 | |
| 85 | #[inline(always)] |
| 86 | fn write_u64rust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_u64().(&mut self, i: u64) { |
| 87 | self.write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().(i); |
| 88 | } |
| 89 | |
| 90 | #[inline(always)] |
| 91 | fn write_u128rust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_u128().(&mut self, i: u128) { |
| 92 | let lo = i as u64; |
| 93 | let hi = (i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().64) as u64; |
| 94 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. = folded_multiplyrust-analyzer cargo foldhash 0.2.0 folded_multiply().(lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., hi rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 95 | } |
| 96 | |
| 97 | #[inline(always)] |
| 98 | fn write_usizerust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_usize().(&mut self, i: usize) { |
| 99 | // u128 doesn't implement From<usize>. |
| 100 | #[cfg(target_pointer_width = "32")] |
| 101 | self.write_num(i as u32); |
| 102 | #[cfg(target_pointer_width = "64")] |
| 103 | self.write_numrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]write_num().(i as u64); |
| 104 | } |
| 105 | |
| 106 | #[cfg(feature = "nightly")] |
| 107 | #[inline(always)] |
| 108 | fn write_strrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write_str().(&mut self, s: &str) { |
| 109 | // Our write function already handles length differences. |
| 110 | self.writerust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]write().(s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()) |
| 111 | } |
| 112 | |
| 113 | #[inline(always)] |
| 114 | fn finishrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`][Hasher]finish().(&self) -> u64 { |
| 115 | if self.sponge_lenrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge_len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().0 { |
| 116 | let lo = self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. as u64; |
| 117 | let hi = (self.spongerust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#sponge. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shr<i32>`]shr().64) as u64; |
| 118 | folded_multiplyrust-analyzer cargo foldhash 0.2.0 folded_multiply().(lo rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator., hi rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().self.seedsrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#seeds.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 119 | } else { |
| 120 | self.accumulatorrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#accumulator. |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that is randomly initialized. |
| 126 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 127 | pub struct RandomStaterust-analyzer cargo foldhash 0.2.0 fast/RandomState# { |
| 128 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#per_hasher_seed.: u64, |
| 129 | global_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#global_seed.: GlobalSeedrust-analyzer cargo foldhash 0.2.0 seed/global/GlobalSeed#, |
| 130 | } |
| 131 | |
| 132 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for RandomStaterust-analyzer cargo foldhash 0.2.0 fast/RandomState# { |
| 133 | #[inline(always)] |
| 134 | fn defaultrust-analyzer cargo foldhash 0.2.0 fast/impl#[RandomState][Default]default().() -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[RandomState][Default] { |
| 135 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[RandomState][Default] { |
| 136 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#per_hasher_seed.: gen_per_hasher_seedrust-analyzer cargo foldhash 0.2.0 seed/gen_per_hasher_seed().(), |
| 137 | global_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#global_seed.: GlobalSeedrust-analyzer cargo foldhash 0.2.0 seed/global/GlobalSeed#::newrust-analyzer cargo foldhash 0.2.0 seed/global/impl#[GlobalSeed]new().(), |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# for RandomStaterust-analyzer cargo foldhash 0.2.0 fast/RandomState# { |
| 143 | type Hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[RandomState][BuildHasher]Hasher# = FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static>; |
| 144 | |
| 145 | #[inline(always)] |
| 146 | fn build_hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[RandomState][BuildHasher]build_hasher().(&self) -> FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static> { |
| 147 | FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#::with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]with_seed().(self.per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#per_hasher_seed., self.global_seedrust-analyzer cargo foldhash 0.2.0 fast/RandomState#global_seed..getrust-analyzer cargo foldhash 0.2.0 seed/global/impl#[GlobalSeed]get().()) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that is randomly |
| 152 | /// initialized by default, but can also be initialized with a specific seed. |
| 153 | /// |
| 154 | /// This can be useful for e.g. testing, but the downside is that this type |
| 155 | /// has a size of 16 bytes rather than the 8 bytes [`RandomState`] is. |
| 156 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 157 | pub struct SeedableRandomStaterust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState# { |
| 158 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#per_hasher_seed.: u64, |
| 159 | shared_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#shared_seed.: &'static SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#, |
| 160 | } |
| 161 | |
| 162 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for SeedableRandomStaterust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState# { |
| 163 | #[inline(always)] |
| 164 | fn defaultrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState][Default]default().() -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState][Default] { |
| 165 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState][Default]::randomrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState]random().() |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | impl SeedableRandomStaterust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState# { |
| 170 | /// Generates a random [`SeedableRandomState`], similar to [`RandomState`]. |
| 171 | #[inline(always)] |
| 172 | pub fn randomrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState]random().() -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 173 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 174 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#per_hasher_seed.: gen_per_hasher_seedrust-analyzer cargo foldhash 0.2.0 seed/gen_per_hasher_seed().(), |
| 175 | shared_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#shared_seed.: SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#::global_randomrust-analyzer cargo foldhash 0.2.0 seed/impl#[SharedSeed]global_random().(), |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// Generates a fixed [`SeedableRandomState`], similar to [`FixedState`]. |
| 180 | #[inline(always)] |
| 181 | pub fn fixedrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState]fixed().() -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 182 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 183 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#per_hasher_seed.: ARBITRARY3rust-analyzer cargo foldhash 0.2.0 ARBITRARY3., |
| 184 | shared_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#shared_seed.: SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#::global_fixedrust-analyzer cargo foldhash 0.2.0 seed/impl#[SharedSeed]global_fixed().(), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// Generates a [`SeedableRandomState`] with the given per-hasher seed |
| 189 | /// and [`SharedSeed`]. |
| 190 | #[inline(always)] |
| 191 | pub fn with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState]with_seed().(per_hasher_seed: u64, shared_seed: &'static SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#) -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 192 | // XOR with ARBITRARY3 such that with_seed(0) matches default. |
| 193 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState] { |
| 194 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#per_hasher_seed.: per_hasher_seed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().ARBITRARY3rust-analyzer cargo foldhash 0.2.0 ARBITRARY3., |
| 195 | shared_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#shared_seed., |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | impl BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# for SeedableRandomStaterust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState# { |
| 201 | type Hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState][BuildHasher]Hasher# = FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static>; |
| 202 | |
| 203 | #[inline(always)] |
| 204 | fn build_hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[SeedableRandomState][BuildHasher]build_hasher().(&self) -> FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static> { |
| 205 | FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#::with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]with_seed().(self.per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#per_hasher_seed., self.shared_seedrust-analyzer cargo foldhash 0.2.0 fast/SeedableRandomState#shared_seed.) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /// A [`BuildHasher`] for [`fast::FoldHasher`](FoldHasher) that always has the same fixed seed. |
| 210 | /// |
| 211 | /// Not recommended unless you absolutely need determinism. |
| 212 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 213 | pub struct FixedStaterust-analyzer cargo foldhash 0.2.0 fast/FixedState# { |
| 214 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/FixedState#per_hasher_seed.: u64, |
| 215 | } |
| 216 | |
| 217 | impl FixedStaterust-analyzer cargo foldhash 0.2.0 fast/FixedState# { |
| 218 | /// Creates a [`FixedState`] with the given per-hasher-seed. |
| 219 | #[inline(always)] |
| 220 | pub const fn with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState]with_seed().(per_hasher_seed: u64) -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState] { |
| 221 | // XOR with ARBITRARY3 such that with_seed(0) matches default. |
| 222 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState] { |
| 223 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/FixedState#per_hasher_seed.: per_hasher_seed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXor<Self>`]bitxor().ARBITRARY3rust-analyzer cargo foldhash 0.2.0 ARBITRARY3., |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for FixedStaterust-analyzer cargo foldhash 0.2.0 fast/FixedState# { |
| 229 | #[inline(always)] |
| 230 | fn defaultrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState][Default]default().() -> Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState][Default] { |
| 231 | Selfrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState][Default] { |
| 232 | per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/FixedState#per_hasher_seed.: ARBITRARY3rust-analyzer cargo foldhash 0.2.0 ARBITRARY3., |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | impl BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# for FixedStaterust-analyzer cargo foldhash 0.2.0 fast/FixedState# { |
| 238 | type Hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState][BuildHasher]Hasher# = FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static>; |
| 239 | |
| 240 | #[inline(always)] |
| 241 | fn build_hasherrust-analyzer cargo foldhash 0.2.0 fast/impl#[FixedState][BuildHasher]build_hasher().(&self) -> FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#<'static> { |
| 242 | FoldHasherrust-analyzer cargo foldhash 0.2.0 fast/FoldHasher#::with_seedrust-analyzer cargo foldhash 0.2.0 fast/impl#[`FoldHasher<'a>`]with_seed().(self.per_hasher_seedrust-analyzer cargo foldhash 0.2.0 fast/FixedState#per_hasher_seed., SharedSeedrust-analyzer cargo foldhash 0.2.0 seed/SharedSeed#::global_fixedrust-analyzer cargo foldhash 0.2.0 seed/impl#[SharedSeed]global_fixed().()) |
| 243 | } |
| 244 | } |