mirror of https://git.sr.ht/~stygianentity/bincode
Extended Encode and Decode for HashMap and HashSet to support custom hashers (#529)
This commit is contained in:
parent
6693276e95
commit
49c8d1148f
|
|
@ -361,7 +361,7 @@ impl Decode for SocketAddrV6 {
|
|||
impl std::error::Error for EncodeError {}
|
||||
impl std::error::Error for DecodeError {}
|
||||
|
||||
impl<K, V> Encode for HashMap<K, V>
|
||||
impl<K, V, S> Encode for HashMap<K, V, S>
|
||||
where
|
||||
K: Encode,
|
||||
V: Encode,
|
||||
|
|
@ -376,16 +376,18 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<K, V> Decode for HashMap<K, V>
|
||||
impl<K, V, S> Decode for HashMap<K, V, S>
|
||||
where
|
||||
K: Decode + Eq + std::hash::Hash,
|
||||
V: Decode,
|
||||
S: std::hash::BuildHasher + Default,
|
||||
{
|
||||
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
|
||||
let len = crate::de::decode_slice_len(decoder)?;
|
||||
decoder.claim_container_read::<(K, V)>(len)?;
|
||||
|
||||
let mut map = HashMap::with_capacity(len);
|
||||
let hash_builder: S = Default::default();
|
||||
let mut map = HashMap::with_capacity_and_hasher(len, hash_builder);
|
||||
for _ in 0..len {
|
||||
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
|
||||
decoder.unclaim_bytes_read(core::mem::size_of::<(K, V)>());
|
||||
|
|
@ -398,15 +400,17 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Decode for HashSet<T>
|
||||
impl<T, S> Decode for HashSet<T, S>
|
||||
where
|
||||
T: Decode + Eq + Hash,
|
||||
S: std::hash::BuildHasher + Default,
|
||||
{
|
||||
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
|
||||
let len = crate::de::decode_slice_len(decoder)?;
|
||||
decoder.claim_container_read::<T>(len)?;
|
||||
|
||||
let mut map = HashSet::new();
|
||||
let hash_builder: S = Default::default();
|
||||
let mut map: HashSet<T, S> = HashSet::with_capacity_and_hasher(len, hash_builder);
|
||||
for _ in 0..len {
|
||||
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
|
||||
decoder.unclaim_bytes_read(core::mem::size_of::<T>());
|
||||
|
|
@ -418,7 +422,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Encode for HashSet<T>
|
||||
impl<T, S> Encode for HashSet<T, S>
|
||||
where
|
||||
T: Encode,
|
||||
{
|
||||
|
|
|
|||
32
tests/std.rs
32
tests/std.rs
|
|
@ -106,6 +106,20 @@ fn test_std_commons() {
|
|||
set.insert("World".to_string());
|
||||
the_same(set);
|
||||
|
||||
// HashMap and HashSet with custom hash algorithm
|
||||
type MyBuildHasher = std::hash::BuildHasherDefault<ExampleCustomHasher>;
|
||||
let mut custom_map: std::collections::HashMap<String, String, MyBuildHasher> =
|
||||
Default::default();
|
||||
custom_map.insert("Hello".to_owned(), "world".to_owned());
|
||||
custom_map.insert("How".to_owned(), "are".to_owned());
|
||||
custom_map.insert("you".to_owned(), "doing?".to_owned());
|
||||
the_same(custom_map);
|
||||
|
||||
let mut custom_set: std::collections::HashSet<String, MyBuildHasher> = Default::default();
|
||||
custom_set.insert("Hello".to_string());
|
||||
custom_set.insert("World".to_string());
|
||||
the_same(custom_set);
|
||||
|
||||
// Borrowed values
|
||||
let config = bincode::config::standard();
|
||||
let mut buffer = [0u8; 1024];
|
||||
|
|
@ -141,3 +155,21 @@ fn test_system_time_out_of_range() {
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
/// Simple example of user-defined hasher to test encoding/decoding HashMap and HashSet with custom hash algorithms.
|
||||
#[derive(Copy, Clone, Default)]
|
||||
pub struct ExampleCustomHasher {
|
||||
pub hash: u64,
|
||||
}
|
||||
|
||||
impl std::hash::Hasher for ExampleCustomHasher {
|
||||
fn write(&mut self, value: &[u8]) {
|
||||
for (index, &item) in value.iter().enumerate() {
|
||||
self.hash ^= u64::from(item) << ((index % 8) * 8);
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
self.hash
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue