Removed lots of usless inline and comptime in Scales and Dimensions

This commit is contained in:
adrien
2026-04-27 19:09:55 +02:00
parent 698e968ef8
commit 168312b78e
2 changed files with 46 additions and 46 deletions
+24 -24
View File
@@ -55,33 +55,33 @@ pub const UnitScale = enum(isize) {
// Undefined
_,
pub inline fn str(self: @This()) []const u8 {
pub fn str(self: @This()) []const u8 {
var buf: [16]u8 = undefined;
return switch (self) {
inline .none => "",
inline .P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year, .inch, .ft, .yd, .mi, .oz, .lb, .st => @tagName(self),
.none => "",
.P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year, .inch, .ft, .yd, .mi, .oz, .lb, .st => @tagName(self),
else => std.fmt.bufPrint(&buf, "[{d}]", .{@intFromEnum(self)}) catch "[]", // This cannot be inline because of non exhaustive enum, but that's ok, it is just str, not calculation
};
}
pub inline fn getFactor(self: @This()) comptime_float {
comptime return switch (self) {
pub fn getFactor(self: @This()) comptime_float {
return switch (self) {
// Standard SI Exponents
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
.P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
// Time Factors
inline .min, .hour, .year => @floatFromInt(@intFromEnum(self)),
.min, .hour, .year => @floatFromInt(@intFromEnum(self)),
// Imperial Length (metres)
inline .inch => 0.0254,
inline .ft => 0.3048,
inline .yd => 0.9144,
inline .mi => 1609.344,
.inch => 0.0254,
.ft => 0.3048,
.yd => 0.9144,
.mi => 1609.344,
// Imperial Mass (grams — base unit for M is gram, i.e. .none = 1 g)
inline .oz => 28.3495231,
inline .lb => 453.59237,
inline .st => 6350.29318,
.oz => 28.3495231,
.lb => 453.59237,
.st => 6350.29318,
else => @floatFromInt(@intFromEnum(self)),
};
@@ -97,7 +97,7 @@ data: std.EnumArray(Dimension, UnitScale),
/// Unspecified dimensions default to `.none` (factor 1).
pub fn init(comptime init_val: ArgOpts) Self {
comptime var s = Self{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) };
inline for (std.meta.fields(@TypeOf(init_val))) |f| {
for (std.meta.fields(@TypeOf(init_val))) |f| {
if (comptime @typeInfo(@TypeOf(@field(init_val, f.name))) == .comptime_int)
s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name)))
else
@@ -106,31 +106,31 @@ pub fn init(comptime init_val: ArgOpts) Self {
return comptime s;
}
pub fn initFill(comptime val: UnitScale) Self {
comptime return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
pub fn initFill(val: UnitScale) Self {
return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
}
pub fn get(comptime self: Self, comptime key: Dimension) UnitScale {
return comptime self.data.get(key);
pub fn get(self: Self, key: Dimension) UnitScale {
return self.data.get(key);
}
pub fn set(comptime self: *Self, comptime key: Dimension, comptime val: UnitScale) void {
pub fn set(self: *Self, key: Dimension, val: UnitScale) void {
self.data.set(key, val);
}
pub fn argsOpt(self: Self) ArgOpts {
var args: ArgOpts = undefined;
inline for (std.enums.values(Dimension)) |d|
for (std.enums.values(Dimension)) |d|
@field(args, @tagName(d)) = self.get(d);
return args;
}
/// Compute the combined scale factor for a given dimension signature.
/// Each dimension's prefix is raised to its exponent and multiplied together.
pub inline fn getFactor(comptime s: Self, comptime d: Dimensions) comptime_float {
pub fn getFactor(s: Self, d: Dimensions) comptime_float {
var factor: f64 = 1.0;
for (std.enums.values(Dimension)) |dim| {
const power = comptime d.get(dim);
const power = d.get(dim);
if (power == 0) continue;
const base = s.get(dim).getFactor();
@@ -144,5 +144,5 @@ pub inline fn getFactor(comptime s: Self, comptime d: Dimensions) comptime_float
factor /= base;
}
}
comptime return factor;
return factor;
}