Skip to content

Commit e082720

Browse files
committed
Implement ClearableCollection for all the hash based collections
1 parent e8467a9 commit e082720

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
};
@@ -227,6 +234,64 @@ impl<I> ClearableCollection for BinaryHeap<I> {
227234
}
228235
}
229236

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

0 commit comments

Comments
 (0)