Extended BorrowDecode for HashMap to support custom hashers (#585)
This commit is contained in:
parent
6d995a74c7
commit
1b5eab9fcf
|
|
@ -444,16 +444,18 @@ where
|
||||||
Ok(map)
|
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
|
where
|
||||||
K: BorrowDecode<'de> + Eq + std::hash::Hash,
|
K: BorrowDecode<'de> + Eq + std::hash::Hash,
|
||||||
V: BorrowDecode<'de>,
|
V: BorrowDecode<'de>,
|
||||||
|
S: std::hash::BuildHasher + Default,
|
||||||
{
|
{
|
||||||
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
|
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
|
||||||
let len = crate::de::decode_slice_len(decoder)?;
|
let len = crate::de::decode_slice_len(decoder)?;
|
||||||
decoder.claim_container_read::<(K, V)>(len)?;
|
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 {
|
for _ in 0..len {
|
||||||
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
|
// 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)>());
|
decoder.unclaim_bytes_read(core::mem::size_of::<(K, V)>());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue