glfw: Change error unions into normal returns, in accordance with the new guarantee to never encounter 'GLFW_NOT_INITIALIZED', and update tests

This commit is contained in:
InKryption 2021-11-22 20:21:46 +01:00 committed by Stephen Gutekanst
parent 88e0d37325
commit b35a7b4fad
3 changed files with 40 additions and 51 deletions

View file

@ -98,8 +98,7 @@ const PhysicalSize = struct {
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: monitor_properties /// 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) PhysicalSize {
pub inline fn getPhysicalSize(self: Monitor) Error!PhysicalSize {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var width_mm: c_int = 0; var width_mm: c_int = 0;
var height_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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: monitor_properties /// 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) [*:0]const u8 {
pub inline fn getName(self: Monitor) Error![*:0]const u8 {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const name = c.glfwGetMonitorName(self.handle); const name = c.glfwGetMonitorName(self.handle);
getError() catch unreachable; 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. /// @thread_safety This function may be called from any thread. Access is not synchronized.
/// ///
/// see also: monitor_userptr, glfw.Monitor.getUserPointer /// 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) void {
pub inline fn setUserPointer(self: Monitor, comptime T: type, ptr: *T) Error!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
c.glfwSetMonitorUserPointer(self.handle, ptr); c.glfwSetMonitorUserPointer(self.handle, ptr);
getError() catch unreachable; 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. /// @thread_safety This function may be called from any thread. Access is not synchronized.
/// ///
/// see also: monitor_userptr, glfw.Monitor.setUserPointer /// 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) ?*T {
pub inline fn getUserPointer(self: Monitor, comptime T: type) Error!?*T {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const ptr = c.glfwGetMonitorUserPointer(self.handle); const ptr = c.glfwGetMonitorUserPointer(self.handle);
getError() catch unreachable; 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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: monitor_monitors, monitor_event, glfw.monitor.getPrimary /// 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(); internal_debug.assertInitialized();
var count: c_int = 0; var count: c_int = 0;
const monitors = c.glfwGetMonitors(&count); 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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: monitor_monitors, glfw.monitors.getAll /// 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() ?Monitor {
pub inline fn getPrimary() Error!?Monitor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const handle = c.glfwGetPrimaryMonitor(); const handle = c.glfwGetPrimaryMonitor();
getError() catch unreachable; 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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: monitor_event /// 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) void {
pub inline fn setCallback(comptime Data: type, data: *Data, f: ?*const fn (monitor: Monitor, event: Event, data: *Data) void) Error!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
if (f) |new_callback| { if (f) |new_callback| {
callback_fn_ptr = @ptrToInt(new_callback); callback_fn_ptr = @ptrToInt(new_callback);
@ -466,7 +460,7 @@ test "getPrimary" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
_ = try getPrimary(); _ = getPrimary();
} }
test "getPos" { test "getPos" {
@ -474,7 +468,7 @@ test "getPos" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getPos(); _ = try m.getPos();
} }
@ -485,7 +479,7 @@ test "getWorkarea" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getWorkarea(); _ = try m.getWorkarea();
} }
@ -496,9 +490,9 @@ test "getPhysicalSize" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getPhysicalSize(); _ = m.getPhysicalSize();
} }
} }
@ -507,7 +501,7 @@ test "getContentScale" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getContentScale(); _ = try m.getContentScale();
} }
@ -518,9 +512,9 @@ test "getName" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getName(); _ = m.getName();
} }
} }
@ -529,13 +523,13 @@ test "userPointer" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
var p = try m.getUserPointer(u32); var p = m.getUserPointer(u32);
try testing.expect(p == null); try testing.expect(p == null);
var x: u32 = 5; var x: u32 = 5;
try m.setUserPointer(u32, &x); m.setUserPointer(u32, &x);
p = try m.getUserPointer(u32); p = m.getUserPointer(u32);
try testing.expectEqual(p.?.*, 5); try testing.expectEqual(p.?.*, 5);
} }
} }
@ -546,7 +540,7 @@ test "setCallback" {
defer glfw.terminate(); defer glfw.terminate();
var custom_data: u32 = 5; 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 { fn callback(monitor: Monitor, event: Event, data: *u32) void {
_ = monitor; _ = monitor;
_ = event; _ = event;
@ -560,7 +554,7 @@ test "getVideoModes" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
const allocator = testing.allocator; const allocator = testing.allocator;
const modes = try m.getVideoModes(allocator); const modes = try m.getVideoModes(allocator);
@ -573,7 +567,7 @@ test "getVideoMode" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
_ = try m.getVideoMode(); _ = try m.getVideoMode();
} }
@ -585,7 +579,7 @@ test "set_getGammaRamp" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
const monitor = try getPrimary(); const monitor = getPrimary();
if (monitor) |m| { if (monitor) |m| {
const ramp = m.getGammaRamp() catch |err| { const ramp = m.getGammaRamp() catch |err| {
std.debug.print("can't get window position, wayland maybe? error={}\n", .{err}); std.debug.print("can't get window position, wayland maybe? error={}\n", .{err});

View file

@ -72,8 +72,7 @@ pub const InternalUserPointer = struct {
/// @thread_safety This function must only be called from the main thread. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: window_hints, glfw.Window.hint, glfw.Window.hintString /// 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() void {
inline fn defaultHints() Error!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
c.glfwDefaultWindowHints(); c.glfwDefaultWindowHints();
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible 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(); internal_debug.assertInitialized();
const ignore_hints_struct = if (comptime @import("builtin").is_test) testing_ignore_window_hints_struct else false; const ignore_hints_struct = if (comptime @import("builtin").is_test) testing_ignore_window_hints_struct else false;
if (!ignore_hints_struct) try hints.set(); 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( const handle = c.glfwCreateWindow(
@intCast(c_int, width), @intCast(c_int, width),
@ -492,8 +491,7 @@ pub inline fn shouldClose(self: Window) bool {
/// synchronized. /// synchronized.
/// ///
/// see also: window_close /// 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) void {
pub inline fn setShouldClose(self: Window, value: bool) Error!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const boolean = if (value) c.GLFW_TRUE else c.GLFW_FALSE; const boolean = if (value) c.GLFW_TRUE else c.GLFW_FALSE;
c.glfwSetWindowShouldClose(self.handle, boolean); 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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: window_monitor, glfw.Window.setMonitor /// 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) ?Monitor {
pub inline fn getMonitor(self: Window) Error!?Monitor {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const monitor = c.glfwGetWindowMonitor(self.handle); const monitor = c.glfwGetWindowMonitor(self.handle);
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible 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. /// @thread_safety This function must only be called from the main thread.
/// ///
/// see also: path_drop /// 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) void {
pub inline fn setDropCallback(self: Window, callback: ?fn (window: Window, paths: [][*:0]const u8) void) Error!void {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
var internal = self.getInternal(); var internal = self.getInternal();
internal.setDropCallback = callback; internal.setDropCallback = callback;
@ -2240,7 +2236,7 @@ test "defaultHints" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
try defaultHints(); defaultHints();
} }
test "hint comptime int" { test "hint comptime int" {
@ -2248,7 +2244,7 @@ test "hint comptime int" {
defer glfw.terminate(); defer glfw.terminate();
try hint(.focused, 1); try hint(.focused, 1);
try defaultHints(); defaultHints();
} }
test "hint int" { test "hint int" {
@ -2258,7 +2254,7 @@ test "hint int" {
var focused: i32 = 1; var focused: i32 = 1;
try hint(.focused, focused); try hint(.focused, focused);
try defaultHints(); defaultHints();
} }
test "hint bool" { test "hint bool" {
@ -2266,7 +2262,7 @@ test "hint bool" {
defer glfw.terminate(); defer glfw.terminate();
try hint(.focused, true); try hint(.focused, true);
try defaultHints(); defaultHints();
} }
test "hint enum(u1)" { test "hint enum(u1)" {
@ -2279,7 +2275,7 @@ test "hint enum(u1)" {
}; };
try hint(.focused, MyEnum.@"true"); try hint(.focused, MyEnum.@"true");
try defaultHints(); defaultHints();
} }
test "hint enum(i32)" { test "hint enum(i32)" {
@ -2292,7 +2288,7 @@ test "hint enum(i32)" {
}; };
try hint(.focused, MyEnum.@"true"); try hint(.focused, MyEnum.@"true");
try defaultHints(); defaultHints();
} }
test "hint array str" { test "hint array str" {
@ -2302,7 +2298,7 @@ test "hint array str" {
const str_arr = [_]u8{ 'm', 'y', 'c', 'l', 'a', 's', 's' }; const str_arr = [_]u8{ 'm', 'y', 'c', 'l', 'a', 's', 's' };
try hint(.x11_class_name, str_arr); try hint(.x11_class_name, str_arr);
try defaultHints(); defaultHints();
} }
test "hint pointer str" { test "hint pointer str" {
@ -2335,7 +2331,7 @@ test "setShouldClose" {
std.debug.print("note: failed to create window: {}\n", .{err}); std.debug.print("note: failed to create window: {}\n", .{err});
return; return;
}; };
try window.setShouldClose(true); window.setShouldClose(true);
defer window.destroy(); defer window.destroy();
} }
@ -2671,7 +2667,7 @@ test "getMonitor" {
}; };
defer window.destroy(); 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" { test "setMonitor" {
@ -2956,7 +2952,7 @@ test "setDropCallback" {
_ = _window; _ = _window;
_ = paths; _ = paths;
} }
}).callback) catch |err| std.debug.print("can't set window drop callback, not supported by OS maybe? error={}\n", .{err}); }).callback);
} }
test "getInputModeCursor" { test "getInputModeCursor" {

View file

@ -24,8 +24,7 @@ const internal_debug = @import("internal_debug.zig");
/// Possible errors include glfw.Error.NotInitialized. /// Possible errors include glfw.Error.NotInitialized.
/// ///
/// @thread_safety This function may be called from any thread. /// @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() bool {
pub inline fn vulkanSupported() Error!bool {
internal_debug.assertInitialized(); internal_debug.assertInitialized();
const supported = c.glfwVulkanSupported(); const supported = c.glfwVulkanSupported();
getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible getError() catch unreachable; // Only error 'GLFW_NOT_INITIALIZED' is impossible
@ -234,7 +233,7 @@ test "vulkanSupported" {
try glfw.init(.{}); try glfw.init(.{});
defer glfw.terminate(); defer glfw.terminate();
_ = try glfw.vulkanSupported(); _ = glfw.vulkanSupported();
} }
test "getRequiredInstanceExtensions" { test "getRequiredInstanceExtensions" {