Skip to content

Commit f54c2ee

Browse files
committed
Implement ClearableCollection for all the hash based collections
1 parent d53266f commit f54c2ee

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

crates/bevy_ecs/src/system/scratch.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ use core::{
77
ops::{Deref, DerefMut},
88
};
99

10+
#[cfg(feature = "std")]
11+
use core::hash::{BuildHasher, Hash};
12+
13+
#[cfg(feature = "std")]
14+
use std::collections::{HashMap, HashSet};
15+
1016
use crate::{
17+
entity::{EntityHashMap, EntityHashSet},
1118
system::{ExclusiveSystemParam, ReadOnlySystemParam, SystemParam},
1219
world::FromWorld,
1320
};
@@ -229,6 +236,64 @@ impl<I> ClearableCollection for BinaryHeap<I> {
229236
}
230237
}
231238

239+
#[cfg(feature = "std")]
240+
impl<K: Hash + Eq, V, S: BuildHasher> ClearableCollection for HashMap<K, V, S> {
241+
fn capacity(&self) -> usize {
242+
HashMap::capacity(self)
243+
}
244+
245+
fn clear(&mut self) {
246+
HashMap::clear(self);
247+
}
248+
249+
fn shrink_to(&mut self, capacity: usize) {
250+
HashMap::shrink_to(self, capacity);
251+
}
252+
}
253+
254+
#[cfg(feature = "std")]
255+
impl<K: Hash + Eq, S: BuildHasher> ClearableCollection for HashSet<K, S> {
256+
fn capacity(&self) -> usize {
257+
HashSet::capacity(self)
258+
}
259+
260+
fn clear(&mut self) {
261+
HashSet::clear(self);
262+
}
263+
264+
fn shrink_to(&mut self, capacity: usize) {
265+
HashSet::shrink_to(self, capacity);
266+
}
267+
}
268+
269+
impl<V> ClearableCollection for EntityHashMap<V> {
270+
fn capacity(&self) -> usize {
271+
self.deref().capacity()
272+
}
273+
274+
fn clear(&mut self) {
275+
self.deref_mut().clear();
276+
}
277+
278+
fn shrink_to(&mut self, capacity: usize) {
279+
self.deref_mut().shrink_to(capacity);
280+
}
281+
}
282+
283+
impl ClearableCollection for EntityHashSet {
284+
fn capacity(&self) -> usize {
285+
self.deref().capacity()
286+
}
287+
288+
fn clear(&mut self) {
289+
self.deref_mut().clear();
290+
}
291+
292+
fn shrink_to(&mut self, capacity: usize) {
293+
self.deref_mut().shrink_to(capacity);
294+
}
295+
}
296+
232297
#[cfg(test)]
233298
mod tests {
234299
use alloc::vec::Vec;

0 commit comments

Comments
 (0)