Skip to content

Commit 056b006

Browse files
authored
Use static_assertions to check for trait impls (#11407)
# Objective - Tests are manually checking whether derived types implement certain traits. (Specifically in `bevy_reflect.) - #11182 introduces [`static_assertions`](https://docs.rs/static_assertions/) to automatically check this. - Simplifies `Reflect` test in #11195. - Closes #11196. ## Solution - Add `static_assertions` and replace current tests. --- I wasn't sure whether to remove the existing test or not. What do you think?
1 parent d151883 commit 056b006

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

crates/bevy_reflect/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ rmp-serde = "1.1"
4141
bincode = "1.3"
4242
serde_json = "1.0"
4343
serde = { version = "1", features = ["derive"] }
44+
static_assertions = "1.1.0"
4445

4546
[[example]]
4647
name = "reflect_docs"

crates/bevy_reflect/src/impls/std.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,9 +1516,13 @@ mod tests {
15161516
};
15171517
use bevy_utils::{Duration, Instant};
15181518
use bevy_utils::{EntityHashMap, HashMap};
1519+
use static_assertions::assert_impl_all;
15191520
use std::f32::consts::{PI, TAU};
15201521
use std::path::Path;
15211522

1523+
// EntityHashMap should implement Reflect
1524+
assert_impl_all!(EntityHashMap<i32, i32>: Reflect);
1525+
15221526
#[test]
15231527
fn can_serialize_duration() {
15241528
let mut type_registry = TypeRegistry::default();
@@ -1599,6 +1603,8 @@ mod tests {
15991603

16001604
#[test]
16011605
fn option_should_impl_enum() {
1606+
assert_impl_all!(Option<()>: Enum);
1607+
16021608
let mut value = Some(123usize);
16031609

16041610
assert!(value
@@ -1672,6 +1678,8 @@ mod tests {
16721678

16731679
#[test]
16741680
fn option_should_impl_typed() {
1681+
assert_impl_all!(Option<()>: Typed);
1682+
16751683
type MyOption = Option<i32>;
16761684
let info = MyOption::type_info();
16771685
if let TypeInfo::Enum(info) = info {
@@ -1702,6 +1710,7 @@ mod tests {
17021710
panic!("Expected `TypeInfo::Enum`");
17031711
}
17041712
}
1713+
17051714
#[test]
17061715
fn nonzero_usize_impl_reflect_from_reflect() {
17071716
let a: &dyn Reflect = &std::num::NonZeroUsize::new(42).unwrap();
@@ -1724,12 +1733,4 @@ mod tests {
17241733
let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
17251734
assert_eq!(path, output);
17261735
}
1727-
1728-
#[test]
1729-
fn entity_hashmap_should_impl_reflect() {
1730-
let entity_map = bevy_utils::EntityHashMap::<i32, i32>::default();
1731-
let entity_map_type_info =
1732-
<EntityHashMap<_, _> as Reflect>::get_represented_type_info(&entity_map);
1733-
assert!(entity_map_type_info.is_some());
1734-
}
17351736
}

0 commit comments

Comments
 (0)