std.meta.hasUniqueRepresentation: better support packed structs

This commit is contained in:
Meghan Denny
2024-06-02 16:29:10 -07:00
committed by GitHub
parent d74180c373
commit db75a8781b

View File

@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
.Array => |info| hasUniqueRepresentation(info.child),
.Struct => |info| {
if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
var sum_size = @as(usize, 0);
inline for (info.fields) |field| {
@@ -1245,6 +1247,19 @@ test hasUniqueRepresentation {
try testing.expect(!hasUniqueRepresentation(TestStruct5));
const TestStruct6 = packed struct(u8) {
@"0": bool,
@"1": bool,
@"2": bool,
@"3": bool,
@"4": bool,
@"5": bool,
@"6": bool,
@"7": bool,
};
try testing.expect(hasUniqueRepresentation(TestStruct6));
const TestUnion1 = packed union {
a: u32,
b: u16,