????
This commit is contained in:
parent
d594b3f568
commit
62438e0d24
15 changed files with 157 additions and 7 deletions
14
crates/moonhare_derives/Cargo.toml
Normal file
14
crates/moonhare_derives/Cargo.toml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
edition = "2024"
|
||||
name = "moonhare_derives"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
syn = "2.0"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
32
crates/moonhare_derives/src/lib.rs
Normal file
32
crates/moonhare_derives/src/lib.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
|
||||
use proc_macro::{self, TokenStream};
|
||||
|
||||
mod node;
|
||||
mod system;
|
||||
|
||||
#[proc_macro_derive(System)]
|
||||
pub fn system(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
system::system(input)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[proc_macro_derive(Node)]
|
||||
pub fn node(input: proc_macro::TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
TokenStream::from(node::node(input))
|
||||
}
|
||||
|
||||
// Add a bound `T: Node` to every type parameter T.
|
||||
fn add_trait_bounds(mut generics: Generics) -> Generics {
|
||||
for param in &mut generics.params {
|
||||
if let GenericParam::Type(ref mut type_param) = *param {
|
||||
type_param.bounds.push(parse_quote!(node::Node));
|
||||
}
|
||||
}
|
||||
generics
|
||||
}
|
||||
24
crates/moonhare_derives/src/node.rs
Normal file
24
crates/moonhare_derives/src/node.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use syn::DeriveInput;
|
||||
|
||||
use crate::add_trait_bounds;
|
||||
|
||||
pub fn node(input: DeriveInput) -> TokenStream2 {
|
||||
let name = input.ident;
|
||||
|
||||
let expanded = quote! {
|
||||
impl Node for #name {
|
||||
fn init(&mut self) {
|
||||
self.init();
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
self.update();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
expanded
|
||||
}
|
||||
31
crates/moonhare_derives/src/system.rs
Normal file
31
crates/moonhare_derives/src/system.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::DeriveInput;
|
||||
|
||||
use crate::add_trait_bounds;
|
||||
|
||||
|
||||
pub fn system(input: DeriveInput) -> TokenStream {
|
||||
let name = input.ident;
|
||||
|
||||
let generics = add_trait_bounds(input.generics);
|
||||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
||||
|
||||
let expanded = quote! {
|
||||
impl #name {
|
||||
fn init(&mut self) {
|
||||
|
||||
}
|
||||
|
||||
fn update(&mut self) {
|
||||
|
||||
}
|
||||
|
||||
fn add_child(&mut self, child: Self) {
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
proc_macro::TokenStream::from(expanded)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue