Skip to content

Commit

Permalink
Add static assertions to bevy_utils for compile-time checks (#11182)
Browse files Browse the repository at this point in the history
# Objective

- We want to use `static_assertions` to perform precise compile time
checks at testing time. In this PR, we add those checks to make sure
that `EntityHashMap` and `PreHashMap` are `Clone` (and we replace the
more clumsy previous tests)
- Fixes #11181 

(will need to be rebased once
#11178 is merged)

---------

Co-authored-by: Charles Bournhonesque <cbournhonesque@snapchat.com>
  • Loading branch information
cBournhonesque and cbournhonesque-sc authored Jan 2, 2024
1 parent dd14f3a commit 0275508
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ thiserror = "1.0"
nonmax = "0.5"
smallvec = { version = "1.11", features = ["serde", "union", "const_generics"] }

[dev-dependencies]
static_assertions = "1.1.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.0", features = ["js"] }

Expand Down
17 changes: 4 additions & 13 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,9 @@ macro_rules! detailed_trace {
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::assert_impl_all;

#[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();
}
// Check that the HashMaps are Clone if the key/values are Clone
assert_impl_all!(EntityHashMap::<u64, usize>: Clone);
assert_impl_all!(PreHashMap::<u64, usize>: Clone);
}

0 comments on commit 0275508

Please sign in to comment.