From 1b5eab9fcf94d691ac4421d86ba9f5c006d489ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Rouill=C3=A9?= Date: Sun, 2 Oct 2022 10:12:14 +0200 Subject: [PATCH] Extended BorrowDecode for HashMap to support custom hashers (#585) --- src/features/impl_std.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index e0b7b4b..9672105 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -444,16 +444,18 @@ where Ok(map) } } -impl<'de, K, V> BorrowDecode<'de> for HashMap +impl<'de, K, V, S> BorrowDecode<'de> for HashMap where K: BorrowDecode<'de> + Eq + std::hash::Hash, V: BorrowDecode<'de>, + S: std::hash::BuildHasher + Default, { fn borrow_decode>(decoder: &mut D) -> Result { 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)>());