@@ -46,6 +46,17 @@ pub trait RelationshipSourceCollection {
4646 fn is_empty ( & self ) -> bool {
4747 self . len ( ) == 0
4848 }
49+
50+ /// Add multiple entities to collection at once.
51+ ///
52+ /// May be faster than repeatedly calling [`Self::add`].
53+ fn extend_from_iter ( & mut self , entities : impl IntoIterator < Item = Entity > ) {
54+ // The method name shouldn't conflict with `Extend::extend` as it's in the rust prelude and
55+ // would always conflict with it.
56+ for entity in entities {
57+ self . add ( entity) ;
58+ }
59+ }
4960}
5061
5162impl RelationshipSourceCollection for Vec < Entity > {
@@ -82,6 +93,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
8293 fn clear ( & mut self ) {
8394 self . clear ( ) ;
8495 }
96+
97+ fn extend_from_iter ( & mut self , entities : impl IntoIterator < Item = Entity > ) {
98+ self . extend ( entities) ;
99+ }
85100}
86101
87102impl RelationshipSourceCollection for EntityHashSet {
@@ -112,6 +127,10 @@ impl RelationshipSourceCollection for EntityHashSet {
112127 fn clear ( & mut self ) {
113128 self . 0 . clear ( ) ;
114129 }
130+
131+ fn extend_from_iter ( & mut self , entities : impl IntoIterator < Item = Entity > ) {
132+ self . extend ( entities) ;
133+ }
115134}
116135
117136impl < const N : usize > RelationshipSourceCollection for SmallVec < [ Entity ; N ] > {
@@ -148,6 +167,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
148167 fn clear ( & mut self ) {
149168 self . clear ( ) ;
150169 }
170+
171+ fn extend_from_iter ( & mut self , entities : impl IntoIterator < Item = Entity > ) {
172+ self . extend ( entities) ;
173+ }
151174}
152175
153176impl RelationshipSourceCollection for Entity {
@@ -187,6 +210,12 @@ impl RelationshipSourceCollection for Entity {
187210 fn clear ( & mut self ) {
188211 * self = Entity :: PLACEHOLDER ;
189212 }
213+
214+ fn extend_from_iter ( & mut self , entities : impl IntoIterator < Item = Entity > ) {
215+ if let Some ( entity) = entities. into_iter ( ) . last ( ) {
216+ * self = entity;
217+ }
218+ }
190219}
191220
192221#[ cfg( test) ]
0 commit comments