Skip to content

Commit

Permalink
Extended BorrowDecode for HashMap to support custom hashers (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
Speedy37 authored Oct 2, 2022
1 parent 52464e0 commit 1dda2a5
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,18 @@ where
Ok(map)
}
}
impl<'de, K, V> BorrowDecode<'de> for HashMap<K, V>
impl<'de, K, V, S> BorrowDecode<'de> for HashMap<K, V, S>
where
K: BorrowDecode<'de> + Eq + std::hash::Hash,
V: BorrowDecode<'de>,
S: std::hash::BuildHasher + Default,
{
fn borrow_decode<D: BorrowDecoder<'de>>(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)>());
Expand Down

0 comments on commit 1dda2a5

Please sign in to comment.