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>
This commit is contained in:
parent
d28e4908ca
commit
c6d41a0d34
@ -6,9 +6,12 @@ use crate::{
|
||||
world::World,
|
||||
};
|
||||
|
||||
use alloc::{collections::VecDeque, vec::Vec};
|
||||
use alloc::{
|
||||
collections::{BTreeSet, VecDeque},
|
||||
vec::Vec,
|
||||
};
|
||||
use bevy_platform::collections::HashSet;
|
||||
use core::hash::BuildHasher;
|
||||
use core::{hash::BuildHasher, mem};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
/// 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> {
|
||||
*self = self.drain().map(|e| entity_mapper.get_mapped(e)).collect();
|
||||
}
|
||||
}
|
||||
|
||||
impl MapEntities for BTreeSet<Entity> {
|
||||
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
|
||||
*self = mem::take(self)
|
||||
.into_iter()
|
||||
.map(|e| entity_mapper.get_mapped(e))
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
impl MapEntities for Vec<Entity> {
|
||||
fn map_entities<E: EntityMapper>(&mut self, entity_mapper: &mut E) {
|
||||
for entity in self.iter_mut() {
|
||||
@ -95,6 +108,7 @@ impl<A: smallvec::Array<Item = Entity>> MapEntities for SmallVec<A> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An implementor of this trait knows how to map an [`Entity`] into another [`Entity`].
|
||||
///
|
||||
/// Usually this is done by using an [`EntityHashMap<Entity>`] to map source entities
|
||||
|
@ -1,3 +1,5 @@
|
||||
use alloc::collections::{btree_set, BTreeSet};
|
||||
|
||||
use crate::entity::{hash_set::EntityHashSet, Entity};
|
||||
use alloc::vec::Vec;
|
||||
use smallvec::SmallVec;
|
||||
@ -445,6 +447,47 @@ impl<const N: usize> OrderedRelationshipSourceCollection for SmallVec<[Entity; N
|
||||
}
|
||||
}
|
||||
|
||||
impl RelationshipSourceCollection for BTreeSet<Entity> {
|
||||
type SourceIter<'a> = core::iter::Copied<btree_set::Iter<'a, Entity>>;
|
||||
|
||||
fn new() -> Self {
|
||||
BTreeSet::new()
|
||||
}
|
||||
|
||||
fn with_capacity(_: usize) -> Self {
|
||||
// BTreeSet doesn't have a capacity
|
||||
Self::new()
|
||||
}
|
||||
|
||||
fn reserve(&mut self, _: usize) {
|
||||
// BTreeSet doesn't have a capacity
|
||||
}
|
||||
|
||||
fn add(&mut self, entity: Entity) -> bool {
|
||||
self.insert(entity)
|
||||
}
|
||||
|
||||
fn remove(&mut self, entity: Entity) -> bool {
|
||||
self.remove(&entity)
|
||||
}
|
||||
|
||||
fn iter(&self) -> Self::SourceIter<'_> {
|
||||
self.iter().copied()
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.len()
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.clear();
|
||||
}
|
||||
|
||||
fn shrink_to_fit(&mut self) {
|
||||
// BTreeSet doesn't have a capacity
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user