Implement optional component return

This commit is contained in:
hodasemi 2025-04-05 09:43:07 +02:00
parent 855c5ac59b
commit 0bccf5c447
3 changed files with 45 additions and 6 deletions

View file

@ -255,6 +255,17 @@ macro_rules! impl_get_components_mut {
crate::get_disjoint_mut::GetDisjointMut::<'a, ($(&'a mut $t,)+)>::get_mut(&mut self.components) crate::get_disjoint_mut::GetDisjointMut::<'a, ($(&'a mut $t,)+)>::get_mut(&mut self.components)
} }
} }
impl<'a, $($t,)+> EntityComponentDisjointMut<'a, ($(Option<&'a mut $t>,)+)> for EntityObject
where
$(
$t: EntityComponent + ComponentDebug + 'static,
)+
{
fn get_components_mut(&'a mut self) -> std::result::Result<($(Option<&'a mut $t>,)+), ComponentNotFoundError> {
crate::get_disjoint_mut::GetDisjointMut::<'a, ($(Option<&'a mut $t>,)+)>::get_mut(&mut self.components)
}
}
}; };
} }

View file

@ -121,9 +121,7 @@ where
type Error = Error; type Error = Error;
fn get_mut(&'a mut self) -> std::result::Result<&'a mut T, Self::Error> { fn get_mut(&'a mut self) -> std::result::Result<&'a mut T, Self::Error> {
self.map self.get_mut_opt()
.get_mut(&TypeId::of::<T>())
.map(|any| any.downcast_mut().unwrap())
.ok_or_else(|| anyhow!("failed downcasting {}", stringify!(T))) .ok_or_else(|| anyhow!("failed downcasting {}", stringify!(T)))
} }
} }
@ -146,7 +144,8 @@ macro_rules! impl_get_disjoint_mut {
types types
.pop_front() .pop_front()
.flatten() .flatten()
.map(|any| any.downcast_mut().unwrap()) .map(|any| any.downcast_mut())
.flatten()
.ok_or_else(|| anyhow!("failed downcasting {}", stringify!($t)))?, .ok_or_else(|| anyhow!("failed downcasting {}", stringify!($t)))?,
)+)) )+))
} }

View file

@ -160,7 +160,8 @@ where
fn get_mut(&'a mut self) -> std::result::Result<&'a mut T, Self::Error> { fn get_mut(&'a mut self) -> std::result::Result<&'a mut T, Self::Error> {
self.map self.map
.get_mut(&TypeId::of::<T>()) .get_mut(&TypeId::of::<T>())
.map(|any| any.downcast_mut().unwrap()) .map(|any| any.downcast_mut())
.flatten()
.ok_or_else(ComponentNotFoundError::component::<T>) .ok_or_else(ComponentNotFoundError::component::<T>)
} }
} }
@ -183,11 +184,39 @@ macro_rules! impl_get_disjoint_mut {
types types
.pop_front() .pop_front()
.flatten() .flatten()
.map(|any| any.downcast_mut().unwrap()) .map(|any| any.downcast_mut())
.flatten()
.ok_or_else(ComponentNotFoundError::component::<$t>)?, .ok_or_else(ComponentNotFoundError::component::<$t>)?,
)+)) )+))
} }
} }
impl<'a, $($t,)+> GetDisjointMut<'a, ($(Option<&'a mut $t>,)+)> for $struct
where
$(
$t: EntityComponent + ComponentDebug + 'static,
)+
{
type Error = $error;
fn get_mut(&'a mut self) -> std::result::Result<($(Option<&'a mut $t>,)+), Self::Error> {
let mut types: std::collections::VecDeque<_>
= self.map.get_disjoint_mut([$(&TypeId::of::<$t>(),)+]).into_iter().collect();
Ok(($(
{
// dummy needed for macro iteration
let _ = stringify!($t);
types
.pop_front()
.flatten()
.map(|any| any.downcast_mut())
.flatten()
},
)+))
}
}
}; };
} }