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 {...@@ -2256,20 +2256,20 @@ test writeVarPackedInt {
2256/// Swap the byte order of all the members of the fields of a struct2256/// Swap the byte order of all the members of the fields of a struct
2257/// (Changing their endianness)2257/// (Changing their endianness)
2258pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {2258pub fn byteSwapAllFields(comptime S: type, ptr: *S) void {
2259 byteSwapAllFieldsAligned(S, @alignOf(S), ptr);2259 byteSwapAllFieldsAligned(S, .of(S), ptr);
2260}2260}
22612261
2262/// Swap the byte order of all the members of the fields of a struct2262/// Swap the byte order of all the members of the fields of a struct
2263/// (Changing their endianness)2263/// (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 {
2265 switch (@typeInfo(S)) {2265 switch (@typeInfo(S)) {
2266 .@"struct" => |struct_info| {2266 .@"struct" => |struct_info| {
2267 if (struct_info.backing_integer) |Int| {2267 if (struct_info.backing_integer) |Int| {
2268 ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*))));2268 ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*))));
2269 } else inline for (std.meta.fields(S)) |f| {2269 } else inline for (std.meta.fields(S)) |f| {
2270 switch (@typeInfo(f.type)) {2270 switch (@typeInfo(f.type)) {
2271 .@"struct" => 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, f.alignment, &@field(ptr, f.name)),2272 .@"union", .array => byteSwapAllFieldsAligned(f.type, .fromByteUnits(f.alignment), &@field(ptr, f.name)),
2273 .@"enum" => {2273 .@"enum" => {
2274 @field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name))));2274 @field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name))));
2275 },2275 },