Pull the hashmap Serialize/Deserialize impl into a module

This commit is contained in:
Gwilym Inzani 2024-10-30 10:36:36 +00:00
parent b2d0e93f8c
commit 69219e3d73

View file

@ -7,9 +7,12 @@ use serde::{
use crate::{ClonableAllocator, HashMap};
impl<K: Serialize, V: Serialize, ALLOCATOR: ClonableAllocator> Serialize
mod hashmap {
use super::*;
impl<K: Serialize, V: Serialize, ALLOCATOR: ClonableAllocator> Serialize
for HashMap<K, V, ALLOCATOR>
{
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
@ -22,39 +25,39 @@ impl<K: Serialize, V: Serialize, ALLOCATOR: ClonableAllocator> Serialize
map.end()
}
}
}
impl<'de, K, V> Deserialize<'de> for HashMap<K, V>
where
impl<'de, K, V> Deserialize<'de> for HashMap<K, V>
where
K: Deserialize<'de> + Hash + Eq,
V: Deserialize<'de>,
{
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(HashMapVisitor::new())
}
}
}
#[derive(Default)]
struct HashMapVisitor<K, V> {
#[derive(Default)]
struct HashMapVisitor<K, V> {
_marker: PhantomData<fn() -> HashMap<K, V>>,
}
}
impl<K, V> HashMapVisitor<K, V> {
impl<K, V> HashMapVisitor<K, V> {
fn new() -> Self {
Self {
_marker: PhantomData,
}
}
}
}
impl<'de, K, V> Visitor<'de> for HashMapVisitor<K, V>
where
impl<'de, K, V> Visitor<'de> for HashMapVisitor<K, V>
where
K: Deserialize<'de> + Hash + Eq,
V: Deserialize<'de>,
{
{
type Value = HashMap<K, V>;
fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
@ -73,4 +76,5 @@ where
Ok(map)
}
}
}