mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
std.meta.eql: use == directly when comparing packed structs (#21982)
This commit is contained in:
parent
c894ac09a3
commit
b2030cb9ab
@ -737,13 +737,15 @@ test TagPayload {
|
|||||||
try testing.expect(MovedEvent == @TypeOf(e.Moved));
|
try testing.expect(MovedEvent == @TypeOf(e.Moved));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compares two of any type for equality. Containers are compared on a field-by-field basis,
|
/// Compares two of any type for equality. Containers that do not support comparison
|
||||||
/// where possible. Pointers are not followed.
|
/// on their own are compared on a field-by-field basis. Pointers are not followed.
|
||||||
pub fn eql(a: anytype, b: @TypeOf(a)) bool {
|
pub fn eql(a: anytype, b: @TypeOf(a)) bool {
|
||||||
const T = @TypeOf(a);
|
const T = @TypeOf(a);
|
||||||
|
|
||||||
switch (@typeInfo(T)) {
|
switch (@typeInfo(T)) {
|
||||||
.@"struct" => |info| {
|
.@"struct" => |info| {
|
||||||
|
if (info.layout == .@"packed") return a == b;
|
||||||
|
|
||||||
inline for (info.fields) |field_info| {
|
inline for (info.fields) |field_info| {
|
||||||
if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
|
if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user