use std::cell::RefCell; use std::rc::Rc; use lattices::{Point, WithBot}; use serde::de::{DeserializeSeed, Visitor}; use serde::{Deserializer, Serialize, Serializer}; use super::point::PointWrapper; use crate::buffer_pool::{AutoReturnBuffer, BufferPool}; use crate::protocol::serialization::lattices::point::PointDeserializer; #[repr(transparent)] pub struct WithBotWrapper<'a, const SIZE: usize>( pub &'a WithBot, ()>>, ); impl Serialize for WithBotWrapper<'_, SIZE> { fn serialize(&self, serializer: S) -> Result where S: Serializer, { if let Some(inner) = self.0.as_reveal_ref() { serializer.serialize_some(&PointWrapper(inner)) } else { serializer.serialize_none() } } } pub struct WithBotDeserializer { pub collector: Rc>>, } impl<'de, const SIZE: usize> DeserializeSeed<'de> for WithBotDeserializer { type Value = WithBot, ()>>; 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 = WithBot, ()>>; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str(std::any::type_name::()) } fn visit_some(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>, { DeserializeSeed::deserialize( PointDeserializer { collector: self.collector, }, deserializer, ) } } let inner = deserializer.deserialize_newtype_struct( "Point", V { collector: self.collector, }, )?; Ok(WithBot::new(Some(inner))) } fn visit_none(self) -> Result where E: serde::de::Error, { Ok(WithBot::new(None)) } } deserializer.deserialize_option(V { collector: self.collector, }) } }