Let Iter hold onto its own allocator

main
copygirl 10 months ago
parent c9bf64a34c
commit 12864ba14a
  1. 16
      src/iter.zig

@ -1,5 +1,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const c = @import("./c.zig");
const flecszigble = @import("./main.zig");
pub fn Iter(comptime ctx: anytype) type {
return struct {
@ -12,21 +14,21 @@ pub fn Iter(comptime ctx: anytype) type {
world: *World,
raw: *c.ecs_iter_t,
owned: bool,
allocator: ?Allocator,
pub fn fromRawPtr(world: *World, ptr: *c.ecs_iter_t) Self {
return .{ .world = world, .raw = ptr, .owned = false };
return .{ .world = world, .raw = ptr, .allocator = null };
}
pub fn fromRawValue(world: *World, value: c.ecs_iter_t) !Self {
const raw = try flecszigble.allocator.create(c.ecs_iter_t);
pub fn fromRawValue(world: *World, value: c.ecs_iter_t, allocator: Allocator) !Self {
const raw = try allocator.create(c.ecs_iter_t);
raw.* = value;
return .{ .world = world, .raw = raw, .owned = true };
return .{ .world = world, .raw = raw, .allocator = allocator };
}
pub fn deinit(self: Self) void {
if (self.isValid()) c.ecs_iter_fini(self.raw);
if (self.owned) flecszigble.allocator.destroy(self.raw);
if (self.allocator) |alloc| alloc.destroy(self.raw);
}
pub fn isValid(self: Self) bool {

Loading…
Cancel
Save