mirror of
https://github.com/lisk77/comet.git
synced 2025-12-12 17:18:50 +00:00
chore(all): fix warnings
This commit is contained in:
parent
c7f0412eff
commit
81bc1cb790
14 changed files with 471 additions and 513 deletions
|
|
@ -2,16 +2,12 @@ extern crate proc_macro;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, DeriveInput, Data, Fields};
|
||||
use syn::{parse_macro_input, Data, DeriveInput, Fields};
|
||||
|
||||
// This is the procedural macro for `derive(MyTrait)`
|
||||
#[proc_macro_derive(Component)]
|
||||
pub fn my_trait_derive(input: TokenStream) -> TokenStream {
|
||||
// Parse the input tokens into a syntax tree (AST)
|
||||
pub fn component_derive(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
// Get the name of the struct
|
||||
let name = &input.ident;
|
||||
let name = &input.ident;
|
||||
|
||||
let fields = if let Data::Struct(data) = &input.data {
|
||||
|
|
@ -33,19 +29,21 @@ pub fn my_trait_derive(input: TokenStream) -> TokenStream {
|
|||
|
||||
let default_fields = if let Data::Struct(data) = &input.data {
|
||||
match &data.fields {
|
||||
Fields::Named(fields) => {
|
||||
// Generate code to assign each field a default value
|
||||
fields.named.iter().map(|field| {
|
||||
Fields::Named(fields) => fields
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| {
|
||||
let field_name = &field.ident;
|
||||
quote! { #field_name: Default::default() }
|
||||
}).collect::<Vec<_>>()
|
||||
},
|
||||
Fields::Unnamed(fields) => {
|
||||
// Generate default values for tuple structs
|
||||
fields.unnamed.iter().map(|_field| {
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
Fields::Unnamed(fields) => fields
|
||||
.unnamed
|
||||
.iter()
|
||||
.map(|_field| {
|
||||
quote! { Default::default() }
|
||||
}).collect::<Vec<_>>()
|
||||
},
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
Fields::Unit => Vec::new(),
|
||||
}
|
||||
} else {
|
||||
|
|
@ -59,13 +57,6 @@ pub fn my_trait_derive(input: TokenStream) -> TokenStream {
|
|||
}
|
||||
});
|
||||
|
||||
for field in &fields {
|
||||
if !is_copy_field(&field) {
|
||||
panic!("All fields in the struct must implement Copy");
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the implementation of MyTrait for the given struct
|
||||
let expanded = quote! {
|
||||
impl Component for #name {
|
||||
fn new() -> Self {
|
||||
|
|
@ -83,7 +74,6 @@ pub fn my_trait_derive(input: TokenStream) -> TokenStream {
|
|||
|
||||
impl Default for #name {
|
||||
fn default() -> Self {
|
||||
// Construct the struct with default values
|
||||
Self {
|
||||
#(#default_fields),*
|
||||
}
|
||||
|
|
@ -115,16 +105,5 @@ pub fn my_trait_derive(input: TokenStream) -> TokenStream {
|
|||
}
|
||||
};
|
||||
|
||||
// Convert the generated code into a TokenStream and return it
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
|
||||
|
||||
fn is_copy_field(field: &syn::Field) -> bool {
|
||||
// Logic to check if the field type implements Copy (this is simplified)
|
||||
// You might need more sophisticated logic to check the actual type of the field.
|
||||
let field_type = &field.ty;
|
||||
// Implement a check for Copy trait for the field type if needed
|
||||
// Return true if it implements Copy; false otherwise
|
||||
true // Assuming it does, just for simplicity
|
||||
}
|
||||
|
|
@ -1,59 +1,47 @@
|
|||
use std::collections::{HashMap, HashSet};
|
||||
use comet_structs::ComponentSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Archetypes {
|
||||
archetypes: HashMap<ComponentSet, HashSet<u32>>
|
||||
archetypes: HashMap<ComponentSet, HashSet<u32>>,
|
||||
}
|
||||
|
||||
impl Archetypes {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
archetypes: HashMap::new()
|
||||
}
|
||||
}
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
archetypes: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component_sets(&self) -> Vec<ComponentSet> {
|
||||
self.archetypes.keys().cloned().collect()
|
||||
}
|
||||
pub fn component_sets(&self) -> Vec<ComponentSet> {
|
||||
self.archetypes.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn create_archetype(&mut self, components: ComponentSet) {
|
||||
self.archetypes.insert(components, HashSet::new());
|
||||
}
|
||||
pub fn create_archetype(&mut self, components: ComponentSet) {
|
||||
self.archetypes.insert(components, HashSet::new());
|
||||
}
|
||||
|
||||
pub fn get_archetype(&self, components: &ComponentSet) -> Option<&HashSet<u32>> {
|
||||
self.archetypes.get(components)
|
||||
}
|
||||
pub fn get_archetype(&self, components: &ComponentSet) -> Option<&HashSet<u32>> {
|
||||
self.archetypes.get(components)
|
||||
}
|
||||
|
||||
pub fn get_archetype_mut(&mut self, components: &ComponentSet) -> Option<&mut HashSet<u32>> {
|
||||
self.archetypes.get_mut(components)
|
||||
}
|
||||
pub fn add_entity_to_archetype(&mut self, components: &ComponentSet, entity: u32) {
|
||||
if let Some(archetype) = self.archetypes.get_mut(components) {
|
||||
archetype.insert(entity);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_entity_to_archetype(&mut self, components: &ComponentSet, entity: u32) {
|
||||
if let Some(archetype) = self.archetypes.get_mut(components) {
|
||||
archetype.insert(entity);
|
||||
}
|
||||
}
|
||||
pub fn remove_entity_from_archetype(&mut self, components: &ComponentSet, entity: u32) {
|
||||
if let Some(archetype) = self.archetypes.get_mut(components) {
|
||||
archetype.retain(|&id| id != entity);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_entity_from_archetype(&mut self, components: &ComponentSet, entity: u32) {
|
||||
if let Some(archetype) = self.archetypes.get_mut(components) {
|
||||
archetype.retain(|&id| id != entity);
|
||||
}
|
||||
}
|
||||
pub fn remove_archetype(&mut self, components: &ComponentSet) {
|
||||
self.archetypes.remove(components);
|
||||
}
|
||||
|
||||
pub fn remove_archetype(&mut self, components: &ComponentSet) {
|
||||
self.archetypes.remove(components);
|
||||
}
|
||||
|
||||
pub fn contains_archetype(&self, components: &ComponentSet) -> bool {
|
||||
self.archetypes.contains_key(components)
|
||||
}
|
||||
|
||||
pub fn archetype_contains_entity(&self, entity_id: u32, components: &ComponentSet) -> bool {
|
||||
if self.contains_archetype(components) {
|
||||
let archetype = self.get_archetype(components).unwrap();
|
||||
return archetype.contains(&entity_id);
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
pub fn contains_archetype(&self, components: &ComponentSet) -> bool {
|
||||
self.archetypes.contains_key(components)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue