diff --git a/zig-ecs/src/ecs/utils.zig b/zig-ecs/src/ecs/utils.zig index abda0d3..f7d72e7 100644 --- a/zig-ecs/src/ecs/utils.zig +++ b/zig-ecs/src/ecs/utils.zig @@ -1,5 +1,28 @@ const std = @import("std"); +pub const ErasedPtr = struct { + ptr: usize, + + pub fn init(ptr: var) ErasedPtr { + if (@sizeOf(@TypeOf(ptr)) == 0) { + return .{ .ptr = undefined }; + } + return .{ .ptr = @ptrToInt(ptr) }; + } + + pub fn as(self: ErasedPtr, comptime T: type) *T { + if (@sizeOf(T) == 0) + return @as(T, undefined); + return self.asPtr(*T); + } + + pub fn asPtr(self: ErasedPtr, comptime PtrT: type) PtrT { + if (@sizeOf(PtrT) == 0) + return @as(PtrT, undefined); + return @intToPtr(PtrT, self.ptr); + } +}; + /// sorts items using lessThan and keeps sub_items with the same sort pub fn sortSub(comptime T1: type, comptime T2: type, items: []T1, sub_items: []T2, lessThan: fn (lhs: T1, rhs: T1) bool) void { var i: usize = 1; diff --git a/zig-ecs/src/resources/cache.zig b/zig-ecs/src/resources/cache.zig index d711844..b5be765 100644 --- a/zig-ecs/src/resources/cache.zig +++ b/zig-ecs/src/resources/cache.zig @@ -1,27 +1,5 @@ const std = @import("std"); - -pub const ErasedPtr = struct { - ptr: usize, - - pub fn init(ptr: var) ErasedPtr { - if (@sizeOf(@TypeOf(ptr)) == 0) { - return .{ .ptr = undefined }; - } - return .{ .ptr = @ptrToInt(ptr) }; - } - - pub fn as(self: ErasedPtr, comptime T: type) *T { - if (@sizeOf(T) == 0) - return @as(T, undefined); - return self.asPtr(*T); - } - - pub fn asPtr(self: ErasedPtr, comptime PtrT: type) PtrT { - if (@sizeOf(PtrT) == 0) - return @as(PtrT, undefined); - return @intToPtr(PtrT, self.ptr); - } -}; +const ErasedPtr = @import("../ecs/utils.zig").ErasedPtr; /// Simple cache for resources of a given type. TLoader should be a struct that implements a single /// method: load(args: var) *T. If any resource has a deinit method it will be called when clear