From 55feb6e67f7579edbcb85ae940f8a780eb394e97 Mon Sep 17 00:00:00 2001 From: nativerv Date: Mon, 19 Feb 2024 03:44:00 +0300 Subject: [PATCH] refactor: trying to do something right! --- crate/bevy_controls/example/basic/Cargo.lock | 9 ++++ crate/bevy_controls/example/basic/src/main.rs | 3 +- crate/bevy_controls/macros/src/lib.rs | 38 +++++++++++++++++ crate/bevy_controls/src/contract.rs | 41 ++++++------------- crate/bevy_controls_derive/src/lib.rs | 2 - 5 files changed, 62 insertions(+), 31 deletions(-) diff --git a/crate/bevy_controls/example/basic/Cargo.lock b/crate/bevy_controls/example/basic/Cargo.lock index 8662db4..cfe1895 100644 --- a/crate/bevy_controls/example/basic/Cargo.lock +++ b/crate/bevy_controls/example/basic/Cargo.lock @@ -400,6 +400,7 @@ name = "bevy_controls" version = "0.1.0" dependencies = [ "bevy_app", + "bevy_controls_macros", "bevy_derive", "bevy_ecs", "bevy_input", @@ -418,6 +419,14 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bevy_controls_macros" +version = "0.1.0" +dependencies = [ + "quote", + "syn 1.0.109", +] + [[package]] name = "bevy_core" version = "0.12.1" diff --git a/crate/bevy_controls/example/basic/src/main.rs b/crate/bevy_controls/example/basic/src/main.rs index 0c72637..716e77a 100644 --- a/crate/bevy_controls/example/basic/src/main.rs +++ b/crate/bevy_controls/example/basic/src/main.rs @@ -2,7 +2,8 @@ //! //! It displays //! -//! Yeah I know, now it's a bit ugly +//! Yeah I know, that's kind of weird that it displays +//! But it does......... use bevy::{ecs::schedule::States, prelude::*}; use bevy_controls::{ diff --git a/crate/bevy_controls/macros/src/lib.rs b/crate/bevy_controls/macros/src/lib.rs index be6b253..62c3fba 100644 --- a/crate/bevy_controls/macros/src/lib.rs +++ b/crate/bevy_controls/macros/src/lib.rs @@ -114,3 +114,41 @@ pub fn game_state_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenS TokenStream::from(out) } + +#[proc_macro_attribute] +pub fn action_supertraits(_attrs: TokenStream, input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemTrait); + + let mut bounds = Vec::new(); + if cfg!(feature = "serialize") { + bounds.push(quote! { serde::Serialize }); + bounds.push(quote! { serde::de::DeserializeOwned }); + }; + if cfg!(feature = "reflect") { + bounds.push(quote! { bevy_reflect::Reflect }); + bounds.push(quote! { bevy_reflect::TypePath }); + bounds.push(quote! { bevy_reflect::FromReflect }); + }; + if cfg!(feature = "inspector-egui") { + bounds.push(quote! { bevy_inspector_egui::inspector_options::InspectorOptionsType }); + }; + + let ItemTrait { + ident, + vis, + items, + supertraits: bounds_old, + .. +} = input; + + let bounds_old = bounds_old.iter().collect::>(); + + let out = quote! { + #vis trait #ident: #(#bounds_old)+* + #(#bounds)+* { #(#items)* } + }; + + // dbg!(&out); + // dbg!(out.to_string()); + + TokenStream::from(out) +} diff --git a/crate/bevy_controls/src/contract.rs b/crate/bevy_controls/src/contract.rs index 14a05c8..0c656e0 100644 --- a/crate/bevy_controls/src/contract.rs +++ b/crate/bevy_controls/src/contract.rs @@ -56,34 +56,19 @@ pub trait InspectorEguiImpl: InspectorOptionsType {} not(feature = "reflect"), not(feature = "inspector-egui") ))] -pub trait Action: ActionInner {} - -#[cfg(all( - feature = "serialize", - not(feature = "reflect"), - not(feature = "inspector-egui") -))] -pub trait Action: ActionInner + SerializeImpl {} - -#[cfg(all( - not(feature = "serialize"), - feature = "reflect", - not(feature = "inspector-egui") -))] -pub trait Action: ActionInner + ReflectImpl {} - -#[cfg(all( - feature = "serialize", - feature = "reflect", - not(feature = "inspector-egui") -))] -pub trait Action: ActionInner + SerializeImpl + ReflectImpl {} - -#[cfg(all(not(feature = "serialize"), feature = "inspector-egui"))] -pub trait Action: ActionInner + ReflectImpl + InspectorEguiImpl {} - -#[cfg(all(feature = "serialize", feature = "inspector-egui"))] -pub trait Action: ActionInner + SerializeImpl + ReflectImpl + InspectorEguiImpl {} + +#[game_state_supertraits] +pub trait Action: 'static + + std::marker::Sync + + std::marker::Send + + PartialEq + + Eq + + Hash + + IntoEnumIterator + + Clone + + Copy + + std::fmt::Debug +{} // ===== GameState ===== // SAFETY: inspector-egui includes reflect diff --git a/crate/bevy_controls_derive/src/lib.rs b/crate/bevy_controls_derive/src/lib.rs index cec5fd5..c135cda 100644 --- a/crate/bevy_controls_derive/src/lib.rs +++ b/crate/bevy_controls_derive/src/lib.rs @@ -11,7 +11,6 @@ pub fn action(input: TokenStream) -> TokenStream { let name = &ast.ident; let gen = quote! { impl bevy_controls::contract::Action for #name {} - impl bevy_controls::contract::ActionInner for #name {} }; gen.into() } @@ -23,7 +22,6 @@ pub fn game_state(input: TokenStream) -> TokenStream { let name = &ast.ident; let gen = quote! { impl bevy_controls::contract::GameState for #name {} - impl bevy_controls::contract::GameStateInner for #name {} }; gen.into() }