From 02755086e857d271e836663b8729af678b9def5d Mon Sep 17 00:00:00 2001 From: Charles Bournhonesque Date: Tue, 2 Jan 2024 17:08:30 -0500 Subject: [PATCH] Add static assertions to bevy_utils for compile-time checks (#11182) # 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 https://github.com/bevyengine/bevy/pull/11178 is merged) --------- Co-authored-by: Charles Bournhonesque --- crates/bevy_utils/Cargo.toml | 3 +++ crates/bevy_utils/src/lib.rs | 17 ++++------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/crates/bevy_utils/Cargo.toml b/crates/bevy_utils/Cargo.toml index a946b76fc925e..bffdce617cc8a 100644 --- a/crates/bevy_utils/Cargo.toml +++ b/crates/bevy_utils/Cargo.toml @@ -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"] } diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index 19d72ba57b245..aeef0b8918090 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -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::::default(); - // This should compile - let _ = map.clone(); - } - - #[test] - fn test_clone_pre_hash_map() { - let map = PreHashMap::::default(); - // This should compile - let _ = map.clone(); - } + // Check that the HashMaps are Clone if the key/values are Clone + assert_impl_all!(EntityHashMap::: Clone); + assert_impl_all!(PreHashMap::: Clone); }