summaryrefslogtreecommitdiff
path: root/ecs/src/relationship.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/relationship.rs')
-rw-r--r--ecs/src/relationship.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs
index 1a8c842..0fe776e 100644
--- a/ecs/src/relationship.rs
+++ b/ecs/src/relationship.rs
@@ -26,6 +26,7 @@ where
ComponentT: Component,
{
/// Creates a new `Relationship` with a single target.
+ #[must_use]
pub fn new(entity_uid: EntityUid) -> Self
{
Self {
@@ -35,6 +36,7 @@ where
}
/// Creates a new `Relationship` with multiple targets.
+ #[must_use]
pub fn new_multiple(entity_uids: impl IntoIterator<Item = EntityUid>) -> Self
{
Self {
@@ -132,6 +134,11 @@ where
ComponentT: Component,
{
/// Returns the component of the target at the specified index.
+ ///
+ /// # Panics
+ /// Will panic if the entity does not exist in the archetype it belongs to. This
+ /// should hopefully never happend.
+ #[must_use]
pub fn get(&self, index: usize) -> Option<ComponentRefMut<'_, ComponentT>>
{
let target = self.get_target(index)?;
@@ -162,22 +169,24 @@ where
}
/// Returns a reference to the target at the specified index.
+ #[must_use]
pub fn get_target(&self, index: usize) -> Option<&EntityUid>
{
match &self.relationship_comp.entity_uid {
SingleOrMultiple::Single(entity_uid) if index == 0 => Some(entity_uid),
SingleOrMultiple::Multiple(entity_uids) => entity_uids.get(index),
- _ => None,
+ SingleOrMultiple::Single(_) => None,
}
}
/// Returns a mutable reference to the target at the specified index.
+ #[must_use]
pub fn get_target_mut(&mut self, index: usize) -> Option<&mut EntityUid>
{
match &mut self.relationship_comp.entity_uid {
SingleOrMultiple::Single(entity_uid) if index == 0 => Some(entity_uid),
SingleOrMultiple::Multiple(entity_uids) => entity_uids.get_mut(index),
- _ => None,
+ SingleOrMultiple::Single(_) => None,
}
}
@@ -215,6 +224,7 @@ where
}
}
+ #[must_use]
pub fn target_count(&self) -> usize
{
match &self.relationship_comp.entity_uid {
@@ -224,12 +234,28 @@ where
}
/// Returns a iterator of the components of the targets of this relationship.
+ #[must_use]
pub fn iter(&self) -> TargetComponentIter<'_, 'rel_comp, Kind, ComponentT>
{
TargetComponentIter { relation: self, index: 0 }
}
}
+impl<'relationship, 'rel_comp, Kind, ComponentT> IntoIterator
+ for &'relationship Relation<'rel_comp, Kind, ComponentT>
+where
+ 'relationship: 'rel_comp,
+ ComponentT: Component,
+{
+ type IntoIter = TargetComponentIter<'relationship, 'rel_comp, Kind, ComponentT>;
+ type Item = ComponentRefMut<'rel_comp, ComponentT>;
+
+ fn into_iter(self) -> Self::IntoIter
+ {
+ self.iter()
+ }
+}
+
/// Iterator of the components of the targets of a relationship.
pub struct TargetComponentIter<'relationship, 'rel_comp, Kind, ComponentT>
where