disambiguate enum blocks and complete distinct type operations
This commit is contained in:
@@ -316,6 +316,21 @@ hide format_field_name func($T type, index usize) []u8 {
|
||||
else: compile_error!("io.print arguments must be a tuple")
|
||||
}
|
||||
}
|
||||
hide distinct_value func($Backing, $Distinct type, value Distinct) Backing {
|
||||
return ptrcast!(Backing, &value)^
|
||||
}
|
||||
|
||||
hide scalar_or_distinct_type func($T type) bool {
|
||||
match typeinfo!(T) {
|
||||
.bool: return true
|
||||
.integer: return true
|
||||
.float: return true
|
||||
.distinct: return true
|
||||
else: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
hide write_integer func(output Writer, $T type, value T, base u64, uppercase bool) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
@@ -324,6 +339,11 @@ hide write_integer func(output Writer, $T type, value T, base u64, uppercase boo
|
||||
} else {
|
||||
try write_integer_unsigned(output, u64(value), base, uppercase)
|
||||
}
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
try write_integer(output, distinct_value(backing, T, value), base, uppercase)
|
||||
} else {
|
||||
compile_error!("io.print integer format requires an integer argument")
|
||||
}
|
||||
else: compile_error!("io.print integer format requires an integer argument")
|
||||
}
|
||||
return
|
||||
@@ -351,6 +371,11 @@ hide write_float func(output Writer, $T type, value T, scientific bool) void ! W
|
||||
}
|
||||
try write_all(output, buffer[0..usize(count)])
|
||||
}
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
try write_float(output, distinct_value(backing, T, value), scientific)
|
||||
} else {
|
||||
compile_error!("io.print float format requires a float argument")
|
||||
}
|
||||
else: compile_error!("io.print float format requires a float argument")
|
||||
}
|
||||
return
|
||||
@@ -360,6 +385,11 @@ hide write_decimal func(output Writer, $T type, value T) void ! WriteError {
|
||||
match typeinfo!(T) {
|
||||
.integer: try write_integer(output, value, 10, false)
|
||||
.float: try write_float(output, value, false)
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
try write_decimal(output, distinct_value(backing, T, value))
|
||||
} else {
|
||||
compile_error!("io.print '{d}' requires an integer or float argument")
|
||||
}
|
||||
else: compile_error!("io.print '{d}' requires an integer or float argument")
|
||||
}
|
||||
return
|
||||
@@ -374,6 +404,11 @@ hide write_character func(output Writer, $T type, value T) void ! WriteError {
|
||||
buffer [1]u8 = [u8(value)]
|
||||
try write_all(output, buffer[..])
|
||||
}
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
try write_character(output, distinct_value(backing, T, value))
|
||||
} else {
|
||||
compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
|
||||
}
|
||||
else: compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
|
||||
}
|
||||
return
|
||||
@@ -401,6 +436,11 @@ hide write_default func(output Writer, $T type, value T) void ! WriteError {
|
||||
}
|
||||
return .write_failed
|
||||
}
|
||||
.distinct |backing|: if scalar_or_distinct_type(backing) {
|
||||
try write_default(output, distinct_value(backing, T, value))
|
||||
} else {
|
||||
compile_error!("io.print '{}' does not support this argument type")
|
||||
}
|
||||
else: compile_error!("io.print '{}' does not support this argument type")
|
||||
}
|
||||
return
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ TypeInfo :: union(enum) {
|
||||
record RecordInfo
|
||||
union void
|
||||
fallible void
|
||||
distinct void
|
||||
distinct type
|
||||
}
|
||||
|
||||
EnumFieldStruct func($E, $Field type, $default ?Field) type {
|
||||
|
||||
@@ -8,6 +8,10 @@ TestTokenKind :: enum(u8) {
|
||||
|
||||
TestNames :: alias EnumFieldStruct(TestTokenKind, ?[]u8, some!(null))
|
||||
TestArrayAlias :: alias [3]u16
|
||||
TestInner :: distinct u16
|
||||
TestOuter :: distinct TestInner
|
||||
TestOuterAlias :: alias TestOuter
|
||||
|
||||
|
||||
hide array_info_matches func($Array, $Child type, $len usize) bool {
|
||||
match typeinfo!(Array) {
|
||||
@@ -15,6 +19,13 @@ hide array_info_matches func($Array, $Child type, $len usize) bool {
|
||||
else: return false
|
||||
}
|
||||
}
|
||||
hide distinct_info_matches func($Distinct, $Backing type) bool {
|
||||
match typeinfo!(Distinct) {
|
||||
.distinct |backing|: return backing == Backing
|
||||
else: return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
array_reflection_exposes_child_and_logical_length test {
|
||||
try testing.expect($(array_info_matches([4]i32, i32, 4)))
|
||||
@@ -23,6 +34,12 @@ array_reflection_exposes_child_and_logical_length test {
|
||||
try testing.expect($(array_info_matches([2]mut i64, i64, 2)))
|
||||
try testing.expect($(array_info_matches([2;0]u8, u8, 2)))
|
||||
}
|
||||
distinct_reflection_exposes_immediate_backing test {
|
||||
try testing.expect($(distinct_info_matches(TestInner, u16)))
|
||||
try testing.expect($(distinct_info_matches(TestOuter, TestInner)))
|
||||
try testing.expect($(distinct_info_matches(TestOuterAlias, TestInner)))
|
||||
}
|
||||
|
||||
|
||||
enum_field_struct_defaults test {
|
||||
names TestNames = {
|
||||
|
||||
Reference in New Issue
Block a user