camelCase for function names

This commit is contained in:
duck
2025-05-04 17:49:28 +05:00
parent b133a747ae
commit c19120c3fc
8 changed files with 102 additions and 103 deletions

View File

@@ -56,32 +56,32 @@ pub const Controller = struct {
}
/// Adds resource to the global storage, discarding any previously existing data
pub inline fn add_resource(self: *Controller, resource: anytype) void {
utils.validate_resource(@TypeOf(resource));
pub inline fn addResource(self: *Controller, resource: anytype) void {
utils.validateResource(@TypeOf(resource));
self.add_anyopaque_resource(
self.addAnyopaqueResource(
@ptrCast(&resource),
utils.hash_type(@TypeOf(resource)),
utils.hashType(@TypeOf(resource)),
@sizeOf(@TypeOf(resource)),
@alignOf(@TypeOf(resource)),
) catch |err| self.fail(err);
}
pub fn queue_system(self: *Controller, comptime function: anytype) void {
utils.validate_system(function);
pub fn queueSystem(self: *Controller, comptime function: anytype) void {
utils.validateSystem(function);
self.queue_system_internal(function) catch |err| self.fail(err);
self.queueSystemInternal(function) catch |err| self.fail(err);
}
fn queue_system_internal(self: *Controller, comptime function: anytype) !void {
var system = try System.from_function(function, self.alloc);
fn queueSystemInternal(self: *Controller, comptime function: anytype) !void {
var system = try System.fromFunction(function, self.alloc);
errdefer system.deinit(self.alloc);
try self.command_buffer.append(self.alloc, .{ .queue_system = system });
}
/// `previous_output` is expected to be aligned accordingly
fn add_anyopaque_resource(
fn addAnyopaqueResource(
self: *Controller,
resource: *const anyopaque,
hash: utils.Hash,

View File

@@ -13,20 +13,20 @@ pub const Request = union(enum) {
};
const Self = @This();
pub fn from_function(comptime function: anytype, alloc: std.mem.Allocator) !Self {
utils.validate_system(function);
pub fn fromFunction(comptime function: anytype, alloc: std.mem.Allocator) !Self {
utils.validateSystem(function);
var requests: [@typeInfo(@TypeOf(function)).Fn.params.len]Request = undefined;
inline for (0.., @typeInfo(@TypeOf(function)).Fn.params) |i, param| {
switch (@typeInfo(param.type.?).Pointer.child) {
var requests: [@typeInfo(@TypeOf(function)).@"fn".params.len]Request = undefined;
inline for (0.., @typeInfo(@TypeOf(function)).@"fn".params) |i, param| {
switch (@typeInfo(param.type.?).pointer.child) {
Controller => requests[i] = .controller,
else => |resource_type| requests[i] = .{ .resource = utils.hash_type(resource_type) },
else => |resource_type| requests[i] = .{ .resource = utils.hashType(resource_type) },
}
}
return Self{
.requested_types = try alloc.dupe(Request, &requests),
.function_runner = utils.generate_runner(function),
.function_runner = utils.generateRunner(function),
};
}

View File

@@ -5,11 +5,11 @@ const System = @import("system.zig");
pub const Hash = u32;
const HashAlgorithm = std.crypto.hash.blake2.Blake2s(@bitSizeOf(Hash));
pub inline fn hash_type(comptime h_type: type) Hash {
return hash_string(@typeName(h_type));
pub inline fn hashType(comptime h_type: type) Hash {
return hashString(@typeName(h_type));
}
pub fn hash_string(comptime name: []const u8) Hash {
pub fn hashString(comptime name: []const u8) Hash {
@setEvalBranchQuota(100000);
var output: [@divExact(@bitSizeOf(Hash), 8)]u8 = undefined;
@@ -21,39 +21,39 @@ pub fn hash_string(comptime name: []const u8) Hash {
);
}
pub fn validate_resource(comptime resource_type: type) void {
pub fn validateResource(comptime resource_type: type) void {
switch (@typeInfo(resource_type)) {
.Struct, .Enum, .Union => return,
.@"struct", .@"enum", .@"union" => return,
else => @compileError("Invalid resource type \"" ++ @typeName(resource_type) ++ "\""),
}
}
pub fn validate_system(comptime system: anytype) void {
pub fn validateSystem(comptime system: anytype) void {
const info = @typeInfo(@TypeOf(system));
if (info != .Fn) @compileError("System can only be a function, got " ++ @typeName(system));
if (info.Fn.return_type != void) @compileError("Systems are not allowed to return any value (" ++ @typeName(info.Fn.return_type.?) ++ " returned)");
if (info.Fn.is_var_args) @compileError("System cannot be variadic");
if (info.Fn.is_generic) @compileError("System cannot be generic");
if (info != .@"fn") @compileError("System can only be a function, got " ++ @typeName(system));
if (info.@"fn".return_type != void) @compileError("Systems are not allowed to return any value (" ++ @typeName(info.Fn.return_type.?) ++ " returned)");
if (info.@"fn".is_var_args) @compileError("System cannot be variadic");
if (info.@"fn".is_generic) @compileError("System cannot be generic");
const controller_requests: usize = 0;
inline for (info.Fn.params) |param| {
if (@typeInfo(param.type.?) != .Pointer) @compileError("Systems can only have pointer parameters");
switch (@typeInfo(param.type.?).Pointer.child) {
inline for (info.@"fn".params) |param| {
if (@typeInfo(param.type.?) != .pointer) @compileError("Systems can only have pointer parameters");
switch (@typeInfo(param.type.?).pointer.child) {
Resource.Controller => {
// controller_requests += 1;
// _ = &controller_requests;
},
else => |t| validate_resource(t),
else => |t| validateResource(t),
}
}
if (controller_requests > 1) @compileError("A system cannot request controller more than once");
}
pub fn generate_runner(comptime system: anytype) fn ([]const *anyopaque) void {
pub fn generateRunner(comptime system: anytype) fn ([]const *anyopaque) void {
const RunnerImpl = struct {
fn runner(resources: []const *anyopaque) void {
var args: std.meta.ArgsTuple(@TypeOf(system)) = undefined;
inline for (0..@typeInfo(@TypeOf(system)).Fn.params.len) |index| {
inline for (0..@typeInfo(@TypeOf(system)).@"fn".params.len) |index| {
args[index] = @alignCast(@ptrCast(resources[index]));
}
@call(.always_inline, system, args);