Zig 0.17.0-dev (Split by item)

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 library itself.

byteSwapAllElements

Reverses the byte order of all elements in a slice. Handles structs, unions, arrays, enums, floats, and integers recursively. Useful for converting between little-endian and big-endian representations.

mem.byteSwapAllElements
pub fn byteSwapAllElements(comptime Elem: type, slice: []Elem) void

File

lib/std/mem.zig:2362

Code

pub fn byteSwapAllElements(comptime Elem: type, slice: []Elem) void {
    for (slice) |*elem| {
        switch (@typeInfo(@TypeOf(elem.*))) {
            .@"struct", .@"union", .array => byteSwapAllFields(@TypeOf(elem.*), elem),
            .@"enum" => {
                elem.* = @fromBackingInt(@intCast(@byteSwap(@backingInt(elem.*))));
            },
            .bool => {},
            .float => |float| {
                const int_repr: @Int(.unsigned, float.bits) = @bitCast(elem.*);
                elem.* = @bitCast(@byteSwap(int_repr));
            },
            else => {
                elem.* = @byteSwap(elem.*);
            },
        }
    }
}