glfw: Tidy up UserPointer access

This change both restricts and clarifies the mutability/nullability of the pointers, and replaces the explicitly-typed pointer usage in setUserPointer with ?*anyopaque, since it now, as of being renamed from c_void, more simply communicates the intent of taking any pointer type.
This commit is contained in:
InKryption 2022-01-02 19:36:38 +01:00 committed by Stephen Gutekanst
parent 1f748d1be8
commit 786da94468

View file

@ -1368,10 +1368,10 @@ pub inline fn getInternal(self: Window) *InternalUserPointer {
/// @thread_safety This function may be called from any thread. Access is not synchronized.
///
/// see also: window_userptr, glfw.Window.getUserPointer
pub inline fn setUserPointer(self: Window, comptime T: type, pointer: *T) void {
pub inline fn setUserPointer(self: Window, pointer: ?*anyopaque) void {
internal_debug.assertInitialized();
var internal = self.getInternal();
internal.user_pointer = @ptrCast(*anyopaque, pointer);
const internal = self.getInternal();
internal.user_pointer = pointer;
}
/// Returns the user pointer of the specified window.
@ -1382,10 +1382,10 @@ pub inline fn setUserPointer(self: Window, comptime T: type, pointer: *T) void {
/// @thread_safety This function may be called from any thread. Access is not synchronized.
///
/// see also: window_userptr, glfw.Window.setUserPointer
pub inline fn getUserPointer(self: Window, comptime PointerType: type) ?PointerType {
pub inline fn getUserPointer(self: Window, comptime T: type) ?*T {
internal_debug.assertInitialized();
var internal = self.getInternal();
if (internal.user_pointer) |p| return @ptrCast(PointerType, @alignCast(@alignOf(std.meta.Child(PointerType)), p));
const internal = self.getInternal();
if (internal.user_pointer) |p| return @ptrCast(?*T, @alignCast(@alignOf(T), p));
return null;
}
@ -2882,7 +2882,7 @@ test "setUserPointer" {
const T = struct { name: []const u8 };
var my_value = T{ .name = "my window!" };
window.setUserPointer(T, &my_value);
window.setUserPointer(&my_value);
}
test "getUserPointer" {
@ -2900,8 +2900,8 @@ test "getUserPointer" {
const T = struct { name: []const u8 };
var my_value = T{ .name = "my window!" };
window.setUserPointer(T, &my_value);
const got = window.getUserPointer(*T);
window.setUserPointer(&my_value);
const got = window.getUserPointer(T);
std.debug.assert(&my_value == got);
}