use std::cell::RefCell; use std::rc::Rc; use lattices::map_union::MapUnionHashMap; use serde::de::{DeserializeSeed, Visitor}; use serde::{Serialize, Serializer}; use super::my_last_write_wins::MyLastWriteWinsDeserializer; use crate::buffer_pool::BufferPool; use crate::protocol::serialization::lattices::my_last_write_wins::MyLastWriteWinsWrapper; use crate::protocol::MyLastWriteWins; #[repr(transparent)] pub struct MapUnionHashMapWrapper<'a, const SIZE: usize>( pub &'a MapUnionHashMap>, ); impl Serialize for MapUnionHashMapWrapper<'_, SIZE> { fn serialize(&self, serializer: S) -> Result where S: Serializer, { use serde::ser::SerializeMap; let inner_map = self.0.as_reveal_ref(); let mut map_serializer = serializer.serialize_map(Some(inner_map.len()))?; for (k, v) in inner_map { map_serializer.serialize_entry(k, &MyLastWriteWinsWrapper(v))?; } map_serializer.end() } } pub struct MapUnionHashMapDeserializer { pub collector: Rc>>, } impl<'de, const SIZE: usize> DeserializeSeed<'de> for MapUnionHashMapDeserializer { type Value = MapUnionHashMap>; fn deserialize(self, deserializer: D) -> Result where D: serde::Deserializer<'de>, { struct V { pub collector: Rc>>, } impl<'de, const SIZE: usize> Visitor<'de> for V { type Value = MapUnionHashMap>; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str(std::any::type_name::()) } fn visit_map(self, mut map: A) -> Result where A: serde::de::MapAccess<'de>, { let mut inner_map = MapUnionHashMap::>::default(); loop { let k: Option = map.next_key()?; if let Some(k) = k { inner_map.as_reveal_mut().insert( k, map.next_value_seed(MyLastWriteWinsDeserializer { collector: self.collector.clone(), })?, ); } else { break; } } Ok(inner_map) } } deserializer.deserialize_map(V { collector: self.collector, }) } }