Skip to content

Commit 906d788

Browse files
authored
Expand the RelationshipSourceCollection to return more information (#18241)
# Objective While redoing #18058 I needed `RelationshipSourceCollection` (henceforth referred to as the **Trait**) to implement `clear` so I added it. ## Solution Add the `clear` method to the **Trait**. Also make `add` and `remove` report if they succeeded. ## Testing Eyeballs --- ## Showcase The `RelationshipSourceCollection` trait now reports if adding or removing an entity from it was successful. It also not contains the `clear` method so you can easily clear the collection in generic contexts. ## Changes EDITED by Alice: We should get this into 0.16, so no migration guide needed. The `RelationshipSourceCollection` methods `add` and `remove` now need to return a boolean indicating if they were successful (adding a entity to a set that already contains it counts as failure). Additionally the `clear` method has been added to support clearing the collection in generic contexts.
1 parent ed529a7 commit 906d788

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ pub trait RelationshipSourceCollection {
2020
fn with_capacity(capacity: usize) -> Self;
2121

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

2529
/// Removes the given `entity` from the collection.
26-
fn remove(&mut self, entity: Entity);
30+
///
31+
/// Returns whether the collection actually contained
32+
/// the entity.
33+
fn remove(&mut self, entity: Entity) -> bool;
2734

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

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

41+
/// Clears the collection.
42+
fn clear(&mut self);
43+
3444
/// Returns true if the collection contains no entities.
3545
#[inline]
3646
fn is_empty(&self) -> bool {
@@ -45,14 +55,20 @@ impl RelationshipSourceCollection for Vec<Entity> {
4555
Vec::with_capacity(capacity)
4656
}
4757

48-
fn add(&mut self, entity: Entity) {
58+
fn add(&mut self, entity: Entity) -> bool {
4959
Vec::push(self, entity);
60+
61+
true
5062
}
5163

52-
fn remove(&mut self, entity: Entity) {
64+
fn remove(&mut self, entity: Entity) -> bool {
5365
if let Some(index) = <[Entity]>::iter(self).position(|e| *e == entity) {
5466
Vec::remove(self, index);
67+
68+
return true;
5569
}
70+
71+
false
5672
}
5773

5874
fn iter(&self) -> Self::SourceIter<'_> {
@@ -62,6 +78,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
6278
fn len(&self) -> usize {
6379
Vec::len(self)
6480
}
81+
82+
fn clear(&mut self) {
83+
self.clear();
84+
}
6585
}
6686

6787
impl RelationshipSourceCollection for EntityHashSet {
@@ -71,14 +91,14 @@ impl RelationshipSourceCollection for EntityHashSet {
7191
EntityHashSet::with_capacity(capacity)
7292
}
7393

74-
fn add(&mut self, entity: Entity) {
75-
self.insert(entity);
94+
fn add(&mut self, entity: Entity) -> bool {
95+
self.insert(entity)
7696
}
7797

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

84104
fn iter(&self) -> Self::SourceIter<'_> {
@@ -88,6 +108,10 @@ impl RelationshipSourceCollection for EntityHashSet {
88108
fn len(&self) -> usize {
89109
self.len()
90110
}
111+
112+
fn clear(&mut self) {
113+
self.0.clear();
114+
}
91115
}
92116

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

100-
fn add(&mut self, entity: Entity) {
124+
fn add(&mut self, entity: Entity) -> bool {
101125
SmallVec::push(self, entity);
126+
127+
true
102128
}
103129

104-
fn remove(&mut self, entity: Entity) {
130+
fn remove(&mut self, entity: Entity) -> bool {
105131
if let Some(index) = <[Entity]>::iter(self).position(|e| *e == entity) {
106132
SmallVec::remove(self, index);
133+
134+
return true;
107135
}
136+
137+
false
108138
}
109139

110140
fn iter(&self) -> Self::SourceIter<'_> {
@@ -114,6 +144,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
114144
fn len(&self) -> usize {
115145
SmallVec::len(self)
116146
}
147+
148+
fn clear(&mut self) {
149+
self.clear();
150+
}
117151
}
118152

119153
impl RelationshipSourceCollection for Entity {
@@ -123,14 +157,20 @@ impl RelationshipSourceCollection for Entity {
123157
Entity::PLACEHOLDER
124158
}
125159

126-
fn add(&mut self, entity: Entity) {
160+
fn add(&mut self, entity: Entity) -> bool {
127161
*self = entity;
162+
163+
true
128164
}
129165

130-
fn remove(&mut self, entity: Entity) {
166+
fn remove(&mut self, entity: Entity) -> bool {
131167
if *self == entity {
132168
*self = Entity::PLACEHOLDER;
169+
170+
return true;
133171
}
172+
173+
false
134174
}
135175

136176
fn iter(&self) -> Self::SourceIter<'_> {
@@ -143,6 +183,10 @@ impl RelationshipSourceCollection for Entity {
143183
}
144184
1
145185
}
186+
187+
fn clear(&mut self) {
188+
*self = Entity::PLACEHOLDER;
189+
}
146190
}
147191

148192
#[cfg(test)]

0 commit comments

Comments
 (0)