From 5755613e9027f823f963b0d3a846a9d66257dc95 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 25 Mar 2024 17:49:32 +0100 Subject: [PATCH] add derive macro for `Signal` --- Cargo.toml | 2 +- hui-derive/Cargo.toml | 23 +++++++++++++++++++++++ hui-derive/src/lib.rs | 12 ++++++++++++ hui/Cargo.toml | 8 ++++---- hui/src/input.rs | 2 ++ hui/src/lib.rs | 7 +++++-- 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 hui-derive/Cargo.toml create mode 100644 hui-derive/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index fc21b5f..5db48b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["hui", "hui-examples", "hui-glium", "hui-winit"] +members = ["hui", "hui-derive", "hui-examples", "hui-glium", "hui-winit"] diff --git a/hui-derive/Cargo.toml b/hui-derive/Cargo.toml new file mode 100644 index 0000000..b26f457 --- /dev/null +++ b/hui-derive/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "hui-derive" +description = "Derive macros for hUI" +repository = "https://github.com/griffi-gh/hui" +readme = "../README.md" +authors = ["griffi-gh "] +rust-version = "1.75" +version = "0.1.0-alpha.4" +edition = "2021" +license = "GPL-3.0-or-later" +publish = true +include = [ + "assets/**/*", + "src/**/*.rs", + "Cargo.toml", +] + +[lib] +proc-macro = true + +[dependencies] +quote = "1.0" +syn = "2.0" diff --git a/hui-derive/src/lib.rs b/hui-derive/src/lib.rs new file mode 100644 index 0000000..637b0a6 --- /dev/null +++ b/hui-derive/src/lib.rs @@ -0,0 +1,12 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + +#[proc_macro_derive(Signal)] +pub fn signal(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let name = input.ident; + quote!(impl ::hui::signal::Signal for #name {}).into() +} diff --git a/hui/Cargo.toml b/hui/Cargo.toml index a07657a..b450a95 100644 --- a/hui/Cargo.toml +++ b/hui/Cargo.toml @@ -16,6 +16,7 @@ include = [ ] [dependencies] +hui-derive = { version = "0.1.0-alpha.4", path = "../hui-derive", optional = true } hashbrown = "0.14" nohash-hasher = "0.2" glam = "0.27" @@ -29,15 +30,14 @@ tinyset = "0.4" image = { version = "0.25", default-features = false, optional = true } [features] -default = ["el_all", "image", "builtin_font", "pixel_perfect_text"] +default = ["el_all", "image", "builtin_font", "pixel_perfect_text", "derive"] -#! Image loading support: +## Enable derive macros +derive = ["dep:hui-derive"] ## Enable image loading support using the `image` crate image = ["dep:image"] -#! #### Built-in font: - ## Enable the built-in font (ProggyTiny, adds *35kb* to the executable) builtin_font = [] diff --git a/hui/src/input.rs b/hui/src/input.rs index 45e4cc5..8154e81 100644 --- a/hui/src/input.rs +++ b/hui/src/input.rs @@ -101,6 +101,8 @@ pub enum KeyboardKey { macro_rules! impl_fits64_for_keyboard_key { ($($i:ident = $v:literal),*) => { impl Fits64 for KeyboardKey { + // SAFETY: not actually doing anything unsafe + #[allow(unsafe_code)] unsafe fn from_u64(x: u64) -> Self { match x { $( $v => KeyboardKey::$i, )* diff --git a/hui/src/lib.rs b/hui/src/lib.rs index 42cbe65..fe61bb5 100644 --- a/hui/src/lib.rs +++ b/hui/src/lib.rs @@ -5,9 +5,12 @@ //! # Features #![doc = document_features::document_features!()] -#![allow(unused_parens)] -//#![forbid(unsafe_code)] +#![deny(unsafe_code)] #![forbid(unsafe_op_in_unsafe_fn)] +#![allow(unused_parens)] + +#[cfg(feature = "derive")] +pub use hui_derive::*; mod instance; mod macros;