Extended BorrowDecode for HashMap to support custom hashers (#585)

This commit is contained in:
Vincent Rouillé 2022-10-02 10:12:14 +02:00 committed by GitHub
parent 6d995a74c7
commit 1b5eab9fcf
1 changed files with 4 additions and 2 deletions

View File

@ -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)>());