I'm trying to make some data structures that I potentially want to use in the future, but so far I'm having an issue with a very simple pooling mechanism.
const Libtests = @import("Libtests");
const pool = @import("pool.zig");
const Pool = pool.Pool;
pub var allocator: std.mem.Allocator = undefined;
const Data = struct {
text: []u8,
value: u32,
};
pub fn main() !void {
var alp = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
allocator = alp.allocator();
var testPool = Pool(Data, allocator, 10);
var data = testPool.Get();
data.text = allocator.alloc(u8, 5);
data.text[0] = 'T';
data.text[1] = 'e';
data.text[2] = 's';
data.text[3] = 't';
data.text[4] = '_';
data.value = 123456;
testPool.Return(data);
data = testPool.Get();
std.debug.print("DATA: {s} {d}", .{ data.text, data.value });
}```
```const std = @import("std");
pub fn Pool(comptime T: type, allocator: std.mem.Allocator, startingDepth: usize) type {
return struct {
const Self = @This();
const alloc = allocator;
var pool: []*T = alloc.alloc(*T, startingDepth);
var poolIndex: usize = 0;
pub fn Return(item: *T) void {
if (pool.len <= poolIndex) {
const tmp: []*T = pool;
pool = alloc.alloc(*T, tmp.len * 2);
@memcpy(pool[0..tmp.len], tmp);
alloc.free(tmp);
}
pool[poolIndex] = item;
poolIndex += 1;
}
pub fn Get() *T {
if (poolIndex > 0) {
poolIndex -= 1;
return pool[poolIndex];
} else {
return &T{};
}
}
};
}```
This results in:
```src/main.zig:16:31: error: unable to resolve comptime value
var testPool = Pool(Data, allocator, 10);
^~~~~~~~~```
But I don't understand why. All should be comptime available, no?