diff --git a/zig-ecs/src/ecs/groups.zig b/zig-ecs/src/ecs/groups.zig index ecb7a42..139e2c6 100644 --- a/zig-ecs/src/ecs/groups.zig +++ b/zig-ecs/src/ecs/groups.zig @@ -138,7 +138,7 @@ pub const OwningGroup = struct { /// grabs an untyped (u1) reference to the first Storage(T) in the owned array fn firstOwnedStorage(self: OwningGroup) *Storage(u1) { const ptr = self.registry.components.get(self.group_data.owned[0]).?; - return @intToPtr(*Storage(u1), ptr); + return @ptrFromInt(*Storage(u1), ptr); } /// total number of entities in the group @@ -265,7 +265,7 @@ pub const OwningGroup = struct { // skip the first one since its what we are using to sort with for (self.group_data.owned[1..]) |type_id| { var other_ptr = self.registry.components.get(type_id).?; - var storage = @intToPtr(*Storage(u1), other_ptr); + var storage = @ptrFromInt(*Storage(u1), other_ptr); storage.swap(storage.data()[pos], entity); } } diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index 3c0cf02..39c7df7 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -83,19 +83,19 @@ pub const Registry = struct { const isValid: bool = blk: { for (self.owned) |tid| { const ptr = self.registry.components.get(tid).?; - if (!@intToPtr(*Storage(u1), ptr).contains(entity)) + if (!@ptrFromInt(*Storage(u1), ptr).contains(entity)) break :blk false; } for (self.include) |tid| { const ptr = self.registry.components.get(tid).?; - if (!@intToPtr(*Storage(u1), ptr).contains(entity)) + if (!@ptrFromInt(*Storage(u1), ptr).contains(entity)) break :blk false; } for (self.exclude) |tid| { const ptr = self.registry.components.get(tid).?; - if (@intToPtr(*Storage(u1), ptr).contains(entity)) + if (@ptrFromInt(*Storage(u1), ptr).contains(entity)) break :blk false; } break :blk true; @@ -108,11 +108,11 @@ pub const Registry = struct { } else { if (isValid) { const ptr = self.registry.components.get(self.owned[0]).?; - if (!(@intToPtr(*Storage(u1), ptr).set.index(entity) < self.current)) { + if (!(@ptrFromInt(*Storage(u1), ptr).set.index(entity) < self.current)) { for (self.owned) |tid| { // store.swap hides a safe version that types it correctly const store_ptr = self.registry.components.get(tid).?; - var store = @intToPtr(*Storage(u1), store_ptr); + var store = @ptrFromInt(*Storage(u1), store_ptr); store.swap(store.data()[self.current], entity); } self.current += 1; @@ -129,12 +129,12 @@ pub const Registry = struct { } } else { const ptr = self.registry.components.get(self.owned[0]).?; - var store = @intToPtr(*Storage(u1), ptr); + var store = @ptrFromInt(*Storage(u1), ptr); if (store.contains(entity) and store.set.index(entity) < self.current) { self.current -= 1; for (self.owned) |tid| { const store_ptr = self.registry.components.get(tid).?; - store = @intToPtr(*Storage(u1), store_ptr); + store = @ptrFromInt(*Storage(u1), store_ptr); store.swap(store.data()[self.current], entity); } } @@ -199,7 +199,7 @@ pub const Registry = struct { var iter = self.components.valueIterator(); while (iter.next()) |ptr| { // HACK: we dont know the Type here but we need to call deinit - var storage = @intToPtr(*Storage(u1), ptr.*); + var storage = @ptrFromInt(*Storage(u1), ptr.*); storage.deinit(); } @@ -217,11 +217,11 @@ pub const Registry = struct { pub fn assure(self: *Registry, comptime T: type) *Storage(T) { var type_id = utils.typeId(T); if (self.components.getEntry(type_id)) |kv| { - return @intToPtr(*Storage(T), kv.value_ptr.*); + return @ptrFromInt(*Storage(T), kv.value_ptr.*); } var comp_set = Storage(T).initPtr(self.allocator); - var comp_set_ptr = @ptrToInt(comp_set); + var comp_set_ptr = @intFromPtr(comp_set); _ = self.components.put(type_id, comp_set_ptr) catch unreachable; return comp_set; } @@ -347,7 +347,7 @@ pub const Registry = struct { var iter = self.components.valueIterator(); while (iter.next()) |value| { // HACK: we dont know the Type here but we need to be able to call methods on the Storage(T) - var store = @intToPtr(*Storage(u1), value.*); + var store = @ptrFromInt(*Storage(u1), value.*); store.removeIfContains(entity); } } @@ -398,7 +398,7 @@ pub const Registry = struct { std.debug.assert(@typeInfo(@TypeOf(context)) == .Pointer); var type_id = utils.typeId(@typeInfo(@TypeOf(context)).Pointer.child); - _ = self.contexts.put(type_id, @ptrToInt(context)) catch unreachable; + _ = self.contexts.put(type_id, @intFromPtr(context)) catch unreachable; } /// Unsets a context variable if it exists @@ -412,7 +412,7 @@ pub const Registry = struct { std.debug.assert(@typeInfo(T) != .Pointer); return if (self.contexts.get(utils.typeId(T))) |ptr| - return if (ptr > 0) @intToPtr(*T, ptr) else null + return if (ptr > 0) @ptrFromInt(*T, ptr) else null else null; } @@ -553,7 +553,7 @@ pub const Registry = struct { // update super on all owned Storages to be the max of size and their current super value inline for (owned) |t| { var storage = self.assure(t); - storage.super = std.math.max(storage.super, new_group_data.size); + storage.super = @max(storage.super, new_group_data.size); } } @@ -632,7 +632,7 @@ pub const Registry = struct { name.* = @typeName(types[i]); } - std.sort.sort([]const u8, &names, {}, impl.asc); + std.sort.pdq([]const u8, &names, {}, impl.asc); comptime var res: []const u8 = ""; inline for (names) |name| res = res ++ name; diff --git a/zig-ecs/src/ecs/utils.zig b/zig-ecs/src/ecs/utils.zig index 1741b4d..ea0e30b 100644 --- a/zig-ecs/src/ecs/utils.zig +++ b/zig-ecs/src/ecs/utils.zig @@ -7,7 +7,7 @@ pub const ErasedPtr = struct { if (@sizeOf(@TypeOf(ptr)) == 0) { return .{ .ptr = undefined }; } - return .{ .ptr = @ptrToInt(ptr) }; + return .{ .ptr = @intFromPtr(ptr) }; } pub fn as(self: ErasedPtr, comptime T: type) *T { @@ -19,7 +19,7 @@ pub const ErasedPtr = struct { pub fn asPtr(self: ErasedPtr, comptime PtrT: type) PtrT { if (@sizeOf(PtrT) == 0) return @as(PtrT, undefined); - return @intToPtr(PtrT, self.ptr); + return @ptrFromInt(PtrT, self.ptr); } }; diff --git a/zig-ecs/src/ecs/views.zig b/zig-ecs/src/ecs/views.zig index ec20505..e662def 100644 --- a/zig-ecs/src/ecs/views.zig +++ b/zig-ecs/src/ecs/views.zig @@ -68,7 +68,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { pub fn init(view: *Self) Iterator { const ptr = view.registry.components.get(view.type_ids[0]).?; - const internal_it = @intToPtr(*Storage(u8), ptr).set.reverseIterator(); + const internal_it = @ptrFromInt(*Storage(u8), ptr).set.reverseIterator(); return .{ .view = view, .internal_it = internal_it }; } @@ -77,7 +77,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { // entity must be in all other Storages for (it.view.type_ids) |tid| { const ptr = it.view.registry.components.get(tid).?; - if (!@intToPtr(*Storage(u1), ptr).contains(entity)) { + if (!@ptrFromInt(*Storage(u1), ptr).contains(entity)) { break :blk; } } @@ -85,7 +85,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { // entity must not be in all other excluded Storages for (it.view.exclude_type_ids) |tid| { const ptr = it.view.registry.components.get(tid).?; - if (@intToPtr(*Storage(u1), ptr).contains(entity)) { + if (@ptrFromInt(*Storage(u1), ptr).contains(entity)) { break :blk; } } @@ -104,7 +104,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { fn getInternalIteratorInstance(it: *Iterator) ReverseSliceIterator(Entity) { const ptr = it.view.registry.components.get(it.view.type_ids[0]).?; - return @intToPtr(*Storage(u8), ptr).set.reverseIterator(); + return @ptrFromInt(*Storage(u8), ptr).set.reverseIterator(); } }; @@ -129,7 +129,7 @@ pub fn MultiView(comptime n_includes: usize, comptime n_excludes: usize) type { var sub_items: [n_includes]usize = undefined; for (self.type_ids, 0..) |tid, i| { const ptr = self.registry.components.get(tid).?; - const store = @intToPtr(*Storage(u8), ptr); + const store = @ptrFromInt(*Storage(u8), ptr); sub_items[i] = store.len(); } diff --git a/zig-ecs/src/resources/assets.zig b/zig-ecs/src/resources/assets.zig index 6481783..ac1e08e 100644 --- a/zig-ecs/src/resources/assets.zig +++ b/zig-ecs/src/resources/assets.zig @@ -17,7 +17,7 @@ pub const Assets = struct { var iter = self.caches.iterator(); while (iter.next()) |ptr| { // HACK: we dont know the Type here but we need to call deinit - @intToPtr(*Cache(u1), ptr.value_ptr.*).deinit(); + @ptrFromInt(*Cache(u1), ptr.value_ptr.*).deinit(); } self.caches.deinit(); @@ -25,11 +25,11 @@ pub const Assets = struct { pub fn get(self: *Assets, comptime AssetT: type) *Cache(AssetT) { if (self.caches.get(utils.typeId(AssetT))) |tid| { - return @intToPtr(*Cache(AssetT), tid); + return @ptrFromInt(*Cache(AssetT), tid); } var cache = Cache(AssetT).initPtr(self.allocator); - _ = self.caches.put(utils.typeId(AssetT), @ptrToInt(cache)) catch unreachable; + _ = self.caches.put(utils.typeId(AssetT), @intFromPtr(cache)) catch unreachable; return cache; } diff --git a/zig-ecs/src/resources/cache.zig b/zig-ecs/src/resources/cache.zig index e4d9a3f..9ada0ad 100644 --- a/zig-ecs/src/resources/cache.zig +++ b/zig-ecs/src/resources/cache.zig @@ -59,7 +59,7 @@ pub fn Cache(comptime T: type) type { pub fn remove(self: *@This(), id: u32) void { if (self.resources.fetchRemove(id)) |kv| { if (@hasDecl(T, "deinit")) { - @call(.always_inline, @field(kv.value, "deinit"), .{}); + @call(.always_inline, T.deinit, .{kv.value}); } } } @@ -69,7 +69,7 @@ pub fn Cache(comptime T: type) type { if (@hasDecl(T, "deinit")) { var iter = self.resources.iterator(); while (iter.next()) |kv| { - @call(.always_inline, @field(kv.value_ptr.*, "deinit"), .{}); + @call(.always_inline, T.deinit, .{kv.value_ptr.*}); } } self.resources.clearAndFree(); diff --git a/zig-ecs/src/signals/delegate.zig b/zig-ecs/src/signals/delegate.zig index 3a79f9c..f69d879 100644 --- a/zig-ecs/src/signals/delegate.zig +++ b/zig-ecs/src/signals/delegate.zig @@ -14,15 +14,16 @@ pub fn Delegate(comptime Event: type) type { /// sets a bound function as the Delegate callback pub fn initBound(ctx: anytype, comptime fn_name: []const u8) Self { std.debug.assert(@typeInfo(@TypeOf(ctx)) == .Pointer); - std.debug.assert(@ptrToInt(ctx) != 0); + std.debug.assert(@intFromPtr(ctx) != 0); const T = @TypeOf(ctx); + const BaseT = @typeInfo(T).Pointer.child; return Self{ - .ctx_ptr_address = @ptrToInt(ctx), + .ctx_ptr_address = @intFromPtr(ctx), .callback = .{ .bound = struct { fn cb(self: usize, param: Event) void { - @call(.always_inline, @field(@intToPtr(T, self), fn_name), .{param}); + @call(.always_inline, @field(BaseT, fn_name), .{ @ptrFromInt(T, self), param }); } }.cb, }, @@ -51,11 +52,11 @@ pub fn Delegate(comptime Event: type) type { } pub fn containsBound(self: Self, ctx: anytype) bool { - std.debug.assert(@ptrToInt(ctx) != 0); + std.debug.assert(@intFromPtr(ctx) != 0); std.debug.assert(@typeInfo(@TypeOf(ctx)) == .Pointer); return switch (self.callback) { - .bound => @ptrToInt(ctx) == self.ctx_ptr_address, + .bound => @intFromPtr(ctx) == self.ctx_ptr_address, else => false, }; } diff --git a/zig-ecs/src/signals/dispatcher.zig b/zig-ecs/src/signals/dispatcher.zig index 265fff9..5a839c4 100644 --- a/zig-ecs/src/signals/dispatcher.zig +++ b/zig-ecs/src/signals/dispatcher.zig @@ -18,7 +18,7 @@ pub const Dispatcher = struct { var iter = self.signals.iterator(); while (iter.next()) |ptr| { // HACK: we dont know the Type here but we need to call deinit - var signal = @intToPtr(*Signal(void), ptr.value_ptr.*); + var signal = @ptrFromInt(*Signal(void), ptr.value_ptr.*); signal.deinit(); } @@ -28,11 +28,11 @@ pub const Dispatcher = struct { fn assure(self: *Dispatcher, comptime T: type) *Signal(T) { var type_id = utils.typeId(T); if (self.signals.get(type_id)) |value| { - return @intToPtr(*Signal(T), value); + return @ptrFromInt(*Signal(T), value); } var signal = Signal(T).create(self.allocator); - var signal_ptr = @ptrToInt(signal); + var signal_ptr = @intFromPtr(signal); _ = self.signals.put(type_id, signal_ptr) catch unreachable; return signal; } diff --git a/zig-ecs/tests/groups_test.zig b/zig-ecs/tests/groups_test.zig index 2706ba7..d5aa392 100644 --- a/zig-ecs/tests/groups_test.zig +++ b/zig-ecs/tests/groups_test.zig @@ -30,8 +30,8 @@ test "sort BasicGroup by Entity" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); } const SortContext = struct { @@ -64,8 +64,8 @@ test "sort BasicGroup by Component" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); } const SortContext = struct { @@ -92,8 +92,8 @@ test "sort OwningGroup by Entity" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); } const SortContext = struct { @@ -125,8 +125,8 @@ test "sort OwningGroup by Component" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); } const SortContext = struct { @@ -153,11 +153,11 @@ test "sort OwningGroup by Component ensure unsorted non-matches" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); var e2 = reg.create(); - reg.add(e2, Sprite{ .x = @intToFloat(f32, i + 1 * 50) }); + reg.add(e2, Sprite{ .x = @floatFromInt(f32, i + 1 * 50) }); } try std.testing.expectEqual(group.len(), 5); @@ -223,8 +223,8 @@ test "nested OwningGroups entity order" { var i: usize = 0; while (i < 5) : (i += 1) { var e = reg.create(); - reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); - reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + reg.add(e, Sprite{ .x = @floatFromInt(f32, i) }); + reg.add(e, Renderable{ .x = @floatFromInt(f32, i) }); } try std.testing.expectEqual(group1.len(), 5); diff --git a/zig-ecs/tests/registry_test.zig b/zig-ecs/tests/registry_test.zig index 10d5cf1..8f1553b 100644 --- a/zig-ecs/tests/registry_test.zig +++ b/zig-ecs/tests/registry_test.zig @@ -58,7 +58,7 @@ test "context not pointer" { // reg.setContext(pos); } -test "context get/set/unset" { +test "context get/set/unset typed" { const SomeType = struct { dummy: u1 }; var reg = Registry.init(std.testing.allocator); @@ -97,7 +97,7 @@ test "destroy" { var i = @as(u8, 0); while (i < 255) : (i += 1) { const e = reg.create(); - reg.add(e, Position{ .x = @intToFloat(f32, i), .y = @intToFloat(f32, i) }); + reg.add(e, Position{ .x = @floatFromInt(f32, i), .y = @floatFromInt(f32, i) }); } reg.destroy(3); @@ -106,7 +106,7 @@ test "destroy" { i = 0; while (i < 6) : (i += 1) { if (i != 3 and i != 4) - try std.testing.expectEqual(Position{ .x = @intToFloat(f32, i), .y = @intToFloat(f32, i) }, reg.getConst(Position, i)); + try std.testing.expectEqual(Position{ .x = @floatFromInt(f32, i), .y = @floatFromInt(f32, i) }, reg.getConst(Position, i)); } }