gtk: correct comptime adwaita.versionAtLeast() comparison (#4084)

Companion to #3977
This commit is contained in:
Mitchell Hashimoto
2024-12-30 08:39:47 -08:00
committed by GitHub

View File

@ -25,7 +25,10 @@ pub inline fn enabled(config: *const Config) bool {
/// in the headers. If it is run in a runtime context, it will
/// check the actual version of the library we are linked against.
/// So generally you probably want to do both checks!
pub fn versionAtLeast(
///
/// This is inlined so that the comptime checks will disable the
/// runtime checks if the comptime checks fail.
pub inline fn versionAtLeast(
comptime major: u16,
comptime minor: u16,
comptime micro: u16,
@ -37,8 +40,9 @@ pub fn versionAtLeast(
// compiling against unknown symbols and makes runtime checks
// very slightly faster.
if (comptime c.ADW_MAJOR_VERSION < major or
c.ADW_MINOR_VERSION < minor or
c.ADW_MICRO_VERSION < micro) return false;
(c.ADW_MAJOR_VERSION == major and c.ADW_MINOR_VERSION < minor) or
(c.ADW_MAJOR_VERSION == major and c.ADW_MINOR_VERSION == minor and c.ADW_MICRO_VERSION < micro))
return false;
// If we're in comptime then we can't check the runtime version.
if (@inComptime()) return true;
@ -56,3 +60,16 @@ pub fn versionAtLeast(
return false;
}
test "versionAtLeast" {
const testing = std.testing;
try testing.expect(versionAtLeast(c.ADW_MAJOR_VERSION, c.ADW_MINOR_VERSION, c.ADW_MICRO_VERSION));
try testing.expect(!versionAtLeast(c.ADW_MAJOR_VERSION, c.ADW_MINOR_VERSION, c.ADW_MICRO_VERSION + 1));
try testing.expect(!versionAtLeast(c.ADW_MAJOR_VERSION, c.ADW_MINOR_VERSION + 1, c.ADW_MICRO_VERSION));
try testing.expect(!versionAtLeast(c.ADW_MAJOR_VERSION + 1, c.ADW_MINOR_VERSION, c.ADW_MICRO_VERSION));
try testing.expect(versionAtLeast(c.ADW_MAJOR_VERSION - 1, c.ADW_MINOR_VERSION, c.ADW_MICRO_VERSION));
try testing.expect(versionAtLeast(c.ADW_MAJOR_VERSION - 1, c.ADW_MINOR_VERSION + 1, c.ADW_MICRO_VERSION));
try testing.expect(versionAtLeast(c.ADW_MAJOR_VERSION - 1, c.ADW_MINOR_VERSION, c.ADW_MICRO_VERSION + 1));
try testing.expect(versionAtLeast(c.ADW_MAJOR_VERSION, c.ADW_MINOR_VERSION - 1, c.ADW_MICRO_VERSION + 1));
}