feature. See also
. The project being documented here (as the example) is the Zig library itself.
Builder.structTypeAssumeCapacity
fn structTypeAssumeCapacity(
self: *Builder,
comptime kind: Type.Structure.Kind,
fields: []const Type,
) Type
File
Code
fn structTypeAssumeCapacity(
self: *Builder,
comptime kind: Type.Structure.Kind,
fields: []const Type,
) Type {
const tag: Type.Tag = switch (kind) {
.normal => .structure,
.@"packed" => .packed_structure,
};
const Adapter = struct {
builder: *const Builder,
pub fn hash(_: @This(), key: []const Type) u32 {
return @truncate(std.hash.Wyhash.hash(
comptime std.hash.int(@backingInt(tag)),
std.mem.sliceAsBytes(key),
));
}
pub fn eql(ctx: @This(), lhs_key: []const Type, _: void, rhs_index: usize) bool {
const rhs_data = ctx.builder.type_items.items[rhs_index];
if (rhs_data.tag != tag) return false;
var rhs_extra = ctx.builder.typeExtraDataTrail(Type.Structure, rhs_data.data);
const rhs_fields = rhs_extra.trail.next(rhs_extra.data.fields_len, Type, ctx.builder);
return std.mem.eql(Type, lhs_key, rhs_fields);
}
};
const gop = self.type_map.getOrPutAssumeCapacityAdapted(fields, Adapter{ .builder = self });
if (!gop.found_existing) {
gop.key_ptr.* = {};
gop.value_ptr.* = {};
self.type_items.appendAssumeCapacity(.{
.tag = tag,
.data = self.addTypeExtraAssumeCapacity(Type.Structure{
.fields_len = @intCast(fields.len),
}),
});
self.type_extra.appendSliceAssumeCapacity(@ptrCast(fields));
}
return @fromBackingInt(@intCast(gop.index));
}