Hello amazing people!
You friendly zig-n00b is stuck while understanding this error:
/zig/zig-linux-x86_64-0.11.0-dev.2297+28d6dd75a/lib/std/mem/Allocator.zig:296:45: error: access of union field 'Pointer' while field 'Struct' is active
const Slice = @typeInfo(@TypeOf(memory)).Pointer;
The code I am testing is this:
pub const TokenKvp = struct {
allocator: *std.mem.Allocator,
key: []u8,
value: []u8,
pub fn init(allocator: *std.mem.Allocator, key: []const u8, value: []const u8) !TokenKvp {
var self = TokenKvp{
.allocator = allocator,
.key = try allocator.dupe(u8, key),
.value = try allocator.dupe(u8, value),
};
return self;
}
pub fn deinit(self: *TokenKvp) void {
self.allocator.free(self.key);
self.allocator.free(self.value);
}
};
pub const Entity = struct {
allocator: *std.mem.Allocator,
kvps: std.ArrayList(TokenKvp),
line: usize,
pub fn init(allocator: *std.mem.Allocator, line: usize) !Entity {
var kvpList = std.ArrayList(TokenKvp).init(allocator.*);
var self = Entity{
.allocator = allocator,
.kvps = kvpList,
.line = line,
};
return self;
}
pub fn addEntity(self: *Entity, key: []const u8, value: []const u8) !void {
var kvp = try TokenKvp.init(self.allocator, key, value);
try self.kvps.append(kvp);
}
pub fn deinit(self: *Entity) void {
for (self.kvps.items) |*kvp| {
kvp.deinit();
}
self.allocator.free(self.kvps);
}
};
test "kvp testing" {
var testingAllocator = std.testing.allocator;
var value: []const u8 = "value";
var key: []const u8 = "key";
var entity = try Entity.init(&testingAllocator, 0);
try entity.addEntity(key, value);
defer entity.deinit();
}
