glfw: make Hat.fromInt, Hat.toInt work on non-5-bit integers

Helps hexops/mach#37

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-10-30 13:00:04 -07:00 committed by Stephen Gutekanst
parent 7cc850982f
commit 2897061ffd

View file

@ -10,6 +10,7 @@ pub const Hat = packed struct {
right: bool = false, right: bool = false,
down: bool = false, down: bool = false,
left: bool = false, left: bool = false,
_reserved: u3 = 0,
inline fn verifyIntType(comptime IntType: type) void { inline fn verifyIntType(comptime IntType: type) void {
comptime { comptime {
@ -22,12 +23,12 @@ pub const Hat = packed struct {
pub inline fn toInt(self: Hat, comptime IntType: type) IntType { pub inline fn toInt(self: Hat, comptime IntType: type) IntType {
verifyIntType(IntType); verifyIntType(IntType);
return @bitCast(IntType, self); return @intCast(IntType, @bitCast(u8, self));
} }
pub inline fn fromInt(flags: anytype) Hat { pub inline fn fromInt(flags: anytype) Hat {
verifyIntType(@TypeOf(flags)); verifyIntType(@TypeOf(flags));
return @bitCast(Hat, flags); return @bitCast(Hat, @intCast(u8, flags));
} }
}; };