pub trait Reflect: ReflectBase {
Show 29 methods
// Required methods
fn source_path() -> &'static str
where Self: Sized;
fn derived_types() -> &'static [TypeId]
where Self: Sized;
fn try_clone_box(&self) -> Option<Box<dyn Reflect>>;
fn query_derived_types(&self) -> &'static [TypeId];
fn type_name(&self) -> &'static str;
fn doc(&self) -> &'static str;
fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef<'_, '_>]));
fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut<'_, '_>]));
fn into_any(self: Box<Self>) -> Box<dyn Any>;
fn as_any(&self, func: &mut dyn FnMut(&(dyn Any + 'static)));
fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Any + 'static)));
fn as_reflect(&self, func: &mut dyn FnMut(&(dyn Reflect + 'static)));
fn as_reflect_mut(
&mut self,
func: &mut dyn FnMut(&mut (dyn Reflect + 'static)),
);
fn set(
&mut self,
value: Box<dyn Reflect>,
) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>;
fn assembly_name(&self) -> &'static str;
fn type_assembly_name() -> &'static str
where Self: Sized;
// Provided methods
fn set_field(
&mut self,
field_name: &str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>),
) { ... }
fn field(
&self,
name: &str,
func: &mut dyn FnMut(Option<&(dyn Reflect + 'static)>),
) { ... }
fn field_mut(
&mut self,
name: &str,
func: &mut dyn FnMut(Option<&mut (dyn Reflect + 'static)>),
) { ... }
fn as_array(
&self,
func: &mut dyn FnMut(Option<&(dyn ReflectArray + 'static)>),
) { ... }
fn as_array_mut(
&mut self,
func: &mut dyn FnMut(Option<&mut (dyn ReflectArray + 'static)>),
) { ... }
fn as_list(
&self,
func: &mut dyn FnMut(Option<&(dyn ReflectList + 'static)>),
) { ... }
fn as_list_mut(
&mut self,
func: &mut dyn FnMut(Option<&mut (dyn ReflectList + 'static)>),
) { ... }
fn as_inheritable_variable(
&self,
func: &mut dyn FnMut(Option<&(dyn ReflectInheritableVariable + 'static)>),
) { ... }
fn as_inheritable_variable_mut(
&mut self,
func: &mut dyn FnMut(Option<&mut (dyn ReflectInheritableVariable + 'static)>),
) { ... }
fn as_hash_map(
&self,
func: &mut dyn FnMut(Option<&(dyn ReflectHashMap + 'static)>),
) { ... }
fn as_hash_map_mut(
&mut self,
func: &mut dyn FnMut(Option<&mut (dyn ReflectHashMap + 'static)>),
) { ... }
fn as_handle(
&self,
func: &mut dyn FnMut(Option<&(dyn ReflectHandle + 'static)>),
) { ... }
fn as_handle_mut(
&mut self,
func: &mut dyn FnMut(Option<&mut (dyn ReflectHandle + 'static)>),
) { ... }
}Expand description
A trait for runtime reflection.
§Code Generation
The derive macro is available under #[reflect(...)] attribute that can be placed on both
the type and its fields.
§Type attributes
#[reflect(hide_all)]- hide all fields from reflection.#[reflect(bounds)]- add type boundary forReflectimpl, for example#[reflect(bounds = "T: Reflect + Clone")]#[reflect(non_cloneable)]- prevent the macro from generating an implementation ofSelf::try_clone_boxtrait for your type. Could be useful for non-cloneable types.#[reflect(derived_type = "Type")]- marks the type for which the attribute is added as a subtype for theType.
§Field attributes
#[reflect(hidden)]- hides the field from reflection.#[reflect(setter = "foo")]- set the desired method that will be used bySelf::set_fielddefault implementation.#[reflect(deref)]- delegate the field access withderef+deref_mutcalls. Could be useful for new-type objects.#[reflect(field = "foo")]- sets the desired method, that will be used to access the field.#[reflect(field_mut = "foo")]- sets the desired method, that will be used to access the field.#[reflect(name = "name")]- overrides the name of the field.#[reflect(display_name = "name")]- sets the human-readable name for the field.#[reflect(tag = "tag")]- sets some arbitrary string tag of the field. It could be used to group properties by a certain criteria or to find a specific property by its tag.#[reflect(read_only)]- the field is not meant to be editable. This flag does not prevent the reflection API from changing the actual value, it is just an instruction for external users (editors, tools, etc.)[#reflect(immutable_collection)]- only for dynamic collections (Vec, etc.) - means that its size cannot be changed, however the items of the collection can still be changed.#[reflect(min_value = "0.0")]- minimal value of the field. Works only for numeric fields!#[reflect(max_value = "1.0")]- maximal value of the field. Works only for numeric fields!#[reflect(step = "0.1")]- increment/decrement step of the field. Works only for numeric fields!#[reflect(precision = "3")]- maximum amount of decimal places for a numeric property.
§Clone
By default, the proc macro adds an implementation of Self::try_clone_box with the assumption
that your type implements the Clone trait. Not all types can implement this trait, in this
case, add #[reflect(non_cloneable)] attribute for your type. This will force the implementation
of Self::try_clone_box to return None.
§Additional Trait Bounds
Reflect restricted to types that implement Debug trait, this is needed to convert the actual value
to string. Display isn’t used here, because it can’t be derived and it is very tedious to implement it
for every type that should support Reflect trait. It is a good compromise between development speed
and the quality of the string output.
Required Methods§
fn source_path() -> &'static strwhere
Self: Sized,
fn derived_types() -> &'static [TypeId]where
Self: Sized,
fn try_clone_box(&self) -> Option<Box<dyn Reflect>>
fn query_derived_types(&self) -> &'static [TypeId]
fn type_name(&self) -> &'static str
fn doc(&self) -> &'static str
fn fields_ref(&self, func: &mut dyn FnMut(&[FieldRef<'_, '_>]))
fn fields_mut(&mut self, func: &mut dyn FnMut(&mut [FieldMut<'_, '_>]))
fn into_any(self: Box<Self>) -> Box<dyn Any>
fn as_any(&self, func: &mut dyn FnMut(&(dyn Any + 'static)))
fn as_any_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Any + 'static)))
fn as_reflect(&self, func: &mut dyn FnMut(&(dyn Reflect + 'static)))
fn as_reflect_mut(&mut self, func: &mut dyn FnMut(&mut (dyn Reflect + 'static)))
fn set( &mut self, value: Box<dyn Reflect>, ) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>
Sourcefn assembly_name(&self) -> &'static str
fn assembly_name(&self) -> &'static str
Returns a parent assembly name of the type that implements this trait. WARNING: You should use
proc-macro (#[derive(Reflect)]) to ensure that this method will return correct assembly
name. In other words - there’s no guarantee, that any implementation other than proc-macro
will return a correct name of the assembly. Alternatively, you can use env!("CARGO_PKG_NAME")
as an implementation.
Sourcefn type_assembly_name() -> &'static strwhere
Self: Sized,
fn type_assembly_name() -> &'static strwhere
Self: Sized,
Returns a parent assembly name of the type that implements this trait. WARNING: You should use
proc-macro (#[derive(Reflect)]) to ensure that this method will return correct assembly
name. In other words - there’s no guarantee, that any implementation other than proc-macro
will return a correct name of the assembly. Alternatively, you can use env!("CARGO_PKG_NAME")
as an implementation.
Provided Methods§
Sourcefn set_field(
&mut self,
field_name: &str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>),
)
fn set_field( &mut self, field_name: &str, value: Box<dyn Reflect>, func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldError>), )
Calls user method specified with #[reflect(setter = ..)] or falls back to
Reflect::field_mut
fn field( &self, name: &str, func: &mut dyn FnMut(Option<&(dyn Reflect + 'static)>), )
fn field_mut( &mut self, name: &str, func: &mut dyn FnMut(Option<&mut (dyn Reflect + 'static)>), )
fn as_array(&self, func: &mut dyn FnMut(Option<&(dyn ReflectArray + 'static)>))
fn as_array_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectArray + 'static)>), )
fn as_list(&self, func: &mut dyn FnMut(Option<&(dyn ReflectList + 'static)>))
fn as_list_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectList + 'static)>), )
fn as_inheritable_variable( &self, func: &mut dyn FnMut(Option<&(dyn ReflectInheritableVariable + 'static)>), )
fn as_inheritable_variable_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectInheritableVariable + 'static)>), )
fn as_hash_map( &self, func: &mut dyn FnMut(Option<&(dyn ReflectHashMap + 'static)>), )
fn as_hash_map_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectHashMap + 'static)>), )
fn as_handle( &self, func: &mut dyn FnMut(Option<&(dyn ReflectHandle + 'static)>), )
fn as_handle_mut( &mut self, func: &mut dyn FnMut(Option<&mut (dyn ReflectHandle + 'static)>), )
Implementations§
Source§impl dyn Reflect
Type-erased API
impl dyn Reflect
Type-erased API
pub fn downcast<T>(self: Box<dyn Reflect>) -> Result<Box<T>, Box<dyn Reflect>>where
T: Reflect,
pub fn take<T>(self: Box<dyn Reflect>) -> Result<T, Box<dyn Reflect>>where
T: Reflect,
pub fn is<T>(&self) -> boolwhere
T: Reflect,
pub fn downcast_ref<T>(&self, func: &mut dyn FnMut(Option<&T>))where
T: Reflect,
pub fn downcast_mut<T>(&mut self, func: &mut dyn FnMut(Option<&mut T>))where
T: Reflect,
Sourcepub fn set_field_by_path<'p>(
&mut self,
path: &'p str,
value: Box<dyn Reflect>,
func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldByPathError<'p>>),
)
pub fn set_field_by_path<'p>( &mut self, path: &'p str, value: Box<dyn Reflect>, func: &mut dyn FnMut(Result<Box<dyn Reflect>, SetFieldByPathError<'p>>), )
Sets a field by its path in the given entity. This method always uses Reflect::set_field which means,
that it will always call custom property setters.