use std::cell::RefCell; use std::rc::Rc; use lattices::Point; use serde::de::{DeserializeSeed, Visitor}; use serde::{Deserializer, Serialize, Serializer}; use crate::buffer_pool::{AutoReturnBuffer, AutoReturnBufferDeserializer, BufferPool}; #[repr(transparent)] pub struct PointWrapper<'a, const SIZE: usize>(pub &'a Point, ()>); impl Serialize for PointWrapper<'_, SIZE> { fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_newtype_struct("Point", &self.0.val) } } pub struct PointDeserializer { pub collector: Rc>>, } impl<'de, const SIZE: usize> DeserializeSeed<'de> for PointDeserializer { type Value = Point, ()>; fn deserialize(self, deserializer: D) -> Result where D: Deserializer<'de>, { struct V { pub collector: Rc>>, } impl<'de, const SIZE: usize> Visitor<'de> for V { type Value = Point, ()>; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str(std::any::type_name::()) } fn visit_newtype_struct(self, deserializer: D) -> Result where D: Deserializer<'de>, { struct V { pub collector: Rc>>, } impl<'de, const SIZE: usize> Visitor<'de> for V { type Value = AutoReturnBuffer; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str(std::any::type_name::()) } fn visit_newtype_struct( self, deserializer: D, ) -> Result where D: Deserializer<'de>, { DeserializeSeed::deserialize( AutoReturnBufferDeserializer { collector: self.collector, }, deserializer, ) } } let inner = deserializer.deserialize_newtype_struct( "Point", V { collector: self.collector, }, )?; Ok(Point::new(inner)) } } deserializer.deserialize_newtype_struct( "Point", V { collector: self.collector, }, ) } }