Skip to content

Commit 070a579

Browse files
authored
Implement Hash trait for SmallVec (#337)
While calling the `hash` function on a `SmallVec` currently works via dereferencing into `&[T]` (see `test_hash` on `tests.rs`), this is not enough to allow deriving `Hash` on e.g. a struct containing a single field of type `SmallVec`.
1 parent 390bd7c commit 070a579

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ use alloc::alloc::Layout;
6969
use core::borrow::Borrow;
7070
use core::borrow::BorrowMut;
7171
use core::fmt::Debug;
72+
use core::hash::{Hash, Hasher};
73+
use core::marker::PhantomData;
7274
use core::mem::align_of;
7375
use core::mem::size_of;
7476
use core::mem::ManuallyDrop;
@@ -79,8 +81,6 @@ use core::ptr::copy;
7981
use core::ptr::copy_nonoverlapping;
8082
use core::ptr::NonNull;
8183

82-
use core::marker::PhantomData;
83-
8484
#[cfg(feature = "serde")]
8585
use serde::{
8686
de::{Deserialize, Deserializer, SeqAccess, Visitor},
@@ -2049,6 +2049,12 @@ where
20492049
}
20502050
}
20512051

2052+
impl<T: Hash, const N: usize> Hash for SmallVec<T, N> {
2053+
fn hash<H: Hasher>(&self, state: &mut H) {
2054+
self.as_slice().hash(state)
2055+
}
2056+
}
2057+
20522058
impl<T, const N: usize> Borrow<[T]> for SmallVec<T, N> {
20532059
#[inline]
20542060
fn borrow(&self) -> &[T] {

0 commit comments

Comments
 (0)