Rollup merge of #89443 - cuviper:btree-hash-len, r=dtolnay
Include the length in BTree hashes This change makes it consistent with `Hash` for all other collections.
This commit is contained in:
commit
e1478d650d
@ -1968,6 +1968,7 @@ fn extend_one(&mut self, (&k, &v): (&'a K, &'a V)) {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<K: Hash, V: Hash> Hash for BTreeMap<K, V> {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.len().hash(state);
|
||||
for elt in self {
|
||||
elt.hash(state);
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
use crate::hash;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
#[test]
|
||||
fn test_hash() {
|
||||
use crate::hash;
|
||||
|
||||
let mut x = BTreeSet::new();
|
||||
let mut y = BTreeSet::new();
|
||||
|
||||
@ -17,3 +16,14 @@ fn test_hash() {
|
||||
|
||||
assert_eq!(hash(&x), hash(&y));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prefix_free() {
|
||||
let x = BTreeSet::from([1, 2, 3]);
|
||||
let y = BTreeSet::<i32>::new();
|
||||
|
||||
// If hashed by iteration alone, `(x, y)` and `(y, x)` would visit the same
|
||||
// order of elements, resulting in the same hash. But now that we also hash
|
||||
// the length, they get distinct sequences of hashed data.
|
||||
assert_ne!(hash(&(&x, &y)), hash(&(&y, &x)));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user