Skip to content

Commit c6d41a0

Browse files
Brezakchescockmockersf
authored
Implement RelationshipSourceCollection for BTreeSet (#18469)
# Objective `BTreeSet` doesn't implement `RelationshipSourceCollection`. ## Solution Implement it. ## Testing `cargo clippy` --- ## Showcase You can now use `BTreeSet` in a `RelationshipTarget` --------- Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com> Co-authored-by: François Mockers <mockersf@gmail.com>
1 parent d28e490 commit c6d41a0

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

crates/bevy_ecs/src/entity/map_entities.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ use crate::{
66
world::World,
77
};
88

9-
use alloc::{collections::VecDeque, vec::Vec};
9+
use alloc::{
10+
collections::{BTreeSet, VecDeque},
11+
vec::Vec,
12+
};
1013
use bevy_platform::collections::HashSet;
11-
use core::hash::BuildHasher;
14+
use core::{hash::BuildHasher, mem};
1215
use smallvec::SmallVec;
1316

1417
/// Operation to map all contained [`Entity`] fields in a type to new values.
@@ -72,6 +75,16 @@ impl<S: BuildHasher + Default> MapEntities for HashSet<Entity, S> {
7275
*self = self.drain().map(|e| entity_mapper.get_mapped(e)).collect();
7376
}
7477
}
78+
79+
impl MapEntities for BTreeSet<Entity> {
80+
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
81+
*self = mem::take(self)
82+
.into_iter()
83+
.map(|e| entity_mapper.get_mapped(e))
84+
.collect();
85+
}
86+
}
87+
7588
impl MapEntities for Vec<Entity> {
7689
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
7790
for entity in self.iter_mut() {
@@ -95,6 +108,7 @@ impl<A: smallvec::Array<Item = Entity>> MapEntities for SmallVec<A> {
95108
}
96109
}
97110
}
111+
98112
/// An implementor of this trait knows how to map an [`Entity`] into another [`Entity`].
99113
///
100114
/// Usually this is done by using an [`EntityHashMap<Entity>`] to map source entities

crates/bevy_ecs/src/relationship/relationship_source_collection.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::collections::{btree_set, BTreeSet};
2+
13
use crate::entity::{hash_set::EntityHashSet, Entity};
24
use alloc::vec::Vec;
35
use smallvec::SmallVec;
@@ -445,6 +447,47 @@ impl<const N: usize> OrderedRelationshipSourceCollection for SmallVec<[Entity; N
445447
}
446448
}
447449

450+
impl RelationshipSourceCollection for BTreeSet<Entity> {
451+
type SourceIter<'a> = core::iter::Copied<btree_set::Iter<'a, Entity>>;
452+
453+
fn new() -> Self {
454+
BTreeSet::new()
455+
}
456+
457+
fn with_capacity(_: usize) -> Self {
458+
// BTreeSet doesn't have a capacity
459+
Self::new()
460+
}
461+
462+
fn reserve(&mut self, _: usize) {
463+
// BTreeSet doesn't have a capacity
464+
}
465+
466+
fn add(&mut self, entity: Entity) -> bool {
467+
self.insert(entity)
468+
}
469+
470+
fn remove(&mut self, entity: Entity) -> bool {
471+
self.remove(&entity)
472+
}
473+
474+
fn iter(&self) -> Self::SourceIter<'_> {
475+
self.iter().copied()
476+
}
477+
478+
fn len(&self) -> usize {
479+
self.len()
480+
}
481+
482+
fn clear(&mut self) {
483+
self.clear();
484+
}
485+
486+
fn shrink_to_fit(&mut self) {
487+
// BTreeSet doesn't have a capacity
488+
}
489+
}
490+
448491
#[cfg(test)]
449492
mod tests {
450493
use super::*;

0 commit comments

Comments
 (0)