Skip to content

Commit 0197fe4

Browse files
committed
update docs, allow overriding all types
1 parent 3a7be5a commit 0197fe4

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

crates/bevy_mod_scripting_display/src/printer/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ impl<'f, 'b: 'f, 't> ReflectPrinter<'f, 'b, 't> {
2424

2525
/// Prints a `Reflect` value as if it was its native `Debug` implementation.
2626
pub fn debug(&mut self, value: &dyn PartialReflect) -> std::fmt::Result {
27+
if let Some(type_info_provider) = &self.type_info {
28+
if let Some(reflect_type) = value.try_as_reflect()
29+
&& let Some(display_type_data) = type_info_provider
30+
.get_type_data::<ReflectDisplayWithTypeInfo>(reflect_type.type_id())
31+
&& let Some(as_dyn_trait) = display_type_data.get(reflect_type)
32+
{
33+
return as_dyn_trait.display_with_type_info(self.formatter, self.type_info);
34+
}
35+
}
36+
2737
// try to print the value as if it was its native Debug implementation
2838
match value.reflect_ref() {
2939
ReflectRef::Struct(s) => self
@@ -154,20 +164,7 @@ impl<'f, 'b: 'f, 't> ReflectPrinter<'f, 'b, 't> {
154164
.finish()
155165
}
156166
}
157-
ReflectRef::Opaque(o) => {
158-
if let Some(type_info_provider) = &self.type_info {
159-
if let Some(reflect_type) = o.try_as_reflect()
160-
&& let Some(display_type_data) =
161-
type_info_provider
162-
.get_type_data::<ReflectDisplayWithTypeInfo>(reflect_type.type_id())
163-
&& let Some(as_dyn_trait) = display_type_data.get(reflect_type)
164-
{
165-
return as_dyn_trait.display_with_type_info(self.formatter, self.type_info);
166-
}
167-
}
168-
169-
o.debug(self.formatter)
170-
}
167+
ReflectRef::Opaque(o) => o.debug(self.formatter),
171168
}
172169
}
173170

docs/src/Summary/controlling-script-bindings.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ There are a few reserved functions that you can override by registering them on
153153
| eq | an equality function, used for checking if two values are equal |||
154154
| lt | a less than function, used for checking if a value is less than another |||
155155
| iter | an iterator function, used for iterating over a value |||
156-
| display | a display function, used for displaying a reference to a value |||
157-
| debug | a display function, used for displaying a mutable reference to a value |||
156+
| display | a display function, used for pretty printing values |\* ||
157+
| debug | a display function, used for displaying the internals of a value |\* ||
158+
159+
\* - You can register an instance of `ReflectDisplayWithTypeInfo` type data on `Reflect` implementing types to override their printing behavior
158160

159161
In this context `overridable` indicates whether language implementations will look for a specific function on your type before looking at the generic `ReflectReference` namespace. You can still remove the existing registration for these functions on the `ReflectReference` namespace if you want to replace them with your own implementation.
160162

0 commit comments

Comments
 (0)