Skip to content

Commit

Permalink
Exposing ArchetypeComponents structs and implementing Default on them…
Browse files Browse the repository at this point in the history
… if all of the components can be Default
  • Loading branch information
recatek committed Jan 28, 2025
1 parent 7f27b03 commit acbce58
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
31 changes: 27 additions & 4 deletions macros/src/generate/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
.iter()
.map(|archetype| format_ident!("{}", archetype.name))
.collect::<Vec<_>>();
let ArchetypeComponents = world_data
.archetypes
.iter()
.map(|archetype| format_ident!("{}Components", archetype.name))
.collect::<Vec<_>>();
let ArchetypeDirect = world_data
.archetypes
.iter()
Expand Down Expand Up @@ -76,23 +81,29 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
let __expand_ecs_iter_destroy_hash = format_ident!("__expand_ecs_iter_destroy_{}", input_hash);

quote!(
#( pub use #ecs_world_sealed::#Archetype; )*

pub use #ecs_world_sealed::{
#World,
#WorldCapacity,

SelectArchetype,
SelectEntity,
SelectEntityDirect
SelectEntityDirect,

#(
#Archetype,
#ArchetypeComponents,
)*
};

#[doc(hidden)]
pub use #ecs_world_sealed::{#__WorldSelectTotal};

/// Convenience mod for accessing only archetypes in exports (for blob exports, etc.)
pub mod archetypes {
#(pub use super::#Archetype;)*
#(
pub use super::#Archetype;
pub use super::#ArchetypeComponents;
)*
}

mod #ecs_world_sealed {
Expand Down Expand Up @@ -1003,6 +1014,18 @@ fn section_archetype(archetype_data: &DataArchetype) -> TokenStream {
}
}

impl Default for #ArchetypeComponents
where
#(for<'a> #Component: Default,)*
{
#[inline(always)]
fn default() -> Self {
Self {
#(#component: #Component::default(),)*
}
}
}

/// Access to all of the stored entity and component data within this archetype.
/// Each index in these parallel slices refers to the components for a given entity.
/// Component access is mutable, but entity access is fixed (entities can't be moved).
Expand Down
23 changes: 20 additions & 3 deletions tests/test_clone.rs → tests/test_impls.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use gecs::prelude::*;

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Default, Debug, PartialEq)]
pub struct CompA(pub u32);
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Default, Debug, PartialEq)]
pub struct CompB(pub u32);
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Default, Debug, PartialEq)]
pub struct CompC(pub u32);

ecs_world! {
Expand All @@ -22,6 +22,23 @@ ecs_world! {
);
}

#[test]
pub fn test_default() {
let mut world = EcsWorld::default();

let entity_0 = world.create::<ArchFoo>(ArchFooComponents::default());
let entity_1 = world.create::<ArchBar>(ArchBarComponents {
comp_a: CompA(1),
..Default::default()
});

let components_0 = world.destroy(entity_0).unwrap();
let components_1 = world.destroy(entity_1).unwrap();

assert_eq!(components_0.comp_a.0, 0);
assert_eq!(components_1.comp_a.0, 1);
}

#[test]
#[rustfmt::skip]
pub fn test_multi_find_clone() {
Expand Down

0 comments on commit acbce58

Please sign in to comment.