Skip to content

Commit

Permalink
move MapType boxing to AlgebraicType (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored Oct 9, 2023
1 parent 3d0d7da commit 099d0b8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
3 changes: 1 addition & 2 deletions crates/sats/src/algebraic_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions crates/sats/src/algebraic_type/map_notation.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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, " }}")
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sats/src/builtin_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapType>),
}

impl MetaType for BuiltinType {
Expand Down
6 changes: 3 additions & 3 deletions crates/sats/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -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),
};
}
Expand All @@ -478,7 +478,7 @@ impl<'de> DeserializeSeed<'de> for WithTypespace<'_, MapType> {

fn deserialize<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Output, D::Error> {
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))
}
}

Expand Down
9 changes: 3 additions & 6 deletions crates/sats/src/map_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlgebraicType>,
pub key_ty: AlgebraicType,
/// The value type of the map.
pub ty: Box<AlgebraicType>,
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 }
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/sats/src/resolve_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ResolveRefs for BuiltinType {
fn resolve_refs(this: WithTypespace<'_, Self>, state: &mut ResolveRefState) -> Option<Self::Output> {
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()),
}
Expand All @@ -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<Self::Output> {
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)?,
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/sats/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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:?}"),
});
Expand All @@ -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()
});

0 comments on commit 099d0b8

Please sign in to comment.