Skip to content

Commit d9c713d

Browse files
committed
better example
1 parent a2d203a commit d9c713d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

examples/reflection/reflection.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,18 @@ fn setup(type_registry: Res<AppTypeRegistry>) {
5959
// You can also get the &dyn PartialReflect value of a field like this
6060
let field = value.field("a").unwrap();
6161

62-
// you can downcast Reflect values like this:
63-
assert_eq!(*field.try_downcast_ref::<usize>().unwrap(), 2);
62+
// For concrete-type based operations like downcasting, we need a `&dyn Reflect`,
63+
// which can be retrieved from a `&dyn PartialReflect` using `as_full`.
64+
// `as_full` returns an `Option<&dyn Reflect>` since some types like `DynamicStruct`
65+
// implement `PartialReflect` but not `Reflect`.
66+
// There are also `as_full_mut` and `into_full` methods for conversion
67+
// into `&mut dyn Reflect` and `Box<dyn Reflect>` respectively.
68+
let full_reflect: &dyn Reflect = field.as_full().unwrap();
6469

65-
// DynamicStruct also implements the `Struct` and `Reflect` traits.
70+
// You can downcast Reflect values like this:
71+
assert_eq!(*full_reflect.downcast_ref::<usize>().unwrap(), 2);
72+
73+
// DynamicStruct also implements the `Struct` and `PartialReflect` traits.
6674
let mut patch = DynamicStruct::default();
6775
patch.insert("a", 4usize);
6876

0 commit comments

Comments
 (0)