Skip to content

Commit 83c5e6e

Browse files
committed
Added convenience methods to TypeInfo
1 parent 23e276a commit 83c5e6e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/bevy_reflect/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ mod tests {
431431

432432
#[test]
433433
fn reflect_type_info() {
434+
// TypeInfo
435+
let info = i32::type_info();
436+
assert_eq!(std::any::type_name::<i32>(), info.type_name());
437+
assert_eq!(std::any::TypeId::of::<i32>(), info.type_id());
438+
434439
// Struct
435440
#[derive(Reflect)]
436441
struct MyStruct {

crates/bevy_reflect/src/type_info.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{ListInfo, MapInfo, Reflect, StructInfo, TupleInfo, TupleStructInfo};
22
use std::any::TypeId;
33
use std::borrow::{Borrow, Cow};
44

5-
/// Compile-time type information for various object types
5+
/// Compile-time type information for various reflected types
66
#[derive(Debug, Clone)]
77
pub enum TypeInfo {
88
Struct(StructInfo),
@@ -17,6 +17,34 @@ pub enum TypeInfo {
1717
Dynamic(DynamicInfo),
1818
}
1919

20+
impl TypeInfo {
21+
/// The name of the reflected type
22+
pub fn type_name(&self) -> &str {
23+
match self {
24+
Self::Struct(info) => info.type_name(),
25+
Self::TupleStruct(info) => info.type_name(),
26+
Self::Tuple(info) => info.type_name(),
27+
Self::List(info) => info.type_name(),
28+
Self::Map(info) => info.type_name(),
29+
Self::Value(info) => info.type_name(),
30+
Self::Dynamic(info) => info.type_name(),
31+
}
32+
}
33+
34+
/// The `TypeId` of the reflected type
35+
pub fn type_id(&self) -> TypeId {
36+
match self {
37+
Self::Struct(info) => info.type_id(),
38+
Self::TupleStruct(info) => info.type_id(),
39+
Self::Tuple(info) => info.type_id(),
40+
Self::List(info) => info.type_id(),
41+
Self::Map(info) => info.type_id(),
42+
Self::Value(info) => info.type_id(),
43+
Self::Dynamic(info) => info.type_id(),
44+
}
45+
}
46+
}
47+
2048
/// A container for compile-time info related to general value types, including primitives
2149
#[derive(Debug, Clone)]
2250
pub struct ValueInfo {

0 commit comments

Comments
 (0)