From fc7ab51620a4d24c2fb734a4a451c71d0951be92 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Wed, 3 Sep 2025 22:25:46 -0700 Subject: [PATCH] add StatePartEnum/TypePartEnum --- crates/fayalite/src/sim/interpreter.rs | 121 +++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/crates/fayalite/src/sim/interpreter.rs b/crates/fayalite/src/sim/interpreter.rs index 35a25d0c..3711f18c 100644 --- a/crates/fayalite/src/sim/interpreter.rs +++ b/crates/fayalite/src/sim/interpreter.rs @@ -884,16 +884,98 @@ macro_rules! impl_state_parts_traits { } } }; + ( + enum $Enum:ident<$V:ident: $StatePartsValue:ident> { + $(#[flatten] + $flattened_variant:ident($flattened_variant_ty:ty), + $(#[variant_in_flattened] + $variant_in_flattened:ident($variant_in_flattened_ty:ty),)* + )? + $($variant:ident($variant_ty:ty),)* + } + ) => { + impl<$V: $StatePartsValue> Copy for $Enum<$V> + where + $($flattened_variant_ty: Copy,)? + $($variant_ty: Copy,)* + { + } + + impl<$V: $StatePartsValue> Clone for $Enum<$V> + where + $($flattened_variant_ty: Clone,)? + $($variant_ty: Clone,)* + { + fn clone(&self) -> Self { + match self { + $(Self::$flattened_variant(v) => Self::$flattened_variant(v.clone()),)? + $(Self::$variant(v) => Self::$variant(v.clone()),)* + } + } + } + + impl<$V: $StatePartsValue> fmt::Debug for $Enum<$V> + where + $($($variant_in_flattened_ty: fmt::Debug,)*)? + $($variant_ty: fmt::Debug,)* + { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + $(type FlattenedVariantType<$V> = $flattened_variant_ty;)? + match self { + $(Self::$flattened_variant(v) => match v { + $(FlattenedVariantType::$variant_in_flattened(v) => f.debug_tuple(stringify!($variant_in_flattened)).field(v).finish(),)* + },)? + $(Self::$variant(v) => f.debug_tuple(stringify!($variant)).field(v).finish(),)* + } + } + } + + impl<$V: $StatePartsValue> PartialEq for $Enum<$V> + where + $($flattened_variant_ty: PartialEq,)? + $($variant_ty: PartialEq,)* + { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + $((Self::$flattened_variant(this), Self::$flattened_variant(other)) => this == other, + (Self::$flattened_variant(_), _) => false,)? + $((Self::$variant(this), Self::$variant(other)) => this == other, + (Self::$variant(_), _) => false,)* + } + } + } + + impl<$V: $StatePartsValue> Eq for $Enum<$V> + where + $($flattened_variant_ty: Eq,)? + $($variant_ty: Eq,)* + { + } + + impl<$V: $StatePartsValue> Hash for $Enum<$V> + where + $($flattened_variant_ty: Hash,)? + $($variant_ty: Hash,)* + { + fn hash(&self, h: &mut H) { + std::mem::discriminant(self).hash(h); + match self { + $(Self::$flattened_variant(v) => v.hash(h),)? + $(Self::$variant(v) => v.hash(h),)* + } + } + } + }; } macro_rules! make_state_part_kinds { ( $( - #[state, field = $state_field:ident] + #[state, field = $state_field:ident, variant = $state_variant:ident] impl $StateStatePartKind:ident for $StateKind:ident $state_impl_body:tt )* $( - #[type, field = $type_field:ident] + #[type, field = $type_field:ident, variant = $type_variant:ident] impl $TypeStatePartKind:ident for $TypeKind:ident $type_impl_body:tt )* ) => { @@ -936,6 +1018,31 @@ macro_rules! make_state_part_kinds { } } + pub(crate) enum StatePartEnum { + Type(TypePartEnum), + $($state_variant(V::Value<$StateKind>),)* + } + + impl_state_parts_traits! { + enum StatePartEnum { + #[flatten] + Type(TypePartEnum), + $(#[variant_in_flattened] + $type_variant(V::Value<$TypeKind>),)* + $($state_variant(V::Value<$StateKind>),)* + } + } + + pub(crate) enum TypePartEnum { + $($type_variant(V::Value<$TypeKind>),)* + } + + impl_state_parts_traits! { + enum TypePartEnum { + $($type_variant(V::Value<$TypeKind>),)* + } + } + #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub(crate) struct StateLayout { pub(crate) ty: TypeLayout, @@ -1395,7 +1502,7 @@ impl> fmt::Debug for MemoryData { } make_state_part_kinds! { - /*#[state, field = small_stack] + /*#[state, field = small_stack, variant = SmallStack] impl StatePartKind for StatePartKindSmallStack { const NAME: &'static str = "SmallStack"; type DebugData = (); @@ -1415,7 +1522,7 @@ make_state_part_kinds! { state_layout.small_stack.debug_data.get(part_index.as_usize()) } } - #[state, field = big_stack] + #[state, field = big_stack, variant = BigStack] impl StatePartKind for StatePartKindBigStack { const NAME: &'static str = "BigStack"; type DebugData = (); @@ -1435,7 +1542,7 @@ make_state_part_kinds! { state_layout.big_stack.debug_data.get(part_index.as_usize()) } }*/ - #[state, field = memories] + #[state, field = memories, variant = Memory] impl StatePartKind for StatePartKindMemories { const NAME: &'static str = "Memories"; type DebugData = (); @@ -1465,7 +1572,7 @@ make_state_part_kinds! { write!(f, "{:#?}", &state.memories[index]) } } - #[type, field = small_slots] + #[type, field = small_slots, variant = SmallSlot] impl StatePartKind for StatePartKindSmallSlots { const NAME: &'static str = "SmallSlots"; type DebugData = SlotDebugData; @@ -1494,7 +1601,7 @@ make_state_part_kinds! { Ok(()) } } - #[type, field = big_slots] + #[type, field = big_slots, variant = BigSlot] impl StatePartKind for StatePartKindBigSlots { const NAME: &'static str = "BigSlots"; type DebugData = SlotDebugData;