Skip to content

Commit

Permalink
Enable cloning EntityHashMap and PreHashMap (#11178)
Browse files Browse the repository at this point in the history
# Objective

- `EntityHashMap`, `EntityHashSet` and `PreHashMap` are currently not
Cloneable because of a missing trivial `Clone` bound for `EntityHash`
and `PreHash`. This PR makes them Cloneable.

(the parent struct `hashbrown::HashMap` requires the `HashBuilder` to be
`Clone` for the `HashMap` to be `Clone`, see:
https://github.com/rust-lang/hashbrown/blob/master/src/map.rs#L195)


## Solution

- Add a `Clone` bound to `PreHash` and `EntityHash`

---------

Co-authored-by: Charles Bournhonesque <cbournhonesque@snapchat.com>
  • Loading branch information
cBournhonesque and cbournhonesque-sc committed Jan 2, 2024
1 parent 536a7bd commit ab10e85
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<V: Clone, H> Clone for Hashed<V, H> {
impl<V: Eq, H> Eq for Hashed<V, H> {}

/// A [`BuildHasher`] that results in a [`PassHasher`].
#[derive(Default)]
#[derive(Default, Clone)]
pub struct PassHash;

impl BuildHasher for PassHash {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K,
}

/// A [`BuildHasher`] that results in a [`EntityHasher`].
#[derive(Default)]
#[derive(Default, Clone)]
pub struct EntityHash;

impl BuildHasher for EntityHash {
Expand Down Expand Up @@ -415,3 +415,22 @@ macro_rules! detailed_trace {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_clone_entity_hash_map() {
let map = EntityHashMap::<u64, usize>::default();
// This should compile
let _ = map.clone();
}

#[test]
fn test_clone_pre_hash_map() {
let map = PreHashMap::<u64, usize>::default();
// This should compile
let _ = map.clone();
}
}

0 comments on commit ab10e85

Please sign in to comment.