1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 20:42:21 +02:00

Predetermine enum discriminator during comptime

This commit is contained in:
Brian Fiete 2021-12-21 12:52:51 -05:00
parent 8807c1ea83
commit 326c33eaa1
4 changed files with 228 additions and 159 deletions

View file

@ -230,6 +230,38 @@ namespace Tests
}
}
[CheckEnum]
enum EnumA
{
case A(int64 aa);
case B(float bb);
}
[AttributeUsage(.All)]
public struct CheckEnumAttribute : Attribute, IComptimeTypeApply
{
public void ApplyToType(Type type)
{
int fieldIdx = 0;
for (var field in type.GetFields())
{
switch (fieldIdx)
{
case 0:
Test.Assert(field.Name == "$payload");
Test.Assert(field.MemberOffset == 0);
Test.Assert(field.FieldType == typeof(int64));
case 1:
Test.Assert(field.Name == "$discriminator");
Test.Assert(field.MemberOffset == 8);
Test.Assert(field.FieldType == typeof(int8));
}
fieldIdx++;
}
Test.Assert(fieldIdx == 4);
}
}
const String cTest0 = Compiler.ReadText("Test0.txt");
const String cTest1 = new String('A', 12);
const uint8[?] cTest0Binary = Compiler.ReadBinary("Test0.txt");