From 60160f3a6b8fb4ac30e0ea720f692e877a5bc3b7 Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 02:14:00 -0700 Subject: [PATCH 1/7] add num_enum and IntoPrimitive impl --- Cargo.toml | 3 +++ src/event.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 15ff1977..3fd572cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ exclude = ["examples/"] keywords = ["graphics", "3D", "opengl", "gamedev", "windowing"] categories = ["rendering::graphics-api"] +[dependencies] +num_enum = "0.7.6" + [features] # Optional log-rs like macros implementation diff --git a/src/event.rs b/src/event.rs index 82e0a925..a614ee33 100644 --- a/src/event.rs +++ b/src/event.rs @@ -14,7 +14,7 @@ pub struct Touch { pub y: f32, } -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::IntoPrimitive)] #[repr(u16)] /// These keycode values are based off of X11's `keysymdef.h`. /// Missing keycodes from that list are given the prefix 0x01. From 9d0f7caf635f63f974b546c4814e8302fd227b16 Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 02:35:14 -0700 Subject: [PATCH 2/7] full impl, tests, fix format --- src/event.rs | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/event.rs b/src/event.rs index a614ee33..f1712a2a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,9 +1,12 @@ -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] +#[derive( + Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive, +)] #[repr(u8)] pub enum MouseButton { Left = 0, Middle = 1, Right = 2, + #[num_enum(default)] Unknown = 255, } @@ -14,7 +17,9 @@ pub struct Touch { pub y: f32, } -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::IntoPrimitive)] +#[derive( + Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive, +)] #[repr(u16)] /// These keycode values are based off of X11's `keysymdef.h`. /// Missing keycodes from that list are given the prefix 0x01. @@ -141,6 +146,7 @@ pub enum KeyCode { Menu = 0xff67, // Android back button Back = 0xff04, + #[num_enum(default)] Unknown = 0x01ff, } @@ -177,7 +183,7 @@ pub trait EventHandler { fn mouse_button_down_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {} fn mouse_button_up_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {} fn mouse_leave_event(&mut self) {} - fn mouse_enter_event(&mut self, _button: MouseButton ,_x: f32, _y: f32 ) {} + fn mouse_enter_event(&mut self, _button: MouseButton, _x: f32, _y: f32) {} fn char_event(&mut self, _character: char, _keymods: KeyMods, _repeat: bool) {} @@ -234,3 +240,33 @@ pub trait EventHandler { /// can be requested with `ctx.dropped_file_bytes()`. fn files_dropped_event(&mut self) {} } + +#[test] +fn test_mouse_button_from_u8() { + assert_eq!(MouseButton::from(0), MouseButton::Left); + assert_eq!(MouseButton::from(1), MouseButton::Middle); + assert_eq!(MouseButton::from(2), MouseButton::Right); + assert_eq!(MouseButton::from(3), MouseButton::Unknown); +} + +#[test] +fn test_mouse_button_into_u8() { + assert_eq!(u8::from(MouseButton::Left), 0); + assert_eq!(u8::from(MouseButton::Middle), 1); + assert_eq!(u8::from(MouseButton::Right), 2); + assert_eq!(u8::from(MouseButton::Unknown), 255); +} + +#[test] +fn test_keycode_from_u16() { + assert_eq!(KeyCode::from(0x0020), KeyCode::Space); + assert_eq!(KeyCode::from(0x0041), KeyCode::A); + assert_eq!(KeyCode::from(0x0), KeyCode::Unknown); +} + +#[test] +fn test_keycode_into_u16() { + assert_eq!(u16::from(KeyCode::Space), 0x0020); + assert_eq!(u16::from(KeyCode::A), 0x0041); + assert_eq!(u16::from(KeyCode::Unknown), 0x01ff); +} From 956062c735c5051f04efc8f0ec77a877edcf2d36 Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 02:40:22 -0700 Subject: [PATCH 3/7] manual enum -> primitive conversion --- src/event.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/event.rs b/src/event.rs index f1712a2a..46b8b1c6 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,6 +1,4 @@ -#[derive( - Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive, -)] +#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive)] #[repr(u8)] pub enum MouseButton { Left = 0, @@ -10,6 +8,12 @@ pub enum MouseButton { Unknown = 255, } +impl From for u8 { + fn from(button: MouseButton) -> Self { + button as u8 + } +} + #[derive(Debug, Copy, Clone)] pub struct Touch { pub id: u32, @@ -17,9 +21,7 @@ pub struct Touch { pub y: f32, } -#[derive( - Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive, num_enum::IntoPrimitive, -)] +#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive)] #[repr(u16)] /// These keycode values are based off of X11's `keysymdef.h`. /// Missing keycodes from that list are given the prefix 0x01. @@ -150,6 +152,12 @@ pub enum KeyCode { Unknown = 0x01ff, } +impl From for u16 { + fn from(keycode: KeyCode) -> Self { + keycode as u16 + } +} + #[derive(Debug, Copy, Clone, PartialEq, Default)] pub struct KeyMods { pub shift: bool, From 1097d612065269db8ba47a97bf1339fedbf98d8c Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 13:14:10 -0700 Subject: [PATCH 4/7] working but kinda ugly --- src/event.rs | 17 ++++++----------- src/lib.rs | 1 + src/macros.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 src/macros.rs diff --git a/src/event.rs b/src/event.rs index 46b8b1c6..64108a57 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,18 +1,13 @@ -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive)] -#[repr(u8)] -pub enum MouseButton { +use crate::macros::convertible_enum; + +convertible_enum!( + /// Foo bar +MouseButton, repr = u8, unknown = Unknown, { Left = 0, Middle = 1, Right = 2, - #[num_enum(default)] Unknown = 255, -} - -impl From for u8 { - fn from(button: MouseButton) -> Self { - button as u8 - } -} +}); #[derive(Debug, Copy, Clone)] pub struct Touch { diff --git a/src/lib.rs b/src/lib.rs index 719b2c67..1821e88e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub mod conf; mod event; pub mod fs; pub mod graphics; +mod macros; pub mod native; use std::collections::HashMap; use std::ops::{Index, IndexMut}; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 00000000..42978158 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,33 @@ +macro_rules! convertible_enum { + ( + $(#[$attr:meta])* + $name: ident, + repr = $repr: ty, + unknown = $unknown_variant: ident, + {$($variant: ident = $value: expr),* $(,)?} + ) => { + #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] + $(#[$attr])* + #[repr($repr)] + pub enum $name { + $($variant = $value,)* + } + + impl From<$repr> for $name { + fn from(value: $repr) -> Self { + match value { + $($value => $name::$variant,)* + _ => $name::$unknown_variant, + } + } + } + + impl From<$name> for $repr { + fn from(value: $name) -> Self { + value as $repr + } + } + }; +} + +pub(crate) use convertible_enum; From 063cbe8c4e8c0937a631f9338d53d2009525180f Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 13:35:23 -0700 Subject: [PATCH 5/7] working impl without num_enum --- Cargo.toml | 3 - src/event.rs | 283 +++++++++++++++++++++++++------------------------- src/macros.rs | 30 ++++-- 3 files changed, 161 insertions(+), 155 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fd572cb..15ff1977 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,6 @@ exclude = ["examples/"] keywords = ["graphics", "3D", "opengl", "gamedev", "windowing"] categories = ["rendering::graphics-api"] -[dependencies] -num_enum = "0.7.6" - [features] # Optional log-rs like macros implementation diff --git a/src/event.rs b/src/event.rs index 64108a57..e6108a57 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,13 +1,17 @@ use crate::macros::convertible_enum; -convertible_enum!( - /// Foo bar -MouseButton, repr = u8, unknown = Unknown, { - Left = 0, - Middle = 1, - Right = 2, - Unknown = 255, -}); +convertible_enum! { + repr = u8, + unknown = Unknown, + + #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] + pub enum MouseButton { + Left = 0, + Middle = 1, + Right = 2, + Unknown = 255, + } +} #[derive(Debug, Copy, Clone)] pub struct Touch { @@ -16,140 +20,137 @@ pub struct Touch { pub y: f32, } -#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, num_enum::FromPrimitive)] -#[repr(u16)] -/// These keycode values are based off of X11's `keysymdef.h`. -/// Missing keycodes from that list are given the prefix 0x01. -pub enum KeyCode { - Space = 0x0020, - Apostrophe = 0x0027, - Comma = 0x002c, - Minus = 0x002d, - Period = 0x002e, - Slash = 0x002f, - Key0 = 0x0030, - Key1 = 0x0031, - Key2 = 0x0032, - Key3 = 0x0033, - Key4 = 0x0034, - Key5 = 0x0035, - Key6 = 0x0036, - Key7 = 0x0037, - Key8 = 0x0038, - Key9 = 0x0039, - Semicolon = 0x003b, - Equal = 0x003d, - A = 0x0041, - B = 0x0042, - C = 0x0043, - D = 0x0044, - E = 0x0045, - F = 0x0046, - G = 0x0047, - H = 0x0048, - I = 0x0049, - J = 0x004a, - K = 0x004b, - L = 0x004c, - M = 0x004d, - N = 0x004e, - O = 0x004f, - P = 0x0050, - Q = 0x0051, - R = 0x0052, - S = 0x0053, - T = 0x0054, - U = 0x0055, - V = 0x0056, - W = 0x0057, - X = 0x0058, - Y = 0x0059, - Z = 0x005a, - LeftBracket = 0x005b, - Backslash = 0x005c, - RightBracket = 0x005d, - GraveAccent = 0x0060, - World1 = 0x0100, - World2 = 0x0101, - Escape = 0xff1b, - Enter = 0xff0d, - Tab = 0xff09, - Backspace = 0xff08, - Insert = 0xff63, - Delete = 0xffff, - Right = 0xff53, - Left = 0xff51, - Down = 0xff54, - Up = 0xff52, - PageUp = 0xff55, - PageDown = 0xff56, - Home = 0xff50, - End = 0xff57, - CapsLock = 0xffe5, - ScrollLock = 0xff14, - NumLock = 0xff7f, - PrintScreen = 0xfd1d, - Pause = 0xff13, - F1 = 0xffbe, - F2 = 0xffbf, - F3 = 0xffc0, - F4 = 0xffc1, - F5 = 0xffc2, - F6 = 0xffc3, - F7 = 0xffc4, - F8 = 0xffc5, - F9 = 0xffc6, - F10 = 0xffc7, - F11 = 0xffc8, - F12 = 0xffc9, - F13 = 0xffca, - F14 = 0xffcb, - F15 = 0xffcc, - F16 = 0xffcd, - F17 = 0xffce, - F18 = 0xffcf, - F19 = 0xffd0, - F20 = 0xffd1, - F21 = 0xffd2, - F22 = 0xffd3, - F23 = 0xffd4, - F24 = 0xffd5, - F25 = 0xffd6, - Kp0 = 0xffb0, - Kp1 = 0xffb1, - Kp2 = 0xffb2, - Kp3 = 0xffb3, - Kp4 = 0xffb4, - Kp5 = 0xffb5, - Kp6 = 0xffb6, - Kp7 = 0xffb7, - Kp8 = 0xffb8, - Kp9 = 0xffb9, - KpDecimal = 0xffae, - KpDivide = 0xffaf, - KpMultiply = 0xffaa, - KpSubtract = 0xffad, - KpAdd = 0xffab, - KpEnter = 0xff8d, - KpEqual = 0xffbd, - LeftShift = 0xffe1, - LeftControl = 0xffe3, - LeftAlt = 0xffe9, - LeftSuper = 0xffeb, - RightShift = 0xffe2, - RightControl = 0xffe4, - RightAlt = 0xffea, - RightSuper = 0xffec, - Menu = 0xff67, - // Android back button - Back = 0xff04, - #[num_enum(default)] - Unknown = 0x01ff, -} +convertible_enum! { + repr = u16, + unknown = Unknown, -impl From for u16 { - fn from(keycode: KeyCode) -> Self { - keycode as u16 + /// These keycode values are based off of X11's `keysymdef.h`. + /// Missing keycodes from that list are given the prefix 0x01. + #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] + pub enum KeyCode { + Space = 0x0020, + Apostrophe = 0x0027, + Comma = 0x002c, + Minus = 0x002d, + Period = 0x002e, + Slash = 0x002f, + Key0 = 0x0030, + Key1 = 0x0031, + Key2 = 0x0032, + Key3 = 0x0033, + Key4 = 0x0034, + Key5 = 0x0035, + Key6 = 0x0036, + Key7 = 0x0037, + Key8 = 0x0038, + Key9 = 0x0039, + Semicolon = 0x003b, + Equal = 0x003d, + A = 0x0041, + B = 0x0042, + C = 0x0043, + D = 0x0044, + E = 0x0045, + F = 0x0046, + G = 0x0047, + H = 0x0048, + I = 0x0049, + J = 0x004a, + K = 0x004b, + L = 0x004c, + M = 0x004d, + N = 0x004e, + O = 0x004f, + P = 0x0050, + Q = 0x0051, + R = 0x0052, + S = 0x0053, + T = 0x0054, + U = 0x0055, + V = 0x0056, + W = 0x0057, + X = 0x0058, + Y = 0x0059, + Z = 0x005a, + LeftBracket = 0x005b, + Backslash = 0x005c, + RightBracket = 0x005d, + GraveAccent = 0x0060, + World1 = 0x0100, + World2 = 0x0101, + Escape = 0xff1b, + Enter = 0xff0d, + Tab = 0xff09, + Backspace = 0xff08, + Insert = 0xff63, + Delete = 0xffff, + Right = 0xff53, + Left = 0xff51, + Down = 0xff54, + Up = 0xff52, + PageUp = 0xff55, + PageDown = 0xff56, + Home = 0xff50, + End = 0xff57, + CapsLock = 0xffe5, + ScrollLock = 0xff14, + NumLock = 0xff7f, + PrintScreen = 0xfd1d, + Pause = 0xff13, + F1 = 0xffbe, + F2 = 0xffbf, + F3 = 0xffc0, + F4 = 0xffc1, + F5 = 0xffc2, + F6 = 0xffc3, + F7 = 0xffc4, + F8 = 0xffc5, + F9 = 0xffc6, + F10 = 0xffc7, + F11 = 0xffc8, + F12 = 0xffc9, + F13 = 0xffca, + F14 = 0xffcb, + F15 = 0xffcc, + F16 = 0xffcd, + F17 = 0xffce, + F18 = 0xffcf, + F19 = 0xffd0, + F20 = 0xffd1, + F21 = 0xffd2, + F22 = 0xffd3, + F23 = 0xffd4, + F24 = 0xffd5, + F25 = 0xffd6, + Kp0 = 0xffb0, + Kp1 = 0xffb1, + Kp2 = 0xffb2, + Kp3 = 0xffb3, + Kp4 = 0xffb4, + Kp5 = 0xffb5, + Kp6 = 0xffb6, + Kp7 = 0xffb7, + Kp8 = 0xffb8, + Kp9 = 0xffb9, + KpDecimal = 0xffae, + KpDivide = 0xffaf, + KpMultiply = 0xffaa, + KpSubtract = 0xffad, + KpAdd = 0xffab, + KpEnter = 0xff8d, + KpEqual = 0xffbd, + LeftShift = 0xffe1, + LeftControl = 0xffe3, + LeftAlt = 0xffe9, + LeftSuper = 0xffeb, + RightShift = 0xffe2, + RightControl = 0xffe4, + RightAlt = 0xffea, + RightSuper = 0xffec, + Menu = 0xff67, + /// Android back button + Back = 0xff04, + Unknown = 0x01ff, } } diff --git a/src/macros.rs b/src/macros.rs index 42978158..2e7be713 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,23 +1,31 @@ macro_rules! convertible_enum { ( - $(#[$attr:meta])* - $name: ident, - repr = $repr: ty, - unknown = $unknown_variant: ident, - {$($variant: ident = $value: expr),* $(,)?} + repr = $repr:ident, + unknown = $unknown_variant:ident, + $(#[$outer:meta])* + $vis:vis enum $name:ident { + $( + $(#[$inner:meta])* + $variant:ident = $value:expr + ),* $(,)? + } ) => { - #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] - $(#[$attr])* + $(#[$outer])* #[repr($repr)] - pub enum $name { - $($variant = $value,)* + $vis enum $name { + $( + $(#[$inner])* + $variant = $value, + )* } impl From<$repr> for $name { fn from(value: $repr) -> Self { match value { - $($value => $name::$variant,)* - _ => $name::$unknown_variant, + $( + $value => Self::$variant, + )* + _ => Self::$unknown_variant, } } } From 34b0f563ac779f6d6c3f096ac57783b007335776 Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 13:36:34 -0700 Subject: [PATCH 6/7] reduce diff --- src/event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/event.rs b/src/event.rs index e6108a57..c774fdae 100644 --- a/src/event.rs +++ b/src/event.rs @@ -24,9 +24,9 @@ convertible_enum! { repr = u16, unknown = Unknown, + #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] /// These keycode values are based off of X11's `keysymdef.h`. /// Missing keycodes from that list are given the prefix 0x01. - #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] pub enum KeyCode { Space = 0x0020, Apostrophe = 0x0027, From 97f11f8fa8e5ae3659ac922603530a70b3712bb4 Mon Sep 17 00:00:00 2001 From: Andy Barron Date: Sat, 8 Aug 2026 13:46:10 -0700 Subject: [PATCH 7/7] macro doc comment --- src/macros.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index 2e7be713..c4f989a0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,3 +1,21 @@ +/// This macro accepts a data-less enum definition and generates `From` impls +/// for converting between the enum and its repr type. +/// +/// Example usage: +/// ```ignore +/// convertible_enum! { +/// repr = u8, +/// unknown = Unknown, +/// +/// #[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)] +/// pub enum MouseButton { +/// Left = 0, +/// Middle = 1, +/// Right = 2, +/// Unknown = 255, +/// } +/// } +/// ``` macro_rules! convertible_enum { ( repr = $repr:ident,