From b35a7b4fad8cac05b53b6ff59ce28ab003ab9611 Mon Sep 17 00:00:00 2001 From: InKryption <59504965+InKryption@users.noreply.github.com> Date: Mon, 22 Nov 2021 20:21:46 +0100 Subject: [PATCH] glfw: Change error unions into normal returns, in accordance with the new guarantee to never encounter 'GLFW_NOT_INITIALIZED', and update tests --- glfw/src/Monitor.zig | 52 ++++++++++++++++++++------------------------ glfw/src/Window.zig | 34 +++++++++++++---------------- glfw/src/vulkan.zig | 5 ++--- 3 files changed, 40 insertions(+), 51 deletions(-) diff --git a/glfw/src/Monitor.zig b/glfw/src/Monitor.zig index 9ffe8bd1..88c15352 100644 --- a/glfw/src/Monitor.zig +++ b/glfw/src/Monitor.zig @@ -98,8 +98,7 @@ const PhysicalSize = struct { /// @thread_safety This function must only be called from the main thread. /// /// see also: monitor_properties -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn getPhysicalSize(self: Monitor) Error!PhysicalSize { +pub inline fn getPhysicalSize(self: Monitor) PhysicalSize { internal_debug.assertInitialized(); var width_mm: c_int = 0; var height_mm: c_int = 0; @@ -157,8 +156,7 @@ pub inline fn getContentScale(self: Monitor) Error!ContentScale { /// @thread_safety This function must only be called from the main thread. /// /// see also: monitor_properties -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn getName(self: Monitor) Error![*:0]const u8 { +pub inline fn getName(self: Monitor) [*:0]const u8 { internal_debug.assertInitialized(); const name = c.glfwGetMonitorName(self.handle); getError() catch unreachable; @@ -178,8 +176,7 @@ pub inline fn getName(self: Monitor) Error![*:0]const u8 { /// @thread_safety This function may be called from any thread. Access is not synchronized. /// /// see also: monitor_userptr, glfw.Monitor.getUserPointer -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn setUserPointer(self: Monitor, comptime T: type, ptr: *T) Error!void { +pub inline fn setUserPointer(self: Monitor, comptime T: type, ptr: *T) void { internal_debug.assertInitialized(); c.glfwSetMonitorUserPointer(self.handle, ptr); getError() catch unreachable; @@ -197,8 +194,7 @@ pub inline fn setUserPointer(self: Monitor, comptime T: type, ptr: *T) Error!voi /// @thread_safety This function may be called from any thread. Access is not synchronized. /// /// see also: monitor_userptr, glfw.Monitor.setUserPointer -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn getUserPointer(self: Monitor, comptime T: type) Error!?*T { +pub inline fn getUserPointer(self: Monitor, comptime T: type) ?*T { internal_debug.assertInitialized(); const ptr = c.glfwGetMonitorUserPointer(self.handle); getError() catch unreachable; @@ -358,7 +354,7 @@ pub inline fn setGammaRamp(self: Monitor, ramp: GammaRamp) Error!void { /// @thread_safety This function must only be called from the main thread. /// /// see also: monitor_monitors, monitor_event, glfw.monitor.getPrimary -pub inline fn getAll(allocator: *mem.Allocator) (mem.Allocator.Error)![]Monitor { +pub inline fn getAll(allocator: *mem.Allocator) mem.Allocator.Error![]Monitor { internal_debug.assertInitialized(); var count: c_int = 0; const monitors = c.glfwGetMonitors(&count); @@ -382,8 +378,7 @@ pub inline fn getAll(allocator: *mem.Allocator) (mem.Allocator.Error)![]Monitor /// @thread_safety This function must only be called from the main thread. /// /// see also: monitor_monitors, glfw.monitors.getAll -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn getPrimary() Error!?Monitor { +pub inline fn getPrimary() ?Monitor { internal_debug.assertInitialized(); const handle = c.glfwGetPrimaryMonitor(); getError() catch unreachable; @@ -426,8 +421,7 @@ pub const Event = enum(c_int) { /// @thread_safety This function must only be called from the main thread. /// /// see also: monitor_event -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monitor: Monitor, event: Event, data: *Data) void) Error!void { +pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monitor: Monitor, event: Event, data: *Data) void) void { internal_debug.assertInitialized(); if (f) |new_callback| { callback_fn_ptr = @ptrToInt(new_callback); @@ -466,7 +460,7 @@ test "getPrimary" { try glfw.init(.{}); defer glfw.terminate(); - _ = try getPrimary(); + _ = getPrimary(); } test "getPos" { @@ -474,7 +468,7 @@ test "getPos" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { _ = try m.getPos(); } @@ -485,7 +479,7 @@ test "getWorkarea" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { _ = try m.getWorkarea(); } @@ -496,9 +490,9 @@ test "getPhysicalSize" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { - _ = try m.getPhysicalSize(); + _ = m.getPhysicalSize(); } } @@ -507,7 +501,7 @@ test "getContentScale" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { _ = try m.getContentScale(); } @@ -518,9 +512,9 @@ test "getName" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { - _ = try m.getName(); + _ = m.getName(); } } @@ -529,13 +523,13 @@ test "userPointer" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { - var p = try m.getUserPointer(u32); + var p = m.getUserPointer(u32); try testing.expect(p == null); var x: u32 = 5; - try m.setUserPointer(u32, &x); - p = try m.getUserPointer(u32); + m.setUserPointer(u32, &x); + p = m.getUserPointer(u32); try testing.expectEqual(p.?.*, 5); } } @@ -546,7 +540,7 @@ test "setCallback" { defer glfw.terminate(); var custom_data: u32 = 5; - try setCallback(u32, &custom_data, &(struct { + setCallback(u32, &custom_data, &(struct { fn callback(monitor: Monitor, event: Event, data: *u32) void { _ = monitor; _ = event; @@ -560,7 +554,7 @@ test "getVideoModes" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { const allocator = testing.allocator; const modes = try m.getVideoModes(allocator); @@ -573,7 +567,7 @@ test "getVideoMode" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { _ = try m.getVideoMode(); } @@ -585,7 +579,7 @@ test "set_getGammaRamp" { try glfw.init(.{}); defer glfw.terminate(); - const monitor = try getPrimary(); + const monitor = getPrimary(); if (monitor) |m| { const ramp = m.getGammaRamp() catch |err| { std.debug.print("can't get window position, wayland maybe? error={}\n", .{err}); diff --git a/glfw/src/Window.zig b/glfw/src/Window.zig index 0ce23297..18b9f985 100644 --- a/glfw/src/Window.zig +++ b/glfw/src/Window.zig @@ -72,8 +72,7 @@ pub const InternalUserPointer = struct { /// @thread_safety This function must only be called from the main thread. /// /// see also: window_hints, glfw.Window.hint, glfw.Window.hintString -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -inline fn defaultHints() Error!void { +inline fn defaultHints() void { internal_debug.assertInitialized(); c.glfwDefaultWindowHints(); getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible @@ -407,7 +406,7 @@ pub inline fn create(width: usize, height: usize, title: [*:0]const u8, monitor: internal_debug.assertInitialized(); const ignore_hints_struct = if (comptime @import("builtin").is_test) testing_ignore_window_hints_struct else false; if (!ignore_hints_struct) try hints.set(); - defer if (!ignore_hints_struct) defaultHints() catch unreachable; // this should be unreachable, being that this should be caught in the previous call to `Hints.set`. + defer if (!ignore_hints_struct) defaultHints(); const handle = c.glfwCreateWindow( @intCast(c_int, width), @@ -492,8 +491,7 @@ pub inline fn shouldClose(self: Window) bool { /// synchronized. /// /// see also: window_close -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn setShouldClose(self: Window, value: bool) Error!void { +pub inline fn setShouldClose(self: Window, value: bool) void { internal_debug.assertInitialized(); const boolean = if (value) c.GLFW_TRUE else c.GLFW_FALSE; c.glfwSetWindowShouldClose(self.handle, boolean); @@ -1112,8 +1110,7 @@ pub inline fn swapBuffers(self: Window) Error!void { /// @thread_safety This function must only be called from the main thread. /// /// see also: window_monitor, glfw.Window.setMonitor -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn getMonitor(self: Window) Error!?Monitor { +pub inline fn getMonitor(self: Window) ?Monitor { internal_debug.assertInitialized(); const monitor = c.glfwGetWindowMonitor(self.handle); getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible @@ -2165,8 +2162,7 @@ fn setDropCallbackWrapper(handle: ?*c.GLFWwindow, path_count: c_int, paths: [*c] /// @thread_safety This function must only be called from the main thread. /// /// see also: path_drop -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn setDropCallback(self: Window, callback: ?fn (window: Window, paths: [][*:0]const u8) void) Error!void { +pub inline fn setDropCallback(self: Window, callback: ?fn (window: Window, paths: [][*:0]const u8) void) void { internal_debug.assertInitialized(); var internal = self.getInternal(); internal.setDropCallback = callback; @@ -2240,7 +2236,7 @@ test "defaultHints" { try glfw.init(.{}); defer glfw.terminate(); - try defaultHints(); + defaultHints(); } test "hint comptime int" { @@ -2248,7 +2244,7 @@ test "hint comptime int" { defer glfw.terminate(); try hint(.focused, 1); - try defaultHints(); + defaultHints(); } test "hint int" { @@ -2258,7 +2254,7 @@ test "hint int" { var focused: i32 = 1; try hint(.focused, focused); - try defaultHints(); + defaultHints(); } test "hint bool" { @@ -2266,7 +2262,7 @@ test "hint bool" { defer glfw.terminate(); try hint(.focused, true); - try defaultHints(); + defaultHints(); } test "hint enum(u1)" { @@ -2279,7 +2275,7 @@ test "hint enum(u1)" { }; try hint(.focused, MyEnum.@"true"); - try defaultHints(); + defaultHints(); } test "hint enum(i32)" { @@ -2292,7 +2288,7 @@ test "hint enum(i32)" { }; try hint(.focused, MyEnum.@"true"); - try defaultHints(); + defaultHints(); } test "hint array str" { @@ -2302,7 +2298,7 @@ test "hint array str" { const str_arr = [_]u8{ 'm', 'y', 'c', 'l', 'a', 's', 's' }; try hint(.x11_class_name, str_arr); - try defaultHints(); + defaultHints(); } test "hint pointer str" { @@ -2335,7 +2331,7 @@ test "setShouldClose" { std.debug.print("note: failed to create window: {}\n", .{err}); return; }; - try window.setShouldClose(true); + window.setShouldClose(true); defer window.destroy(); } @@ -2671,7 +2667,7 @@ test "getMonitor" { }; defer window.destroy(); - _ = window.getMonitor() catch |err| std.debug.print("can't get monitor, not supported by OS maybe? error={}\n", .{err}); + _ = window.getMonitor(); } test "setMonitor" { @@ -2956,7 +2952,7 @@ test "setDropCallback" { _ = _window; _ = paths; } - }).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err}); + }).callback); } test "getInputModeCursor" { diff --git a/glfw/src/vulkan.zig b/glfw/src/vulkan.zig index c9e4c4bf..4cc854aa 100644 --- a/glfw/src/vulkan.zig +++ b/glfw/src/vulkan.zig @@ -24,8 +24,7 @@ const internal_debug = @import("internal_debug.zig"); /// Possible errors include glfw.Error.NotInitialized. /// /// @thread_safety This function may be called from any thread. -// TODO: Consider whether to retain error here, despite us guaranteeing the absence of 'GLFW_NOT_INITIALIZED' -pub inline fn vulkanSupported() Error!bool { +pub inline fn vulkanSupported() bool { internal_debug.assertInitialized(); const supported = c.glfwVulkanSupported(); getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible @@ -234,7 +233,7 @@ test "vulkanSupported" { try glfw.init(.{}); defer glfw.terminate(); - _ = try glfw.vulkanSupported(); + _ = glfw.vulkanSupported(); } test "getRequiredInstanceExtensions" {