Add RocList.first_elem_ptr to list.zig

This commit is contained in:
Richard Feldman 2022-07-01 15:28:54 -04:00
parent f82d515894
commit ff596f5e75
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798

View File

@ -3,6 +3,7 @@ const utils = @import("utils.zig");
const RocResult = utils.RocResult;
const UpdateMode = utils.UpdateMode;
const mem = std.mem;
const math = std.math;
const EqFn = fn (?[*]u8, ?[*]u8) callconv(.C) bool;
const CompareFn = fn (?[*]u8, ?[*]u8, ?[*]u8) callconv(.C) u8;
@ -30,6 +31,14 @@ pub const RocList = extern struct {
return RocList{ .bytes = null, .length = 0, .capacity = 0 };
}
pub fn first_elem_ptr(self: RocList, comptime elem_type: type) [*]elem_type {
const refcount_byte_count = math.max(@alignOf(usize), @alignOf(elem_type));
const addr = @ptrToInt(self.bytes) + refcount_byte_count;
// The first element is stored right after the refcount.
return @intToPtr([*]elem_type, addr);
}
pub fn isUnique(self: RocList) bool {
// the empty list is unique (in the sense that copying it will not leak memory)
if (self.isEmpty()) {