Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.meta.hasUniqueRepresentation: better support packed structs #20143

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
.Array => |info| hasUniqueRepresentation(info.child),

.Struct => |info| {
if (info.layout == .@"packed" and @sizeOf(T) == @bitSizeOf(T) / 8) return true;
nektro marked this conversation as resolved.
Show resolved Hide resolved

var sum_size = @as(usize, 0);

inline for (info.fields) |field| {
Expand Down Expand Up @@ -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,
Expand Down
Loading