diff --git a/crates/bevy_input/src/axis.rs b/crates/bevy_input/src/axis.rs index e1863969e8..56a2b35c0b 100644 --- a/crates/bevy_input/src/axis.rs +++ b/crates/bevy_input/src/axis.rs @@ -1,4 +1,5 @@ -use std::{collections::HashMap, hash::Hash}; +use bevy_utils::{AHashExt, HashMap}; +use std::hash::Hash; #[derive(Debug)] pub struct Axis { diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index ca594002e7..76492cb010 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -9,15 +9,89 @@ pub type BoxedFuture<'a, T> = Pin + Send + 'a>>; #[cfg(target_arch = "wasm32")] pub type BoxedFuture<'a, T> = Pin + 'a>>; +/// A std hash map 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 HashMap = std::collections::HashMap; -pub type HashSet = std::collections::HashSet; -pub trait HashMapExt { - fn with_capacity(cap: usize) -> Self; +pub trait AHashExt { + fn new() -> Self; + + fn with_capacity(capacity: usize) -> Self; } -impl HashMapExt for HashMap { - fn with_capacity(cap: usize) -> Self { - HashMap::with_capacity_and_hasher(cap, RandomState::default()) +impl AHashExt for HashMap { + /// Creates an empty `HashMap` with AHash. + /// + /// The hash map is initially created with a capacity of 0, so it will not + /// allocate until it is first inserted into. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{HashMap, AHashExt}; + /// let mut map: HashMap<&str, i32> = HashMap::new(); + /// ``` + #[inline] + fn new() -> Self { + Default::default() + } + + /// 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); + /// ``` + #[inline] + fn with_capacity(capacity: usize) -> Self { + HashMap::with_capacity_and_hasher(capacity, RandomState::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 AHash. + /// + /// The hash set is initially created with a capacity of 0, so it will not + /// allocate until it is first inserted into. + /// + /// # Examples + /// + /// ``` + /// use bevy_utils::{HashSet, AHashExt}; + /// let set: HashSet = HashSet::new(); + /// ``` + #[inline] + fn new() -> Self { + Default::default() + } + + /// 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()) } }