Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions crates/bevy_ecs/src/relationship/relationship_source_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@ pub trait RelationshipSourceCollection {
fn with_capacity(capacity: usize) -> Self;

/// Adds the given `entity` to the collection.
fn add(&mut self, entity: Entity);
///
/// Returns whether the entity was added to the collection.
/// Mainly useful when dealing with collections that don't allow
/// multiple instances of the same entity ([`EntityHashSet`]).
fn add(&mut self, entity: Entity) -> bool;

/// Removes the given `entity` from the collection.
fn remove(&mut self, entity: Entity);
///
/// Returns whether the collection actually contained
/// the entity.
fn remove(&mut self, entity: Entity) -> bool;

/// Iterates all entities in the collection.
fn iter(&self) -> Self::SourceIter<'_>;

/// Returns the current length of the collection.
fn len(&self) -> usize;

/// Clears the collection.
fn clear(&mut self);

/// Returns true if the collection contains no entities.
#[inline]
fn is_empty(&self) -> bool {
Expand All @@ -45,14 +55,20 @@ impl RelationshipSourceCollection for Vec<Entity> {
Vec::with_capacity(capacity)
}

fn add(&mut self, entity: Entity) {
fn add(&mut self, entity: Entity) -> bool {
Vec::push(self, entity);

true
}

fn remove(&mut self, entity: Entity) {
fn remove(&mut self, entity: Entity) -> bool {
if let Some(index) = <[Entity]>::iter(self).position(|e| *e == entity) {
Vec::remove(self, index);

return true;
}

false
}

fn iter(&self) -> Self::SourceIter<'_> {
Expand All @@ -62,6 +78,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
fn len(&self) -> usize {
Vec::len(self)
}

fn clear(&mut self) {
self.clear();
}
}

impl RelationshipSourceCollection for EntityHashSet {
Expand All @@ -71,14 +91,14 @@ impl RelationshipSourceCollection for EntityHashSet {
EntityHashSet::with_capacity(capacity)
}

fn add(&mut self, entity: Entity) {
self.insert(entity);
fn add(&mut self, entity: Entity) -> bool {
self.insert(entity)
}

fn remove(&mut self, entity: Entity) {
fn remove(&mut self, entity: Entity) -> bool {
// We need to call the remove method on the underlying hash set,
// which takes its argument by reference
self.0.remove(&entity);
self.0.remove(&entity)
}

fn iter(&self) -> Self::SourceIter<'_> {
Expand All @@ -88,6 +108,10 @@ impl RelationshipSourceCollection for EntityHashSet {
fn len(&self) -> usize {
self.len()
}

fn clear(&mut self) {
self.0.clear();
}
}

impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
Expand All @@ -97,14 +121,20 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
SmallVec::with_capacity(capacity)
}

fn add(&mut self, entity: Entity) {
fn add(&mut self, entity: Entity) -> bool {
SmallVec::push(self, entity);

true
}

fn remove(&mut self, entity: Entity) {
fn remove(&mut self, entity: Entity) -> bool {
if let Some(index) = <[Entity]>::iter(self).position(|e| *e == entity) {
SmallVec::remove(self, index);

return true;
}

false
}

fn iter(&self) -> Self::SourceIter<'_> {
Expand All @@ -114,6 +144,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
fn len(&self) -> usize {
SmallVec::len(self)
}

fn clear(&mut self) {
self.clear();
}
}

impl RelationshipSourceCollection for Entity {
Expand All @@ -123,14 +157,20 @@ impl RelationshipSourceCollection for Entity {
Entity::PLACEHOLDER
}

fn add(&mut self, entity: Entity) {
fn add(&mut self, entity: Entity) -> bool {
*self = entity;

true
}

fn remove(&mut self, entity: Entity) {
fn remove(&mut self, entity: Entity) -> bool {
if *self == entity {
*self = Entity::PLACEHOLDER;

return true;
}

false
}

fn iter(&self) -> Self::SourceIter<'_> {
Expand All @@ -143,6 +183,10 @@ impl RelationshipSourceCollection for Entity {
}
1
}

fn clear(&mut self) {
*self = Entity::PLACEHOLDER;
}
}

#[cfg(test)]
Expand Down