From 52e0f7870695f11eea7700df9a5c07025dc7d5a2 Mon Sep 17 00:00:00 2001 From: Steven Casper Date: Thu, 8 Jan 2026 00:44:08 +0100 Subject: [PATCH] byteSwapAllFieldsAligned: use std.mem.Alignment API (#30724) Following up on #30571 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30724 Reviewed-by: Andrew Kelley Co-authored-by: Steven Casper Co-committed-by: Steven Casper --- lib/std/mem.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/mem.zig b/lib/std/mem.zig index b2a71cfd6b24a7d0cd501ff0fd695acee7aa073b..b47d228f8636e2215b811145f0f07a66f002c419 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -2256,20 +2256,20 @@ test writeVarPackedInt { /// Swap the byte order of all the members of the fields of a struct /// (Changing their endianness) pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { - byteSwapAllFieldsAligned(S, @alignOf(S), ptr); + byteSwapAllFieldsAligned(S, .of(S), ptr); } /// Swap the byte order of all the members of the fields of a struct /// (Changing their endianness) -pub fn byteSwapAllFieldsAligned(comptime S: type, comptime A: comptime_int, ptr: *align(A) S) void { +pub fn byteSwapAllFieldsAligned(comptime S: type, comptime a: Alignment, ptr: *align(a.toByteUnits()) S) void { switch (@typeInfo(S)) { .@"struct" => |struct_info| { if (struct_info.backing_integer) |Int| { ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*)))); } else inline for (std.meta.fields(S)) |f| { switch (@typeInfo(f.type)) { - .@"struct" => byteSwapAllFieldsAligned(f.type, f.alignment, &@field(ptr, f.name)), - .@"union", .array => byteSwapAllFieldsAligned(f.type, f.alignment, &@field(ptr, f.name)), + .@"struct" => byteSwapAllFieldsAligned(f.type, .fromByteUnits(f.alignment), &@field(ptr, f.name)), + .@"union", .array => byteSwapAllFieldsAligned(f.type, .fromByteUnits(f.alignment), &@field(ptr, f.name)), .@"enum" => { @field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name)))); }, -- 2.54.0