@@ -20,17 +20,27 @@ pub trait RelationshipSourceCollection {
20
20
fn with_capacity ( capacity : usize ) -> Self ;
21
21
22
22
/// 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 ;
24
28
25
29
/// 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 ;
27
34
28
35
/// Iterates all entities in the collection.
29
36
fn iter ( & self ) -> Self :: SourceIter < ' _ > ;
30
37
31
38
/// Returns the current length of the collection.
32
39
fn len ( & self ) -> usize ;
33
40
41
+ /// Clears the collection.
42
+ fn clear ( & mut self ) ;
43
+
34
44
/// Returns true if the collection contains no entities.
35
45
#[ inline]
36
46
fn is_empty ( & self ) -> bool {
@@ -45,14 +55,20 @@ impl RelationshipSourceCollection for Vec<Entity> {
45
55
Vec :: with_capacity ( capacity)
46
56
}
47
57
48
- fn add ( & mut self , entity : Entity ) {
58
+ fn add ( & mut self , entity : Entity ) -> bool {
49
59
Vec :: push ( self , entity) ;
60
+
61
+ true
50
62
}
51
63
52
- fn remove ( & mut self , entity : Entity ) {
64
+ fn remove ( & mut self , entity : Entity ) -> bool {
53
65
if let Some ( index) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
54
66
Vec :: remove ( self , index) ;
67
+
68
+ return true ;
55
69
}
70
+
71
+ false
56
72
}
57
73
58
74
fn iter ( & self ) -> Self :: SourceIter < ' _ > {
@@ -62,6 +78,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
62
78
fn len ( & self ) -> usize {
63
79
Vec :: len ( self )
64
80
}
81
+
82
+ fn clear ( & mut self ) {
83
+ self . clear ( ) ;
84
+ }
65
85
}
66
86
67
87
impl RelationshipSourceCollection for EntityHashSet {
@@ -71,14 +91,14 @@ impl RelationshipSourceCollection for EntityHashSet {
71
91
EntityHashSet :: with_capacity ( capacity)
72
92
}
73
93
74
- fn add ( & mut self , entity : Entity ) {
75
- self . insert ( entity) ;
94
+ fn add ( & mut self , entity : Entity ) -> bool {
95
+ self . insert ( entity)
76
96
}
77
97
78
- fn remove ( & mut self , entity : Entity ) {
98
+ fn remove ( & mut self , entity : Entity ) -> bool {
79
99
// We need to call the remove method on the underlying hash set,
80
100
// which takes its argument by reference
81
- self . 0 . remove ( & entity) ;
101
+ self . 0 . remove ( & entity)
82
102
}
83
103
84
104
fn iter ( & self ) -> Self :: SourceIter < ' _ > {
@@ -88,6 +108,10 @@ impl RelationshipSourceCollection for EntityHashSet {
88
108
fn len ( & self ) -> usize {
89
109
self . len ( )
90
110
}
111
+
112
+ fn clear ( & mut self ) {
113
+ self . 0 . clear ( ) ;
114
+ }
91
115
}
92
116
93
117
impl < const N : usize > RelationshipSourceCollection for SmallVec < [ Entity ; N ] > {
@@ -97,14 +121,20 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
97
121
SmallVec :: with_capacity ( capacity)
98
122
}
99
123
100
- fn add ( & mut self , entity : Entity ) {
124
+ fn add ( & mut self , entity : Entity ) -> bool {
101
125
SmallVec :: push ( self , entity) ;
126
+
127
+ true
102
128
}
103
129
104
- fn remove ( & mut self , entity : Entity ) {
130
+ fn remove ( & mut self , entity : Entity ) -> bool {
105
131
if let Some ( index) = <[ Entity ] >:: iter ( self ) . position ( |e| * e == entity) {
106
132
SmallVec :: remove ( self , index) ;
133
+
134
+ return true ;
107
135
}
136
+
137
+ false
108
138
}
109
139
110
140
fn iter ( & self ) -> Self :: SourceIter < ' _ > {
@@ -114,6 +144,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
114
144
fn len ( & self ) -> usize {
115
145
SmallVec :: len ( self )
116
146
}
147
+
148
+ fn clear ( & mut self ) {
149
+ self . clear ( ) ;
150
+ }
117
151
}
118
152
119
153
impl RelationshipSourceCollection for Entity {
@@ -123,14 +157,20 @@ impl RelationshipSourceCollection for Entity {
123
157
Entity :: PLACEHOLDER
124
158
}
125
159
126
- fn add ( & mut self , entity : Entity ) {
160
+ fn add ( & mut self , entity : Entity ) -> bool {
127
161
* self = entity;
162
+
163
+ true
128
164
}
129
165
130
- fn remove ( & mut self , entity : Entity ) {
166
+ fn remove ( & mut self , entity : Entity ) -> bool {
131
167
if * self == entity {
132
168
* self = Entity :: PLACEHOLDER ;
169
+
170
+ return true ;
133
171
}
172
+
173
+ false
134
174
}
135
175
136
176
fn iter ( & self ) -> Self :: SourceIter < ' _ > {
@@ -143,6 +183,10 @@ impl RelationshipSourceCollection for Entity {
143
183
}
144
184
1
145
185
}
186
+
187
+ fn clear ( & mut self ) {
188
+ * self = Entity :: PLACEHOLDER ;
189
+ }
146
190
}
147
191
148
192
#[ cfg( test) ]
0 commit comments