mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-28 19:27:30 +03:00
Rollup merge of #86140 - scottmcm:array-hash-facepalm, r=kennytm
Mention the `Borrow` guarantee on the `Hash` implementations for Arrays and `Vec` To remind people like me who forget about it and send PRs to make them different, and to (probably) get a test failure if the code is changed to no longer uphold it.
This commit is contained in:
@@ -2407,6 +2407,23 @@ fn clone_from(&mut self, other: &Self) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The hash of a vector is the same as that of the corresponding slice,
|
||||
/// as required by the `core::borrow::Borrow` implementation.
|
||||
///
|
||||
/// ```
|
||||
/// use std::hash::{BuildHasher, Hash, Hasher};
|
||||
///
|
||||
/// fn hash_of(x: impl Hash, b: &impl BuildHasher) -> u64 {
|
||||
/// let mut h = b.build_hasher();
|
||||
/// x.hash(&mut h);
|
||||
/// h.finish()
|
||||
/// }
|
||||
///
|
||||
/// let b = std::collections::hash_map::RandomState::new();
|
||||
/// let v: Vec<u8> = vec![0xa8, 0x3c, 0x09];
|
||||
/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
|
||||
/// assert_eq!(hash_of(v, &b), hash_of(s, &b));
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Hash, A: Allocator> Hash for Vec<T, A> {
|
||||
#[inline]
|
||||
|
||||
@@ -139,6 +139,23 @@ fn borrow_mut(&mut self) -> &mut [T] {
|
||||
}
|
||||
}
|
||||
|
||||
/// The hash of an array is the same as that of the corresponding slice,
|
||||
/// as required by the `Borrow` implementation.
|
||||
///
|
||||
/// ```
|
||||
/// use std::hash::{BuildHasher, Hash, Hasher};
|
||||
///
|
||||
/// fn hash_of(x: impl Hash, b: &impl BuildHasher) -> u64 {
|
||||
/// let mut h = b.build_hasher();
|
||||
/// x.hash(&mut h);
|
||||
/// h.finish()
|
||||
/// }
|
||||
///
|
||||
/// let b = std::collections::hash_map::RandomState::new();
|
||||
/// let a: [u8; 3] = [0xa8, 0x3c, 0x09];
|
||||
/// let s: &[u8] = &[0xa8, 0x3c, 0x09];
|
||||
/// assert_eq!(hash_of(a, &b), hash_of(s, &b));
|
||||
/// ```
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Hash, const N: usize> Hash for [T; N] {
|
||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
||||
|
||||
Reference in New Issue
Block a user