diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index 9d66179df3..c3b78781d3 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -37,6 +37,29 @@ impl std::hash::BuildHasher for FixedState { /// AHash is designed for performance and is NOT cryptographically secure. pub type HashMap = std::collections::HashMap; +pub trait AHashExt { + fn with_capacity(capacity: usize) -> Self; +} + +impl AHashExt for HashMap { + /// Creates an empty `HashMap` with the specified capacity with AHash. + /// + /// The hash map will be able to hold at least `capacity` elements without + /// reallocating. If `capacity` is 0, the hash map will not allocate. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{HashMap, AHashExt}; + /// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[inline] + fn with_capacity(capacity: usize) -> Self { + HashMap::with_capacity_and_hasher(capacity, RandomState::default()) + } +} + /// A stable std hash map implementing AHash, a high speed keyed hashing algorithm /// intended for use in in-memory hashmaps. /// @@ -46,12 +69,50 @@ pub type HashMap = std::collections::HashMap; /// AHash is designed for performance and is NOT cryptographically secure. pub type StableHashMap = std::collections::HashMap; +impl AHashExt for StableHashMap { + /// Creates an empty `StableHashMap` with the specified capacity with AHash. + /// + /// The hash map will be able to hold at least `capacity` elements without + /// reallocating. If `capacity` is 0, the hash map will not allocate. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{StableHashMap, AHashExt}; + /// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10); + /// assert!(map.capacity() >= 10); + /// ``` + #[inline] + fn with_capacity(capacity: usize) -> Self { + StableHashMap::with_capacity_and_hasher(capacity, FixedState::default()) + } +} + /// A std hash set implementing AHash, a high speed keyed hashing algorithm /// intended for use in in-memory hashmaps. /// /// AHash is designed for performance and is NOT cryptographically secure. pub type HashSet = std::collections::HashSet; +impl AHashExt for HashSet { + /// Creates an empty `HashSet` with the specified capacity with AHash. + /// + /// The hash set will be able to hold at least `capacity` elements without + /// reallocating. If `capacity` is 0, the hash set will not allocate. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{HashSet, AHashExt}; + /// let set: HashSet = HashSet::with_capacity(10); + /// assert!(set.capacity() >= 10); + /// ``` + #[inline] + fn with_capacity(capacity: usize) -> Self { + HashSet::with_capacity_and_hasher(capacity, RandomState::default()) + } +} + /// A stable std hash set implementing AHash, a high speed keyed hashing algorithm /// intended for use in in-memory hashmaps. /// @@ -60,3 +121,22 @@ pub type HashSet = std::collections::HashSet; /// /// AHash is designed for performance and is NOT cryptographically secure. pub type StableHashSet = std::collections::HashSet; + +impl AHashExt for StableHashSet { + /// Creates an empty `StableHashSet` with the specified capacity with AHash. + /// + /// The hash set will be able to hold at least `capacity` elements without + /// reallocating. If `capacity` is 0, the hash set will not allocate. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{StableHashSet, AHashExt}; + /// let set: StableHashSet = StableHashSet::with_capacity(10); + /// assert!(set.capacity() >= 10); + /// ``` + #[inline] + fn with_capacity(capacity: usize) -> Self { + StableHashSet::with_capacity_and_hasher(capacity, FixedState::default()) + } +}