diff --git a/lyra-ecs/src/bundle.rs b/lyra-ecs/src/bundle.rs index bdbc659..98be308 100644 --- a/lyra-ecs/src/bundle.rs +++ b/lyra-ecs/src/bundle.rs @@ -17,20 +17,21 @@ pub trait Bundle { fn is_dynamic(&self) -> bool; } -// The macro below can implement this for us, but this is here for development -impl Bundle for (C1,) { +impl Bundle for C { fn type_ids(&self) -> Vec { - vec![DynTypeId::of::()] + vec![DynTypeId::of::()] } fn info(&self) -> Vec { - vec![ComponentInfo::new::()] + vec![ComponentInfo::new::()] } fn take(self, mut f: impl FnMut(NonNull, DynTypeId, usize)) { - let (c1, ) = self; + f(NonNull::from(&self).cast(), DynTypeId::of::(), size_of::()); - f(NonNull::from(&c1).cast(), DynTypeId::of::(), size_of::()); + // this must be done to avoid calling drop on heap memory that the component + // may manage. So something like a Vec, or HashMap, etc. + std::mem::forget(self); } fn is_dynamic(&self) -> bool { @@ -53,9 +54,14 @@ macro_rules! impl_bundle_tuple { fn take(self, mut f: impl FnMut(NonNull, DynTypeId, usize)) { // these names wont follow rust convention, but its a macro so deal with it - let ($($name),+) = self; + let ($($name,)+) = self; - $(f(NonNull::from(&$name).cast(), DynTypeId::of::<$name>(), size_of::<$name>());)+ + $( + f(NonNull::from(&$name).cast(), DynTypeId::of::<$name>(), size_of::<$name>()); + // this must be done to avoid calling drop on heap memory that the component + // may manage. So something like a Vec, or HashMap, etc. + std::mem::forget($name); + )+ } fn is_dynamic(&self) -> bool { @@ -66,6 +72,7 @@ macro_rules! impl_bundle_tuple { } // hopefully 16 components in a bundle is enough +impl_bundle_tuple! { C1 } impl_bundle_tuple! { C1, C2 } impl_bundle_tuple! { C1, C2, C3 } impl_bundle_tuple! { C1, C2, C3, C4 }