1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
use locspan::{Stripped, StrippedHash};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
/// Hash a set of items.
///
/// The standard library does not provide (yet) a `Hash` implementation
/// for set types. This can be used instead.
///
/// Note that this function not particularly strong and does
/// not protect against DoS attacks.
pub fn hash_set<S: IntoIterator, H: Hasher>(set: S, hasher: &mut H)
where
S::Item: Hash,
{
// See: https://github.com/rust-lang/rust/pull/48366
// Elements must be combined with a associative and commutative operation •.
// (u64, •, 0) must form a commutative monoid.
// This is satisfied by • = u64::wrapping_add.
let mut hash = 0;
for item in set {
let mut h = DefaultHasher::new();
item.hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}
pub fn hash_set_stripped<S: IntoIterator, H: Hasher>(set: S, hasher: &mut H)
where
S::Item: StrippedHash,
{
// See: https://github.com/rust-lang/rust/pull/48366
// Elements must be combined with a associative and commutative operation •.
// (u64, •, 0) must form a commutative monoid.
// This is satisfied by • = u64::wrapping_add.
let mut hash = 0;
for item in set {
let mut h = DefaultHasher::new();
item.stripped_hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}
/// Hash an optional set of items.
pub fn hash_set_opt<S: IntoIterator, H: Hasher>(set_opt: Option<S>, hasher: &mut H)
where
S::Item: Hash,
{
if let Some(set) = set_opt {
hash_set(set, hasher)
}
}
pub fn hash_set_stripped_opt<S: IntoIterator, H: Hasher>(set_opt: Option<S>, hasher: &mut H)
where
S::Item: StrippedHash,
{
if let Some(set) = set_opt {
hash_set_stripped(set, hasher)
}
}
/// Hash a map.
///
/// The standard library does not provide (yet) a `Hash` implementation
/// for unordered map types. This can be used instead.
///
/// Note that this function not particularly strong and does
/// not protect against DoS attacks.
pub fn hash_map<'a, K: 'a + Hash, V: 'a + Hash, H: Hasher>(
map: impl 'a + IntoIterator<Item = (&'a K, &'a V)>,
hasher: &mut H,
) {
// See: https://github.com/rust-lang/rust/pull/48366
// Elements must be combined with a associative and commutative operation •.
// (u64, •, 0) must form a commutative monoid.
// This is satisfied by • = u64::wrapping_add.
let mut hash = 0;
for entry in map {
let mut h = DefaultHasher::new();
entry.hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}
pub fn hash_map_stripped<'a, K: 'a + Hash, V: 'a + StrippedHash, H: Hasher>(
map: impl 'a + IntoIterator<Item = (&'a K, &'a V)>,
hasher: &mut H,
) {
// See: https://github.com/rust-lang/rust/pull/48366
// Elements must be combined with a associative and commutative operation •.
// (u64, •, 0) must form a commutative monoid.
// This is satisfied by • = u64::wrapping_add.
let mut hash = 0;
for (k, v) in map {
let mut h = DefaultHasher::new();
(k, Stripped(v)).hash(&mut h);
hash = u64::wrapping_add(hash, h.finish());
}
hasher.write_u64(hash);
}