diff --git a/agb-hashmap/src/serde.rs b/agb-hashmap/src/serde.rs index e014120f..7b081de4 100644 --- a/agb-hashmap/src/serde.rs +++ b/agb-hashmap/src/serde.rs @@ -7,70 +7,74 @@ use serde::{ use crate::{ClonableAllocator, HashMap}; -impl Serialize - for HashMap -{ - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - let mut map = serializer.serialize_map(Some(self.len()))?; +mod hashmap { + use super::*; - for (key, value) in self { - map.serialize_entry(key, value)?; + impl Serialize + for HashMap + { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let mut map = serializer.serialize_map(Some(self.len()))?; + + for (key, value) in self { + map.serialize_entry(key, value)?; + } + + map.end() + } + } + + impl<'de, K, V> Deserialize<'de> for HashMap + where + K: Deserialize<'de> + Hash + Eq, + V: Deserialize<'de>, + { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_map(HashMapVisitor::new()) + } + } + + #[derive(Default)] + struct HashMapVisitor { + _marker: PhantomData HashMap>, + } + + impl HashMapVisitor { + fn new() -> Self { + Self { + _marker: PhantomData, + } + } + } + + impl<'de, K, V> Visitor<'de> for HashMapVisitor + where + K: Deserialize<'de> + Hash + Eq, + V: Deserialize<'de>, + { + type Value = HashMap; + + fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result { + formatter.write_str("an agb::HashMap") } - map.end() - } -} + fn visit_map(self, mut access: M) -> Result + where + M: MapAccess<'de>, + { + let mut map = HashMap::with_capacity(access.size_hint().unwrap_or(8)); -impl<'de, K, V> Deserialize<'de> for HashMap -where - K: Deserialize<'de> + Hash + Eq, - V: Deserialize<'de>, -{ - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - deserializer.deserialize_map(HashMapVisitor::new()) - } -} + while let Some((key, value)) = access.next_entry()? { + map.insert(key, value); + } -#[derive(Default)] -struct HashMapVisitor { - _marker: PhantomData HashMap>, -} - -impl HashMapVisitor { - fn new() -> Self { - Self { - _marker: PhantomData, + Ok(map) } } } - -impl<'de, K, V> Visitor<'de> for HashMapVisitor -where - K: Deserialize<'de> + Hash + Eq, - V: Deserialize<'de>, -{ - type Value = HashMap; - - fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result { - formatter.write_str("an agb::HashMap") - } - - fn visit_map(self, mut access: M) -> Result - where - M: MapAccess<'de>, - { - let mut map = HashMap::with_capacity(access.size_hint().unwrap_or(8)); - - while let Some((key, value)) = access.next_entry()? { - map.insert(key, value); - } - - Ok(map) - } -}