From 099d0b8a9069a42d195c2cd93f584062352084a4 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 9 Oct 2023 18:15:14 +0200 Subject: [PATCH] move MapType boxing to AlgebraicType (#388) --- crates/sats/src/algebraic_type.rs | 3 +-- crates/sats/src/algebraic_type/map_notation.rs | 4 ++-- crates/sats/src/builtin_type.rs | 2 +- crates/sats/src/de/impls.rs | 6 +++--- crates/sats/src/map_type.rs | 9 +++------ crates/sats/src/resolve_refs.rs | 10 +++++----- crates/sats/src/ser/impls.rs | 6 +++--- 7 files changed, 18 insertions(+), 22 deletions(-) diff --git a/crates/sats/src/algebraic_type.rs b/crates/sats/src/algebraic_type.rs index eb0912ae62..6c4e54ca6a 100644 --- a/crates/sats/src/algebraic_type.rs +++ b/crates/sats/src/algebraic_type.rs @@ -224,8 +224,7 @@ impl AlgebraicType { /// Returns a map type from the type `key` to the type `value`. pub fn map(key: Self, value: Self) -> Self { - let value = MapType::new(key, value); - AlgebraicType::Builtin(BuiltinType::Map(value)) + AlgebraicType::Builtin(BuiltinType::Map(Box::new(MapType::new(key, value)))) } /// Returns a sum type of unit variants with names taken from `var_names`. diff --git a/crates/sats/src/algebraic_type/map_notation.rs b/crates/sats/src/algebraic_type/map_notation.rs index a446cbde8e..2d719a57c9 100644 --- a/crates/sats/src/algebraic_type/map_notation.rs +++ b/crates/sats/src/algebraic_type/map_notation.rs @@ -1,7 +1,7 @@ use super::AlgebraicType; use crate::builtin_type::BuiltinType; use crate::de::fmt_fn; -use crate::{ArrayType, MapType}; +use crate::ArrayType; use std::fmt::{self, Formatter}; /// Wraps an algebraic `ty` in a `Display` impl using a object/map JSON-like notation. @@ -49,7 +49,7 @@ pub fn fmt_algebraic_type(ty: &AlgebraicType) -> impl '_ + fmt::Display { BuiltinType::F64 => write!(f, ", 0: F64")?, BuiltinType::String => write!(f, ", 0: String")?, BuiltinType::Array(ArrayType { elem_ty }) => write!(f, ", 0: Array, 1: {}", fmt(elem_ty))?, - BuiltinType::Map(MapType { key_ty, ty }) => write!(f, "0: Map, 1: {}, 2: {}", fmt(key_ty), fmt(ty))?, + BuiltinType::Map(map) => write!(f, "0: Map, 1: {}, 2: {}", fmt(&map.key_ty), fmt(&map.ty))?, } write!(f, " }}") } diff --git a/crates/sats/src/builtin_type.rs b/crates/sats/src/builtin_type.rs index 489d533aea..ac59c5ba46 100644 --- a/crates/sats/src/builtin_type.rs +++ b/crates/sats/src/builtin_type.rs @@ -49,7 +49,7 @@ pub enum BuiltinType { /// The type of map values consisting of a key type `key_ty` and value `ty`. /// Values [`BuiltinValue::Map(map)`](crate::BuiltinValue::Map) will have this type. /// The order of entries in a map value is observable. - Map(MapType), + Map(Box), } impl MetaType for BuiltinType { diff --git a/crates/sats/src/de/impls.rs b/crates/sats/src/de/impls.rs index ca9f1ba703..e1b7260221 100644 --- a/crates/sats/src/de/impls.rs +++ b/crates/sats/src/de/impls.rs @@ -328,7 +328,7 @@ impl<'de> DeserializeSeed<'de> for WithTypespace<'_, BuiltinType> { BuiltinType::F64 => f64::deserialize(deserializer).map(Into::into), BuiltinType::String => String::deserialize(deserializer).map(Into::into), BuiltinType::Array(ty) => self.with(ty).deserialize(deserializer).map(Into::into), - BuiltinType::Map(ty) => self.with(ty).deserialize(deserializer).map(Into::into), + BuiltinType::Map(ty) => self.with(&**ty).deserialize(deserializer).map(Into::into), } } } @@ -466,7 +466,7 @@ impl<'de> DeserializeSeed<'de> for WithTypespace<'_, ArrayType> { .deserialize_array_seed(BasicVecVisitor, self.with(ty)) .map(ArrayValue::Array), AlgebraicType::Builtin(BuiltinType::Map(ty)) => deserializer - .deserialize_array_seed(BasicVecVisitor, self.with(ty)) + .deserialize_array_seed(BasicVecVisitor, self.with(&**ty)) .map(ArrayValue::Map), }; } @@ -478,7 +478,7 @@ impl<'de> DeserializeSeed<'de> for WithTypespace<'_, MapType> { fn deserialize>(self, deserializer: D) -> Result { let MapType { key_ty, ty } = self.ty(); - deserializer.deserialize_map_seed(BasicMapVisitor, self.with(&**key_ty), self.with(&**ty)) + deserializer.deserialize_map_seed(BasicMapVisitor, self.with(key_ty), self.with(ty)) } } diff --git a/crates/sats/src/map_type.rs b/crates/sats/src/map_type.rs index 1a72cef937..8b9ba7fbc3 100644 --- a/crates/sats/src/map_type.rs +++ b/crates/sats/src/map_type.rs @@ -5,18 +5,15 @@ use crate::{de::Deserialize, meta_type::MetaType, ser::Serialize, AlgebraicType} #[sats(crate = crate)] pub struct MapType { /// The key type of the map. - pub key_ty: Box, + pub key_ty: AlgebraicType, /// The value type of the map. - pub ty: Box, + pub ty: AlgebraicType, } impl MapType { /// Returns a map type with keys of type `key` and values of type `value`. pub fn new(key: AlgebraicType, value: AlgebraicType) -> Self { - Self { - key_ty: Box::new(key), - ty: Box::new(value), - } + Self { key_ty: key, ty: value } } } diff --git a/crates/sats/src/resolve_refs.rs b/crates/sats/src/resolve_refs.rs index a1db00957f..243871b596 100644 --- a/crates/sats/src/resolve_refs.rs +++ b/crates/sats/src/resolve_refs.rs @@ -78,7 +78,7 @@ impl ResolveRefs for BuiltinType { fn resolve_refs(this: WithTypespace<'_, Self>, state: &mut ResolveRefState) -> Option { match this.ty() { BuiltinType::Array(ty) => this.with(ty)._resolve_refs(state).map(Self::Array), - BuiltinType::Map(m) => this.with(m)._resolve_refs(state).map(Self::Map), + BuiltinType::Map(m) => this.with(&**m)._resolve_refs(state).map(Box::new).map(Self::Map), // These types are plain and cannot have refs in them. x => Some(x.clone()), } @@ -95,11 +95,11 @@ impl ResolveRefs for ArrayType { } impl ResolveRefs for MapType { - type Output = MapType; + type Output = Self; fn resolve_refs(this: WithTypespace<'_, Self>, state: &mut ResolveRefState) -> Option { - Some(MapType { - key_ty: Box::new(this.map(|m| &*m.key_ty)._resolve_refs(state)?), - ty: Box::new(this.map(|m| &*m.ty)._resolve_refs(state)?), + Some(Self { + key_ty: this.map(|m| &m.key_ty)._resolve_refs(state)?, + ty: this.map(|m| &m.ty)._resolve_refs(state)?, }) } } diff --git a/crates/sats/src/ser/impls.rs b/crates/sats/src/ser/impls.rs index 72ea148e9e..6a8db584f0 100644 --- a/crates/sats/src/ser/impls.rs +++ b/crates/sats/src/ser/impls.rs @@ -173,7 +173,7 @@ impl_serialize!([] ValueWithType<'_, BuiltinValue>, (self, ser) => match (self.v (BuiltinValue::F64(v), BuiltinType::F64) => ser.serialize_f64((*v).into()), (BuiltinValue::String(s), BuiltinType::String) => ser.serialize_str(s), (BuiltinValue::Array { val }, BuiltinType::Array(ty)) => self.with(ty, val).serialize(ser), - (BuiltinValue::Map { val }, BuiltinType::Map(ty)) => self.with(ty, val).serialize(ser), + (BuiltinValue::Map { val }, BuiltinType::Map(ty)) => self.with(&**ty, val).serialize(ser), (val, ty) => panic!("mismatched value and schema: {val:?} {ty:?}"), }); impl_serialize!( @@ -221,7 +221,7 @@ impl_serialize!([] ValueWithType<'_, ArrayValue>, (self, ser) => match (self.val (ArrayValue::Array(v), AlgebraicType::Builtin(BuiltinType::Array(ty))) => { self.with(ty, v).serialize(ser) } - (ArrayValue::Map(v), AlgebraicType::Builtin(BuiltinType::Map(m))) => self.with(m, v).serialize(ser), + (ArrayValue::Map(v), AlgebraicType::Builtin(BuiltinType::Map(m))) => self.with(&**m, v).serialize(ser), (val, _) if val.is_empty() => ser.serialize_array(0)?.end(), (val, ty) => panic!("mismatched value and schema: {val:?} {ty:?}"), }); @@ -230,7 +230,7 @@ impl_serialize!([] ValueWithType<'_, MapValue>, (self, ser) => { let MapType { key_ty, ty } = self.ty(); let mut map = ser.serialize_map(val.len())?; for (key, val) in val { - map.serialize_entry(&self.with(&**key_ty, key), &self.with(&**ty, val))?; + map.serialize_entry(&self.with(key_ty, key), &self.with(ty, val))?; } map.end() });