Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

byteSwapAllFieldsAligned

Public Function
std.mem.byteSwapAllFieldsAligned
pub fn byteSwapAligned(
    comptime S: type,
    comptime a: Alignment,
    ptr: *align(a.toByteUnits()) S,
) void

Reverses the byte order. Handles structs, unions, arrays, enums, floats, and integers recursively. The order of extern struct fields and array elements remains unchanged and will be byte swapped recursively. Useful for converting between little-endian and big-endian representations.

Params

  • S: type
  • a: Alignment
  • ptr: *align(a.toByteUnits()) S

File

mem.zig:2249
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
pub fn byteSwapAligned(
    comptime S: type,
    comptime a: Alignment,
    ptr: *align(a.toByteUnits()) S,
) void {
    switch (@typeInfo(S)) {
        .@"struct" => |@"struct"| {
            if (@"struct".backing_integer) |Int| {
                ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*))));
            } else {
                if (@"struct".layout != .@"extern") {
                    @compileError("byteSwapAligned expects a packed or extern struct");
                }
                inline for (@"struct".field_types, @"struct".field_names, @"struct".field_attrs) |f_type, f_name, f_attr| {
                    switch (@typeInfo(f_type)) {
                        .@"struct" => byteSwapAligned(f_type, .fromByteUnits(f_attr.@"align" orelse @alignOf(f_type)), &@field(ptr, f_name)),
                        .@"union", .array => byteSwapAligned(f_type, .fromByteUnits(f_attr.@"align" orelse @alignOf(f_type)), &@field(ptr, f_name)),
                        .@"enum" => {
                            @field(ptr, f_name) = @fromBackingInt(@byteSwap(@backingInt(@field(ptr, f_name))));
                        },
                        .bool => {},
                        .float => |float| {
                            @field(ptr, f_name) = @bitCast(@byteSwap(@as(@Int(.unsigned, float.bits), @bitCast(@field(ptr, f_name)))));
                        },
                        else => {
                            @field(ptr, f_name) = @byteSwap(@field(ptr, f_name));
                        },
                    }
                }
            }
        },
        .@"union" => |@"union"| if (@"union".backing_integer) |Int| {
            ptr.* = @bitCast(@byteSwap(@as(Int, @bitCast(ptr.*))));
        } else {
            if (@"union".layout != .@"extern") {
                @compileError("byteSwapAligned expects a packed or extern union");
            }

            const first_size = @bitSizeOf(@"union".field_types[0]);
            inline for (@"union".field_types) |field_type| {
                if (@bitSizeOf(field_type) != first_size) {
                    @compileError("Unable to byte-swap unions with varying field sizes");
                }
            }

            const FieldInt = @Int(.unsigned, first_size);
            const field_ptr = &@field(ptr, @"union".field_names[0]);
            field_ptr.* = @bitCast(@byteSwap(@as(FieldInt, @bitCast(field_ptr.*))));
        },
        .array => |array| {
            byteSwapAllElements(array.child, ptr);
        },
        .@"enum" => {
            ptr.* = @fromBackingInt(@byteSwap(@backingInt(ptr.*)));
        },
        .bool => {},
        .float => |float| {
            const int_repr: @Int(.unsigned, float.bits) = @bitCast(ptr.*);
            ptr.* = @bitCast(@byteSwap(int_repr));
        },
        else => {
            ptr.* = @byteSwap(ptr.*);
        },
    }
}