Function byteSwapAllFields [src]

Swap the byte order of all the members of the fields of a struct (Changing their endianness)

Prototype

pub fn byteSwapAllFields(comptime S: type, ptr: *S) void

Parameters

S: typeptr: *S

Example

test byteSwapAllFields { const T = extern struct { f0: u8, f1: u16, f2: u32, f3: [1]u8, f4: bool, f5: f32, }; const K = extern struct { f0: u8, f1: T, f2: u16, f3: [1]u8, f4: bool, f5: f32, }; var s = T{ .f0 = 0x12, .f1 = 0x1234, .f2 = 0x12345678, .f3 = .{0x12}, .f4 = true, .f5 = @as(f32, @bitCast(@as(u32, 0x4640e400))), }; var k = K{ .f0 = 0x12, .f1 = s, .f2 = 0x1234, .f3 = .{0x12}, .f4 = false, .f5 = @as(f32, @bitCast(@as(u32, 0x45d42800))), }; byteSwapAllFields(T, &s); byteSwapAllFields(K, &k); try std.testing.expectEqual(T{ .f0 = 0x12, .f1 = 0x3412, .f2 = 0x78563412, .f3 = .{0x12}, .f4 = true, .f5 = @as(f32, @bitCast(@as(u32, 0x00e44046))), }, s); try std.testing.expectEqual(K{ .f0 = 0x12, .f1 = s, .f2 = 0x3412, .f3 = .{0x12}, .f4 = false, .f5 = @as(f32, @bitCast(@as(u32, 0x0028d445))), }, k); }

Source

pub fn byteSwapAllFields(comptime S: type, ptr: *S) void { switch (@typeInfo(S)) { .@"struct" => { inline for (std.meta.fields(S)) |f| { switch (@typeInfo(f.type)) { .@"struct" => |struct_info| if (struct_info.backing_integer) |Int| { @field(ptr, f.name) = @bitCast(@byteSwap(@as(Int, @bitCast(@field(ptr, f.name))))); } else { byteSwapAllFields(f.type, &@field(ptr, f.name)); }, .array => byteSwapAllFields(f.type, &@field(ptr, f.name)), .@"enum" => { @field(ptr, f.name) = @enumFromInt(@byteSwap(@intFromEnum(@field(ptr, f.name)))); }, .bool => {}, .float => |float_info| { @field(ptr, f.name) = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(@field(ptr, f.name))))); }, else => { @field(ptr, f.name) = @byteSwap(@field(ptr, f.name)); }, } } }, .array => { for (ptr) |*item| { switch (@typeInfo(@TypeOf(item.*))) { .@"struct", .array => byteSwapAllFields(@TypeOf(item.*), item), .@"enum" => { item.* = @enumFromInt(@byteSwap(@intFromEnum(item.*))); }, .bool => {}, .float => |float_info| { item.* = @bitCast(@byteSwap(@as(std.meta.Int(.unsigned, float_info.bits), @bitCast(item.*)))); }, else => { item.* = @byteSwap(item.*); }, } } }, else => @compileError("byteSwapAllFields expects a struct or array as the first argument"), } }