diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2021-06-17 05:54:54 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-17 05:54:54 +0900 |
| commit | b1fb32d165ac892145988cf75ef121edc68f220d (patch) | |
| tree | 5434808b9bcb70d91bd932bd6792f5405eb863fe /library/alloc/src/vec | |
| parent | 4ff55ecf0477034a05bcf4a135e0a25e428cfccb (diff) | |
| parent | 3802d573c36902dbab68199beee3041514efd8bd (diff) | |
| download | rust-b1fb32d165ac892145988cf75ef121edc68f220d.tar.gz rust-b1fb32d165ac892145988cf75ef121edc68f220d.zip | |
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.
Diffstat (limited to 'library/alloc/src/vec')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 4a1d564e2ab..b59d2977add 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2407,6 +2407,23 @@ impl<T: Clone, A: Allocator + Clone> Clone for Vec<T, A> { } } +/// 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] |
