Fix memory bug caused by non-copy components (Vec, HashMap, etc.)
ci/woodpecker/push/debug Pipeline failed Details
ci/woodpecker/manual/debug Pipeline failed Details

This commit is contained in:
SeanOMik 2024-01-06 22:52:30 -05:00
parent ac24d1f913
commit 8f58096643
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
1 changed files with 15 additions and 8 deletions

View File

@ -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<C1: Component> Bundle for (C1,) {
impl<C: Component> Bundle for C {
fn type_ids(&self) -> Vec<DynTypeId> {
vec![DynTypeId::of::<C1>()]
vec![DynTypeId::of::<C>()]
}
fn info(&self) -> Vec<ComponentInfo> {
vec![ComponentInfo::new::<C1>()]
vec![ComponentInfo::new::<C>()]
}
fn take(self, mut f: impl FnMut(NonNull<u8>, DynTypeId, usize)) {
let (c1, ) = self;
f(NonNull::from(&self).cast(), DynTypeId::of::<C>(), size_of::<C>());
f(NonNull::from(&c1).cast(), DynTypeId::of::<C1>(), size_of::<C1>());
// 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<u8>, 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 }