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
Resizes to a new bit_length. If the new length is larger than the old length, fills any added bits with fill. If new_len is not zero, deinit must eventually be called.
pubfnresize(self: *@This(), allocator: Allocator, new_len: usize, fill: bool) !void {
constold_len = self.bit_length;
constold_masks = numMasks(old_len);
constnew_masks = numMasks(new_len);
constold_allocation = (self.masks - 1)[0..(self.masks - 1)[0]];
if (new_masks == 0) {
assert(new_len == 0);
allocator.free(old_allocation);
self.masks = empty_masks_ptr;
self.bit_length = 0;
return;
}
if (old_allocation.len != new_masks + 1) realloc: {
// If realloc fails, it may mean one of two things.// If we are growing, it means we are out of memory.// If we are shrinking, it means the allocator doesn't// want to move the allocation. This means we need to// hold on to the extra 8 bytes required to be able to free// this allocation properly.constnew_allocation = allocator.realloc(old_allocation, new_masks + 1) catch |err| {
if (new_masks + 1 > old_allocation.len) returnerr;
break :realloc;
};
new_allocation[0] = new_allocation.len;
self.masks = new_allocation.ptr + 1;
}
// If we increased in size, we need to set any new bits// to the fill value.if (new_len > old_len) {
// set the padding bits in the old last item to 1if (fillandold_masks > 0) {
constold_padding_bits = old_masks * @bitSizeOf(MaskInt) - old_len;
constold_mask = (~@as(MaskInt, 0)) >> @as(ShiftInt, @intCast(old_padding_bits));
self.masks[old_masks - 1] |= ~old_mask;
}
// fill in any new masksif (new_masks > old_masks) {
constfill_value = std.math.boolMask(MaskInt, fill);
@memset(self.masks[old_masks..new_masks], fill_value);
}
}
// Zero out the padding bitsif (new_len > 0) {
constpadding_bits = new_masks * @bitSizeOf(MaskInt) - new_len;
constlast_item_mask = (~@as(MaskInt, 0)) >> @as(ShiftInt, @intCast(padding_bits));
self.masks[new_masks - 1] &= last_item_mask;
}
// And finally, save the new length.self.bit_length = new_len;
}
Resizes to a new bit_length. If the new length is larger than the old length, fills any added bits with
fill. If new_len is not zero, deinit must eventually be called.