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?
This commit is contained in:
BD103 2024-01-18 12:21:18 -05:00 committed by GitHub
parent d151883f3e
commit 056b006d4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View file

@ -41,6 +41,7 @@ rmp-serde = "1.1"
bincode = "1.3"
serde_json = "1.0"
serde = { version = "1", features = ["derive"] }
static_assertions = "1.1.0"
[[example]]
name = "reflect_docs"

View file

@ -1516,9 +1516,13 @@ mod tests {
};
use bevy_utils::{Duration, Instant};
use bevy_utils::{EntityHashMap, HashMap};
use static_assertions::assert_impl_all;
use std::f32::consts::{PI, TAU};
use std::path::Path;
// EntityHashMap should implement Reflect
assert_impl_all!(EntityHashMap<i32, i32>: Reflect);
#[test]
fn can_serialize_duration() {
let mut type_registry = TypeRegistry::default();
@ -1599,6 +1603,8 @@ mod tests {
#[test]
fn option_should_impl_enum() {
assert_impl_all!(Option<()>: Enum);
let mut value = Some(123usize);
assert!(value
@ -1672,6 +1678,8 @@ mod tests {
#[test]
fn option_should_impl_typed() {
assert_impl_all!(Option<()>: Typed);
type MyOption = Option<i32>;
let info = MyOption::type_info();
if let TypeInfo::Enum(info) = info {
@ -1702,6 +1710,7 @@ mod tests {
panic!("Expected `TypeInfo::Enum`");
}
}
#[test]
fn nonzero_usize_impl_reflect_from_reflect() {
let a: &dyn Reflect = &std::num::NonZeroUsize::new(42).unwrap();
@ -1724,12 +1733,4 @@ mod tests {
let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
assert_eq!(path, output);
}
#[test]
fn entity_hashmap_should_impl_reflect() {
let entity_map = bevy_utils::EntityHashMap::<i32, i32>::default();
let entity_map_type_info =
<EntityHashMap<_, _> as Reflect>::get_represented_type_info(&entity_map);
assert!(entity_map_type_info.is_some());
}
}