1
0
Fork 0

add StatePartEnum/TypePartEnum

This commit is contained in:
Jacob Lifshay 2025-09-03 22:25:46 -07:00
parent 6d36698adf
commit fc7ab51620
Signed by: programmerjake
SSH key fingerprint: SHA256:HnFTLGpSm4Q4Fj502oCFisjZSoakwEuTsJJMSke63RQ

View file

@ -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<H: Hasher>(&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 { 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 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 impl $TypeStatePartKind:ident for $TypeKind:ident $type_impl_body:tt
)* )*
) => { ) => {
@ -936,6 +1018,31 @@ macro_rules! make_state_part_kinds {
} }
} }
pub(crate) enum StatePartEnum<V: StatePartsValue> {
Type(TypePartEnum<V>),
$($state_variant(V::Value<$StateKind>),)*
}
impl_state_parts_traits! {
enum StatePartEnum<V: StatePartsValue> {
#[flatten]
Type(TypePartEnum<V>),
$(#[variant_in_flattened]
$type_variant(V::Value<$TypeKind>),)*
$($state_variant(V::Value<$StateKind>),)*
}
}
pub(crate) enum TypePartEnum<V: StatePartsValue> {
$($type_variant(V::Value<$TypeKind>),)*
}
impl_state_parts_traits! {
enum TypePartEnum<V: StatePartsValue> {
$($type_variant(V::Value<$TypeKind>),)*
}
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) struct StateLayout<BK: InsnsBuildingKind> { pub(crate) struct StateLayout<BK: InsnsBuildingKind> {
pub(crate) ty: TypeLayout<BK>, pub(crate) ty: TypeLayout<BK>,
@ -1395,7 +1502,7 @@ impl<T: Deref<Target = BitSlice>> fmt::Debug for MemoryData<T> {
} }
make_state_part_kinds! { make_state_part_kinds! {
/*#[state, field = small_stack] /*#[state, field = small_stack, variant = SmallStack]
impl StatePartKind for StatePartKindSmallStack { impl StatePartKind for StatePartKindSmallStack {
const NAME: &'static str = "SmallStack"; const NAME: &'static str = "SmallStack";
type DebugData = (); type DebugData = ();
@ -1415,7 +1522,7 @@ make_state_part_kinds! {
state_layout.small_stack.debug_data.get(part_index.as_usize()) 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 { impl StatePartKind for StatePartKindBigStack {
const NAME: &'static str = "BigStack"; const NAME: &'static str = "BigStack";
type DebugData = (); type DebugData = ();
@ -1435,7 +1542,7 @@ make_state_part_kinds! {
state_layout.big_stack.debug_data.get(part_index.as_usize()) state_layout.big_stack.debug_data.get(part_index.as_usize())
} }
}*/ }*/
#[state, field = memories] #[state, field = memories, variant = Memory]
impl StatePartKind for StatePartKindMemories { impl StatePartKind for StatePartKindMemories {
const NAME: &'static str = "Memories"; const NAME: &'static str = "Memories";
type DebugData = (); type DebugData = ();
@ -1465,7 +1572,7 @@ make_state_part_kinds! {
write!(f, "{:#?}", &state.memories[index]) write!(f, "{:#?}", &state.memories[index])
} }
} }
#[type, field = small_slots] #[type, field = small_slots, variant = SmallSlot]
impl StatePartKind for StatePartKindSmallSlots { impl StatePartKind for StatePartKindSmallSlots {
const NAME: &'static str = "SmallSlots"; const NAME: &'static str = "SmallSlots";
type DebugData = SlotDebugData; type DebugData = SlotDebugData;
@ -1494,7 +1601,7 @@ make_state_part_kinds! {
Ok(()) Ok(())
} }
} }
#[type, field = big_slots] #[type, field = big_slots, variant = BigSlot]
impl StatePartKind for StatePartKindBigSlots { impl StatePartKind for StatePartKindBigSlots {
const NAME: &'static str = "BigSlots"; const NAME: &'static str = "BigSlots";
type DebugData = SlotDebugData; type DebugData = SlotDebugData;