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.

Alignment

Builder.Alignment
pub const Alignment = enum(u6)

File

lib/std/zig/llvm/Builder.zig:2023

Code

pub const Alignment = enum(u6) {
    default = maxInt(u6),
    _,

    pub const Lazy = enum(u32) {
        /// Values which fit in a `u6` are already-resolved `Alignment` values. Other values are
        /// indices into `Builder.alignment_forward_references`, offset by `maxInt(u6)`.
        _,

        pub fn wrap(a: Alignment) Lazy {
            return @fromBackingInt(@intCast(@backingInt(a)));
        }
        pub fn resolve(l: Lazy, b: *const Builder) Alignment {
            return switch (@backingInt(l)) {
                0...maxInt(u6) => |raw| @fromBackingInt(@intCast(raw)),
                else => |offset_index| b.alignment_forward_references.items[offset_index - maxInt(u6)],
            };
        }

        fn fromFwdRefIndex(index: usize) Lazy {
            return @fromBackingInt(@intCast(index + maxInt(u6)));
        }
        fn toFwdRefIndex(l: Lazy) usize {
            return @backingInt(l) - maxInt(u6);
        }
    };

    pub fn fromByteUnits(bytes: u64) Alignment {
        if (bytes == 0) return .default;
        assert(std.math.isPowerOfTwo(bytes));
        assert(bytes <= 1 << 32);
        return @fromBackingInt(@intCast(@ctz(bytes)));
    }

    pub fn toByteUnits(self: Alignment) ?u64 {
        return switch (self) {
            .default => null,
            else => @as(u64, 1) << @backingInt(self),
        };
    }

    /// Asserts that neither `a` nor `b` is `.default`.
    pub fn max(a: Alignment, b: Alignment) Alignment {
        assert(a != .default);
        assert(b != .default);
        return @fromBackingInt(@intCast(@max(@backingInt(a), @backingInt(b))));
    }

    pub fn toLlvm(self: Alignment) u6 {
        return switch (self) {
            .default => 0,
            else => @backingInt(self) + 1,
        };
    }

    pub const Prefixed = struct {
        alignment: Alignment,
        prefix: []const u8,

        pub fn format(p: Prefixed, w: *Writer) Writer.Error!void {
            const byte_units = p.alignment.toByteUnits() orelse return;
            return w.print("{s}align {d}", .{ p.prefix, byte_units });
        }
    };

    pub fn fmt(alignment: Alignment, prefix: []const u8) Prefixed {
        return .{ .alignment = alignment, .prefix = prefix };
    }
}