From d48105ee6f410d25ccda5a083f127fc53f6918cd Mon Sep 17 00:00:00 2001 From: "Heinz N. Gies" Date: Mon, 26 Jun 2023 10:41:51 +0200 Subject: [PATCH] Impl serde de for SizedHashMap Signed-off-by: Heinz N. Gies --- src/serde.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/serde.rs b/src/serde.rs index c0776a7..368df68 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -22,11 +22,12 @@ mod se { } mod de { - use crate::HashMap; + use crate::SizedHashMap; use core::hash::Hash; use core::marker::PhantomData; use serde::de::{Deserialize, Deserializer, MapAccess, Visitor}; use std::fmt; + use std::hash::BuildHasher; mod size_hint { use core::cmp; @@ -40,12 +41,13 @@ mod de { } } - impl<'de, K, V> Deserialize<'de> for HashMap + impl<'de, K, V, H, const N: usize> Deserialize<'de> for SizedHashMap where K: Eq + Hash + Deserialize<'de>, V: Deserialize<'de>, + H: Default + BuildHasher, { - fn deserialize(deserializer: D) -> Result, D::Error> + fn deserialize(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { @@ -55,19 +57,21 @@ mod de { } } - struct HashMapVisitor + struct HashMapVisitor where K: Eq + Hash, + H: Default + BuildHasher, { - marker: PhantomData>, + marker: PhantomData>, } - impl<'de, K, V> Visitor<'de> for HashMapVisitor + impl<'de, K, V, H, const N: usize> Visitor<'de> for HashMapVisitor where K: Eq + Hash + Deserialize<'de>, V: Deserialize<'de>, + H: Default + BuildHasher, { - type Value = HashMap; + type Value = SizedHashMap; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("an Object/Map structure") @@ -79,7 +83,7 @@ mod de { { let size = size_hint::cautious(map.size_hint()); - let mut m = HashMap::with_capacity(size); + let mut m = SizedHashMap::with_capacity_and_hasher(size, H::default()); while let Some(k) = map.next_key()? { let v = map.next_value()?; m.insert(k, v);