authorgravatar for sebastiancasper3@gmail.comSteven Casper <sebastiancasper3@gmail.com> 2026-01-08 00:44:08+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-08 00:44:08+01:00
log52e0f7870695f11eea7700df9a5c07025dc7d5a2
treed4dc0afea6e9682a700b88ee7399763a1dafc2a7
parent335c0fcba1bad1b3b391cdbf8b3b037267a6fee7

byteSwapAllFieldsAligned: use std.mem.Alignment API (#30724)

Following up on #30571 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30724 Reviewed-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: Steven Casper <sebastiancasper3@gmail.com> Co-committed-by: Steven Casper <sebastiancasper3@gmail.com>

1 files changed, 4 insertions(+), 4 deletions(-)

lib/std/mem.zig+4-4
......@@ -2256,20 +2256,20 @@ test writeVarPackedInt {
22562256/// Swap the byte order of all the members of the fields of a struct
22572257/// (Changing their endianness)
22582258pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
2259 byteSwapAllFieldsAligned(S, @alignOf(S), ptr);
2259 byteSwapAllFieldsAligned(S, .of(S), ptr);
22602260}
22612261
22622262/// Swap the byte order of all the members of the fields of a struct
22632263/// (Changing their endianness)
2264pub fn byteSwapAllFieldsAligned(comptime S: type, comptime A: comptime_int, ptr: *align(A) S) void {
2264pub fn byteSwapAllFieldsAligned(comptime S: type, comptime a: Alignment, ptr: *align(a.toByteUnits()) S) void {
22652265 switch (@typeInfo(S)) {
22662266 .@"struct" => |struct_info| {
22672267 if (struct_info.backing_integer) |Int| {
22682268 ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*))));
22692269 } else inline for (std.meta.fields(S)) |f| {
22702270 switch (@typeInfo(f.type)) {
2271 .@"struct" => byteSwapAllFieldsAligned(f.type, f.alignment, &@field(ptr, f.name)),
2272 .@"union", .array => byteSwapAllFieldsAligned(f.type, f.alignment, &@field(ptr, f.name)),
2271 .@"struct" => byteSwapAllFieldsAligned(f.type, .fromByteUnits(f.alignment), &@field(ptr, f.name)),
2272 .@"union", .array => byteSwapAllFieldsAligned(f.type, .fromByteUnits(f.alignment), &@field(ptr, f.name)),
22732273 .@"enum" => {
22742274 @field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name))));
22752275 },