# core/reflection (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/reflection/

Reflection. (in place of the runtime reflection of the original vanilla.js: collectReflectedProperties and getReflectedProperties of ui/uidocument.js,
  getComponentProperties of misc/devtools.js)
- The original is JavaScript, so at runtime it scans an object's prototype to find get, is, set functions. Rust has no runtime reflection,
  so like Unreal's UHT (UPROPERTY) and Qt's moc, it generates code at compile time to do the same. In Rust the procedural macro `#[reflect]` does that.
  (the macros/ crate, re-exported by the engine as vanilla::reflect) The place of C++ RTTI typeid, dynamic_cast is already AsAny (std::any::Any).
- This module holds the value type (ReflectValue) and trait (Reflect) used by macro-generated code, and the common properties of the original Component base class.
- Usage: put `#[vanilla::reflect]` on both the component's impl block and its impl Component block.
  ```text
    #[vanilla::reflect]
    impl Spin {
        pub fn get_speed(&self) -> f32 { self.speed }
        pub fn set_speed(&mut self, speed: f32) { self.speed = speed; }
    }
    #[vanilla::reflect]
    impl Component for Spin { ... }
  ```
  Then UIDocument writes and reads with the Speed key without registered write and read functions (the original saveByReflection, loadByReflection),
  and the DevTools inspector shows a speed line. (original getComponentProperties)
- In the original every object is reflected. The Rust edition makes Reflect a supertrait of Component, so a component without reflection cannot exist.
  Leaving out either one gives a compile error that points at where to add it. A type without getters takes it on an empty block, `#[vanilla::reflect] impl Spin {}`.
  The side that uses reflection upcasts `&dyn Component` to `&dyn Reflect` (trait upcasting) and calls it.
- Follows the original rules:
  - Getters are argument-less pub get_*, is_* functions, and saved properties are getters with a set_* of the same name. The key is the original name with the prefix removed. (get_line_width → LineWidth)
  - The order is the definition order and for the same key the first one wins. GizmoVisible, Enable, ComponentType of the original Component base class are appended at the end.
  - Saved values are the types the original saveByReflection picks (number, boolean, text, Color, Vector2, node). Enums that are text in the original (such as TextAlign) are converted to the same text.
- What it does like the original:
  - Getters of the original superclass come along through the prototype chain. The Rust version has no inheritance, so it links the embedded value with #[reflect(base = field)].
    Functions that belong to the superclass in the original but that the Rust version redefines for convenience are excluded with `#[reflect(skip)]` to keep the original order.
    Reading and setting look up separately starting from the most derived type, like the original `component[getterName]`, `component[setterName]`. (RichText gets the getter from Text and has its own setter)
  - Pairs are made by key (baseName) like the original. (if the key of get_target is set to TargetNode, it is not paired with set_target (key Target))
  - Getters redefined in the impl Component block show only those also in the original class (excluding the rest with `#[reflect(skip)]`), and their place in the original class body is matched with #[reflect(after = "...")].
  - Properties left out of saving in the original because their value is an object are kept in the list but left out of saving with `#[reflect(unsaved)]`. (FontFace of ShadowText)
  - When reading, the type is decided by the setter's type. The original decides by the type of the current value, and takes the saved type if there is no current value (null). (the original saveByReflection writes only Color as four numbers,
    and only Vector2 as two numbers) So both engines read the same document the same way.
- What differs from the original:
  - In the original every object is reflected. In the Rust version only types with `#[reflect]` are. Rust has no way to read a method list at runtime, so there is truly no alternative.
    (the same form as Unreal UCLASS, UPROPERTY) Omissions can be found this way: putting it on only one block makes a compile error point to where to put it,
    and omitting both makes UIDocument warn when writing and the DevTools inspector show a "no reflection" row.
  - &mut self getters are called through the branch taking &mut (get_reflected_value_mut, collect_reflected_getters_mut). In the &self branch the value is None.
    Getters that take the tree (such as get_target_button(tree), the original holds the neighboring component directly) are called by the side holding the tree with collect_reflected_getters_in_tree.
    Setters that take the tree (such as set_snap_current_index(tree, index)) are called by the side holding the tree with set_reflected_value_in_tree.
    Setters that take more arguments than the value (such as set_image(image, slice)) are in the property list (paired if a set function exists, like the original) but are not set.
    In place of the original setComponentType, Rust has no setter (the kind is decided by the type), so it is only written. In the original too, reading this key does not change the kind.

Example: Putting the reflection macro on the impl block and the impl Component block, then reading and setting properties

```rust
use vanilla::core::reflection::{collect_reflected_properties, read_reflected_property, write_reflected_property};
use vanilla::{reflect, Component, ComponentBase, ReflectValue};

struct Spin {
	component_base: ComponentBase,
	speed: f32,
}

#[reflect]
impl Spin {
	pub fn get_speed(&self) -> f32 {
		self.speed
	}

	pub fn set_speed(&mut self, speed: f32) {
		self.speed = speed;
	}
}

#[reflect]
impl Component for Spin {
	fn get_component_base(&self) -> &ComponentBase {
		&self.component_base
	}

	fn get_component_base_mut(&mut self) -> &mut ComponentBase {
		&mut self.component_base
	}

	fn get_component_type(&self) -> &'static str {
		"Spin"
	}
}

let mut spin = Spin {
	component_base: ComponentBase::new(),
	speed: 1.5,
};
let property_names: Vec<&str> = collect_reflected_properties(&spin).iter().map(|property| property.name).collect();
assert_eq!(property_names, ["Speed", "GizmoVisible", "Enable", "ComponentType"]);
assert_eq!(read_reflected_property(&spin, "Speed"), Some(ReflectValue::Number(1.5)));
assert!(write_reflected_property(&mut spin, "Speed", &ReflectValue::Number(3.0)));
assert_eq!(spin.get_speed(), 3.0);
```

Example: Function options (name, transient, skip) and the embedded value (base)

```rust
use vanilla::core::reflection::{collect_reflected_getters, collect_reflected_properties};
use vanilla::{reflect, Component, ComponentBase};

struct Meter {
	component_base: ComponentBase,
	value: f32,
}

#[reflect]
impl Meter {
	pub fn get_value(&self) -> f32 {
		self.value
	}

	pub fn set_value(&mut self, value: f32) {
		self.value = value;
	}
}

#[reflect]
impl Component for Meter {
	fn get_component_base(&self) -> &ComponentBase {
		&self.component_base
	}

	fn get_component_base_mut(&mut self) -> &mut ComponentBase {
		&mut self.component_base
	}

	fn get_component_type(&self) -> &'static str {
		"Meter"
	}
}

struct LabeledMeter {
	component_base: ComponentBase,
	meter: Meter,
	label: String,
}

#[reflect(base = meter)]
impl LabeledMeter {
	#[reflect(name = "Caption")]
	pub fn get_label(&self) -> &str {
		&self.label
	}

	#[reflect(name = "Caption")]
	pub fn set_label(&mut self, label: &str) {
		self.label = String::from(label);
	}

	#[reflect(transient)]
	pub fn get_label_length(&self) -> usize {
		self.label.len()
	}

	#[reflect(skip)]
	pub fn get_meter(&self) -> &Meter {
		&self.meter
	}
}

#[reflect]
impl Component for LabeledMeter {
	fn get_component_base(&self) -> &ComponentBase {
		&self.component_base
	}

	fn get_component_base_mut(&mut self) -> &mut ComponentBase {
		&mut self.component_base
	}

	fn get_component_type(&self) -> &'static str {
		"LabeledMeter"
	}
}

let labeled_meter = LabeledMeter {
	component_base: ComponentBase::new(),
	meter: Meter {
		component_base: ComponentBase::new(),
		value: 0.5,
	},
	label: String::from("hp"),
};
let property_names: Vec<&str> = collect_reflected_properties(&labeled_meter).iter().map(|property| property.name).collect();
assert_eq!(property_names, ["Caption", "Value", "GizmoVisible", "Enable", "ComponentType"]);
let getter_names: Vec<&str> = collect_reflected_getters(&labeled_meter).iter().map(|getter| getter.name).collect();
assert_eq!(getter_names, ["getCaption", "getLabelLength", "getValue"]);
```

## ToReflectValue (trait)

Type convertible to a reflection value.

Example: Basic types to reflection values

```rust
use vanilla::{Color, ReflectValue, ToReflectValue, Vector2};

assert_eq!(0.25f32.to_reflect_value(), ReflectValue::Number(0.25));
assert_eq!("left".to_reflect_value(), ReflectValue::String(String::from("left")));
assert_eq!(Some(Vector2::create(1.0, 2.0)).to_reflect_value(), ReflectValue::Vector2(Vector2::create(1.0, 2.0)));
let empty_color: Option<Color> = None;
assert_eq!(empty_color.to_reflect_value(), ReflectValue::Null);
```

- `fn to_reflect_value(&self) -> ReflectValue` (method, since 0.4.0): Converts to a reflection value.
  - returns ReflectValue: Reflection value of this value.

## FromReflectValue (trait) (Sized)

Type readable from a reflection value. (None if the type does not match, in place of the typeof equality check of the original loadByReflection)

Example: Reading an array read from a document according to the type

```rust
use vanilla::{Color, FromReflectValue, ReflectValue, Vector2};

let list_value = ReflectValue::List(vec![ReflectValue::Number(1.0), ReflectValue::Number(2.0)]);
assert_eq!(Vector2::from_reflect_value(&list_value), Some(Vector2::create(1.0, 2.0)));
assert_eq!(Color::from_reflect_value(&list_value), None);
assert_eq!(Option::<f32>::from_reflect_value(&ReflectValue::Null), Some(None));
assert_eq!(f32::from_reflect_value(&ReflectValue::Boolean(true)), None);
```

- `fn from_reflect_value(value: &'a ReflectValue) -> Option<Self>` (static, since 0.4.0): Reads from a reflection value.
  - `value` &'a ReflectValue: Reflection value to read.
  - returns Option<Self>: The read value. None if the type does not match.

## ReflectedProperty (struct) (Clone, Copy, Debug, PartialEq, Eq)

One saved property. ({ name, getterName, setterName } returned by the original getReflectedProperties)
- name is the original key (the original name with the prefix removed), getter_name and setter_name are Rust function names. If setter_name is None, Rust has no setter.
- If is_saved is false, it is a property that reflection saving does not write because its value is an object in the original. (`#[reflect(unsaved)]`, FontFace of ShadowText)

Example: Looking at one common Component property

```rust
use vanilla::core::reflection::COMPONENT_REFLECTED_PROPERTIES;

let enable_property = COMPONENT_REFLECTED_PROPERTIES[1];
assert_eq!(enable_property.name, "Enable");
assert_eq!(enable_property.getter_name, "is_enable");
assert_eq!(enable_property.setter_name, Some("set_enable"));
assert!(enable_property.is_saved);
```

- `pub name: &'static str` (field, since 0.4.0): Original key. (the original name with the prefix removed, LineWidth)
  - returns &'static str
- `pub getter_name: &'static str` (field, since 0.4.0): Rust getter name. (get_line_width)
  - returns &'static str
- `pub setter_name: Option<&'static str>` (field, since 0.4.0): Rust setter name. None if Rust has no setter.
  - returns Option<&'static str>
- `pub is_saved: bool` (field, since 0.4.0): true if it is a property reflection saving writes. (false if `#[reflect(unsaved)]`)
  - returns bool

## ReflectedGetter (struct) (Clone, Debug, PartialEq)

One getter and its value. ({ name, value } returned by the original getComponentProperties, name is the original function name getLineWidth, isCircle)

Example: Adding to the getter list (for the same name only the first one stays)

```rust
use vanilla::core::reflection::push_reflected_getter;
use vanilla::{ReflectedGetter, ReflectValue};

let mut getter_list: Vec<ReflectedGetter> = Vec::new();
push_reflected_getter(&mut getter_list, "getSpeed", ReflectValue::Number(2.0));
push_reflected_getter(&mut getter_list, "getSpeed", ReflectValue::Number(9.0));
assert_eq!(getter_list.len(), 1);
assert_eq!(getter_list[0].name, "getSpeed");
assert_eq!(getter_list[0].value, ReflectValue::Number(2.0));
```

- `pub name: &'static str` (field, since 0.4.0): Original function name. (getLineWidth, isCircle)
  - returns &'static str
- `pub value: ReflectValue` (field, since 0.4.0): Value the getter returned.
  - returns ReflectValue

## Reflect (trait)

Reflection implementation. (generated by the macro from an impl block with `#[reflect]`)
- Do not implement it directly. Putting the reflection macro on the type's impl block and impl Component block makes the macro generate it.

Example: Calling the macro-generated reflection implementation directly

```rust
use vanilla::{reflect, Component, ComponentBase, Reflect, ReflectValue};

struct Gauge {
	component_base: ComponentBase,
	ratio: f32,
}

#[reflect]
impl Gauge {
	pub fn get_ratio(&self) -> f32 {
		self.ratio
	}

	pub fn set_ratio(&mut self, ratio: f32) {
		self.ratio = ratio;
	}
}

#[reflect]
impl Component for Gauge {
}

let mut gauge = Gauge {
	component_base: ComponentBase::new(),
	ratio: 0.5,
};
assert_eq!(gauge.get_reflected_type_name(), "Gauge");
assert_eq!(gauge.get_reflected_properties()[0].name, "Ratio");
assert!(gauge.set_reflected_value("Ratio", &ReflectValue::Number(0.75)));
assert_eq!(gauge.get_reflected_value("Ratio"), Some(ReflectValue::Number(0.75)));
assert!(!gauge.set_reflected_value("Ratio", &ReflectValue::Boolean(true)));
assert_eq!(gauge.get_reflected_value("Missing"), None);
```

- `fn get_reflected_type_name(&self) -> &'static str` (method, since 0.4.0): Type name. (in place of the original constructor.name, the type used when writing an unregistered component to a document)
  - returns &'static str: Name of the impl target type. (the last name of the path)
- `fn get_reflected_properties(&self) -> Vec<ReflectedProperty>` (method, since 0.4.0): Saved property list. (paired getters and setters, definition order. Common Component properties are not included)
  - returns Vec<ReflectedProperty>: Saved property list. (keys missing here from the embedded value (base) are appended)
- `fn get_reflected_value(&self, property_name: &str) -> Option<ReflectValue>` (method, since 0.4.0): Reads a property value. (original key, None for unknown keys or &mut self getters)
  - `property_name` &str: Original key. (a name with the prefix removed, like Speed)
  - returns Option<ReflectValue>: Property value. None for unknown keys, &mut self getters, and getters taking the tree.
- `fn get_reflected_value_mut(&mut self, property_name: &str) -> Option<ReflectValue>` (method, since 0.4.0): Reads a property value. (including &mut self getters)
  - `property_name` &str: Original key.
  - returns Option<ReflectValue>: Property value. None for unknown keys or getters taking the tree.
- `fn set_reflected_value(&mut self, property_name: &str, value: &ReflectValue) -> bool` (method, since 0.4.0): Sets a property value. (false if the type does not match, the key is unknown, or the setter takes the tree or cannot be called with a single value)
  - `property_name` &str: Original key.
  - `value` &ReflectValue: Value to set. (converted to the setter's type)
  - returns bool: true if set.
- `fn set_reflected_value_in_tree(&mut self, node_tree: &mut NodeTree, property_name: &str, value: &ReflectValue) -> bool` (method, since 0.4.0): Sets a property value. (including setters taking the tree. Called with this component taken out of the tree)
  - `node_tree` &mut NodeTree: Tree to pass to setters taking the tree.
  - `property_name` &str: Original key.
  - `value` &ReflectValue: Value to set. (converted to the setter's type)
  - returns bool: true if set. false if the type does not match, the key is unknown, or the setter cannot be called with a single value.
- `fn has_mutable_getters(&self) -> bool` (method, since 0.4.0): Whether there are &mut self getters. (if so, the inspector and saving read through the &mut branch)
  - returns bool: true if there is at least one &mut self getter. (also checks the embedded value)
- `fn collect_reflected_getters(&self, getter_list: &mut Vec<ReflectedGetter>)` (method, since 0.4.0): Collects getter values. (for the inspector, all getters including those without setters. Those redefined in the impl Component block → impl block order. &mut self getters are excluded)
  - `getter_list` &mut Vec<ReflectedGetter>: List to add getters to. (not added if the same name already exists)
- `fn collect_reflected_getters_in_tree(&self, node_tree: &NodeTree, getter_list: &mut Vec<ReflectedGetter>)` (method, since 0.4.0): Collects getter values. (including getters taking the tree, &mut self getters excluded. Getters that hold a neighboring component directly in the original take the tree in the Rust version)
  - `node_tree` &NodeTree: Tree to pass to getters taking the tree.
  - `getter_list` &mut Vec<ReflectedGetter>: List to add getters to. (not added if the same name already exists)
- `fn collect_reflected_getters_mut(&mut self, node_tree: &NodeTree, getter_list: &mut Vec<ReflectedGetter>)` (method, since 0.4.0): Collects getter values. (including &mut self getters and getters taking the tree, definition order. Called with this component taken out of the tree)
  - `node_tree` &NodeTree: Tree to pass to getters taking the tree.
  - `getter_list` &mut Vec<ReflectedGetter>: List to add getters to. (not added if the same name already exists)

## ReflectComponentGetters (trait)

Collects getters redefined in the impl Component block. (generated by the macro from an impl Component block with `#[reflect]`)
- Things like isTickNeeded, isDrawnInsideContent that subclasses override in the original. The original inspector shows them as functions of that class.
- Do not implement it directly. Putting the reflection macro on the impl Component block makes the macro generate it.

Example: Getters redefined in the impl Component block come before the getters of the impl block

```rust
use vanilla::core::reflection::{collect_reflected_getters, ReflectComponentGetters};
use vanilla::{reflect, Component, ComponentBase, ReflectedGetter, ReflectValue};

struct Label {
	component_base: ComponentBase,
}

#[reflect]
impl Label {
	pub fn get_text(&self) -> &str {
		"hello"
	}
}

#[reflect]
impl Component for Label {
	fn is_tick_needed(&self) -> bool {
		false
	}
}

let label = Label {
	component_base: ComponentBase::new(),
};
let mut getter_list: Vec<ReflectedGetter> = Vec::new();
let mut anchored_getter_list = Vec::new();
label.collect_component_getters(&mut getter_list, &mut anchored_getter_list);
assert_eq!(getter_list[0].name, "isTickNeeded");
assert_eq!(getter_list[0].value, ReflectValue::Boolean(false));
let all_getter_list = collect_reflected_getters(&label);
let getter_names: Vec<&str> = all_getter_list.iter().map(|getter| getter.name).collect();
assert_eq!(getter_names, ["isTickNeeded", "getText"]);
```

- `fn collect_component_getters(&self, getter_list: &mut Vec<ReflectedGetter>, anchored_getter_list: &mut Vec<(&'static str, ReflectedGetter)>)` (method, since 0.4.0): Collects getters. (those with a set place go into anchored_getter_list as (preceding original getter name, getter), the rest go to the front of getter_list)
  - `getter_list` &mut Vec<ReflectedGetter>: List to add getters without a set place to.
  - `anchored_getter_list` &mut Vec<(&'static str, ReflectedGetter)>: List to add getters placed with `#[reflect(after = "...")]` to, as (preceding original getter name, getter).

## ReflectGetProbe (struct) (ReflectGetViaName)

Autoref specialization for reading getter values. (the macro calls it as (&&&ReflectGetProbe(&value)).read_reflect_value())
- Uses the way method lookup strips references one at a time to pick a matching implementation, choosing in the order convertible type (ToReflectValue) → container type (Option, array) → type name.
  Getters of non-convertible types are also left out of saving like the original without a compile error, and are shown as [type name] in the inspector.

Example: Reading in exactly the form the macro calls

```rust
use vanilla::core::reflection::{ReflectGetProbe, ReflectGetViaName as _, ReflectGetViaOpaque as _, ReflectGetViaValue as _};
use vanilla::{ReflectValue, Vector2};

let speed = 0.5f32;
assert_eq!((&&&ReflectGetProbe(&speed)).read_reflect_value(), ReflectValue::Number(0.5));
let points = vec![Vector2::zero()];
assert_eq!((&&&ReflectGetProbe(&points)).read_reflect_value(), ReflectValue::Object(String::from("Array")));
let pair = (1u8, 2u8);
assert_eq!((&&&ReflectGetProbe(&pair)).read_reflect_value(), ReflectValue::Object(String::from("(u8, u8)")));
```

- `pub 0: &'a T` (field, since 0.4.0): Reference to the getter value to read.
  - returns &'a T

## ReflectGetViaValue (trait)

Convertible type. (picked first)

Example: Getter value of a convertible type

```rust
use vanilla::core::reflection::{ReflectGetProbe, ReflectGetViaValue};
use vanilla::ReflectValue;

let is_open = true;
assert_eq!((&&ReflectGetProbe(&is_open)).read_reflect_value(), ReflectValue::Boolean(true));
```

- `fn read_reflect_value(&self) -> ReflectValue` (method, since 0.4.0): Reads a getter value as a reflection value.
  - returns ReflectValue: Value converted with ToReflectValue.

## ReflectOpaque (trait)

Container type. (Option is Null if None and the inner type name if Some, arrays are Array. In place of the original null and arrays)

Example: Reflection values of Option and arrays

```rust
use vanilla::core::reflection::ReflectOpaque;
use vanilla::{Color, ReflectValue};

let color: Option<Color> = Some(Color::white());
assert_eq!(color.to_opaque_reflect_value(), ReflectValue::Object(String::from("Color")));
let empty_color: Option<Color> = None;
assert_eq!(empty_color.to_opaque_reflect_value(), ReflectValue::Null);
assert_eq!(vec![1, 2].to_opaque_reflect_value(), ReflectValue::Object(String::from("Array")));
```

- `fn to_opaque_reflect_value(&self) -> ReflectValue` (method, since 0.4.0): Reflection value of a container type.
  - returns ReflectValue: Option is Null if None, the inner type name if Some. Arrays are `Object("Array")`.

## ReflectGetViaOpaque (trait)

Picks the container type. (picked second)

Example: An Option holding a type not convertible to a value

```rust
use vanilla::core::reflection::{ReflectGetProbe, ReflectGetViaOpaque};
use vanilla::ReflectValue;

let empty_list: Option<Vec<u8>> = None;
assert_eq!((&ReflectGetProbe(&empty_list)).read_reflect_value(), ReflectValue::Null);
```

- `fn read_reflect_value(&self) -> ReflectValue` (method, since 0.4.0): Reads a getter value of a container type.
  - returns ReflectValue: Value converted with ReflectOpaque.

## ReflectGetViaName (trait)

Type name. (picked last. In place of `[constructor.name]` of the original formatPropertyValue)

Example: Non-convertible types as type names

```rust
use vanilla::core::reflection::{ReflectGetProbe, ReflectGetViaName};
use vanilla::ReflectValue;

let pair = (1u8, 2u8);
assert_eq!(ReflectGetProbe(&pair).read_reflect_value(), ReflectValue::Object(String::from("(u8, u8)")));
```

- `fn read_reflect_value(&self) -> ReflectValue` (method, since 0.4.0): Reads as a type name.
  - returns ReflectValue: `Object(short type name)`.

## ReflectSetProbe (struct) (ReflectSetViaValue<T>)

Autoref specialization for reading setter values. (the macro calls it as (`&ReflectSetProbe::<SetterType>`::new(value)).convert_reflect_value())
- If the setter type is FromReflectValue it converts, otherwise it is always None so reading that property is skipped. (no compile error)

Example: Trying to convert to the setter type (None for non-convertible types)

```rust
use std::rc::Rc;
use vanilla::core::reflection::{ReflectSetProbe, ReflectSetViaNone as _, ReflectSetViaValue as _};
use vanilla::{Color, ReflectValue};

let color_list = ReflectValue::List(vec![ReflectValue::Number(1.0), ReflectValue::Number(0.0), ReflectValue::Number(0.0), ReflectValue::Number(1.0)]);
assert_eq!((&ReflectSetProbe::<Color>::new(&color_list)).convert_reflect_value(), Some(Color::create(1.0, 0.0, 0.0, 1.0)));
assert!((&ReflectSetProbe::<Rc<Vec<u8>>>::new(&color_list)).convert_reflect_value().is_none());
```

- `pub fn new(value: &'a ReflectValue) -> ReflectSetProbe<'a, T>` (constructor, since 0.4.0): Constructor.
  - `value` &'a ReflectValue: Reflection value to convert.
  - returns ReflectSetProbe<'a, T>: Probe that converts to setter type T.

## ReflectSetViaValue (trait)

Readable type. (picked first)

Example: A number to an integer setter type

```rust
use vanilla::core::reflection::{ReflectSetProbe, ReflectSetViaValue};
use vanilla::ReflectValue;

let number = ReflectValue::Number(3.0);
assert_eq!(ReflectSetProbe::<usize>::new(&number).convert_reflect_value(), Some(3));
```

- `fn convert_reflect_value(&self) -> Option<T>` (method, since 0.4.0): Converts a reflection value to the setter type.
  - returns Option<T>: The converted value. None if the type does not match.

## ReflectSetViaNone (trait)

Unreadable type. (always None)

Example: A type without FromReflectValue

```rust
use std::rc::Rc;
use vanilla::core::reflection::{ReflectSetProbe, ReflectSetViaNone};
use vanilla::ReflectValue;

let number = ReflectValue::Number(3.0);
assert!((&ReflectSetProbe::<Rc<Vec<u8>>>::new(&number)).convert_reflect_value().is_none());
```

- `fn convert_reflect_value(&self) -> Option<T>` (method, since 0.4.0): Value of an unreadable type.
  - returns Option<T>: Always None.

## Functions and constants

- `pub const COMPONENT_REFLECTED_PROPERTIES: [ReflectedProperty; 3]` (field, since 0.4.0): Saved properties of the original Component base class. (isGizmoVisible / setGizmoVisible, isEnable / setEnable, getComponentType / setComponentType, definition order)
  - returns [ReflectedProperty; 3]
- `pub fn collect_reflected_properties(component: &dyn Component) -> Vec<ReflectedProperty>` (method, since 0.4.0): A component's saved property list. (Original collectReflectedProperties: this type's own, then the Component common properties, the earlier one wins for the same key)
  - `component` &dyn Component: The component to inspect.
  - returns Vec<ReflectedProperty>: Saved property list. (after this type's, those of GizmoVisible, Enable, ComponentType whose keys are missing)
- `pub fn read_reflected_property(component: &dyn Component, property_name: &str) -> Option<ReflectValue>` (method, since 0.4.0): Reads a component's property value. (the original `component[getterName]`(), this type's first, otherwise the common Component properties)
  - `component` &dyn Component: Component to read.
  - `property_name` &str: Original key.
  - returns Option<ReflectValue>: Property value. None if the key is in neither this type nor the common properties.

  Example: A component without getters still reads and writes the common properties

  ```rust
  use vanilla::core::reflection::{read_reflected_property, write_reflected_property};
  use vanilla::{reflect, Component, ComponentBase, ReflectValue};
  
  struct Plain {
  	component_base: ComponentBase,
  }
  
  #[reflect]
  impl Plain {}
  
  #[reflect]
  impl Component for Plain {
  }
  
  let mut plain = Plain {
  	component_base: ComponentBase::new(),
  };
  assert_eq!(read_reflected_property(&plain, "Enable"), Some(ReflectValue::Boolean(true)));
  assert_eq!(read_reflected_property(&plain, "ComponentType"), Some(ReflectValue::String(String::from("Plain"))));
  assert_eq!(read_reflected_property(&plain, "Speed"), None);
  assert!(write_reflected_property(&mut plain, "Enable", &ReflectValue::Boolean(false)));
  assert!(!plain.is_enable());
  assert!(!write_reflected_property(&mut plain, "ComponentType", &ReflectValue::String(String::from("Other"))));
  ```

- `pub fn read_reflected_property_mut(component: &mut dyn Component, property_name: &str) -> Option<ReflectValue>` (method, since 0.4.0): Reads a component's property value. (including &mut self getters)
  - `component` &mut dyn Component: Component to read.
  - `property_name` &str: Original key.
  - returns Option<ReflectValue>: Property value. None if the key is in neither this type nor the common properties.
- `pub fn write_reflected_property(component: &mut dyn Component, property_name: &str, value: &ReflectValue) -> bool` (method, since 0.4.0): Sets a component's property value. (the original `component[setterName]`(value), true if set. false for ComponentType because Rust has no setter)
  - `component` &mut dyn Component: Component to set.
  - `property_name` &str: Original key.
  - `value` &ReflectValue: Value to set.
  - returns bool: true if set. false if the type does not match, the key is unknown, or there is no setter.
- `pub fn write_reflected_property_in_tree(component: &mut dyn Component, node_tree: &mut NodeTree, property_name: &str, value: &ReflectValue) -> bool` (method, since 0.4.0): Sets a component's property value. (including setters taking the tree. Called with this component taken out of the tree, NodeTree::with_component_at_mut)
  - `component` &mut dyn Component: Component to set. (taken out of the tree)
  - `node_tree` &mut NodeTree: Tree to pass to setters taking the tree.
  - `property_name` &str: Original key.
  - `value` &ReflectValue: Value to set.
  - returns bool: true if set. false if the type does not match, the key is unknown, or there is no setter.

  Example: Taking out a component attached to the tree by its order index and setting a property

  ```rust
  use vanilla::core::reflection::write_reflected_property_in_tree;
  use vanilla::{Component, NodeTree, Paint, ReflectValue};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let node_id = node_tree.create_child(root_id, "panel");
  node_tree[node_id].add_component(Paint::new());
  let is_written = node_tree.with_component_at_mut(node_id, 0, |component, tree| {
  	write_reflected_property_in_tree(component, tree, "Enable", &ReflectValue::Boolean(false))
  });
  assert_eq!(is_written, Some(true));
  assert!(!node_tree[node_id].get_component::<Paint>().expect("paint").is_enable());
  ```

- `pub fn collect_reflected_getters(component: &dyn Component) -> Vec<ReflectedGetter>` (method, since 0.4.0): All getter values of a component. (Original getComponentProperties, only that type's own, without those of the Component base class)
  - `component` &dyn Component: The component to inspect.
  - returns Vec<ReflectedGetter>: List of getter names and values. (An empty list for a component without getters)
- `pub fn collect_reflected_getters_in_tree(component: &dyn Component, node_tree: &NodeTree) -> Vec<ReflectedGetter>` (method, since 0.4.0): All getter values of a component. (Including getters that take the tree)
  - `component` &dyn Component: The component to inspect.
  - `node_tree` &NodeTree: Tree to pass to getters taking the tree.
  - returns Vec<ReflectedGetter>: List of getter names and values. (An empty list for a component without getters)
- `pub fn collect_reflected_getters_mut(component: &mut dyn Component, node_tree: &NodeTree) -> Vec<ReflectedGetter>` (method, since 0.4.0): All getter values of a component. (Including &mut self getters and getters that take the tree. Call it with the component taken out of the tree, NodeTree::with_component_at_mut)
  - `component` &mut dyn Component: Component to look at. (taken out of the tree)
  - `node_tree` &NodeTree: Tree to pass to getters taking the tree.
  - returns Vec<ReflectedGetter>: List of getter names and values. (An empty list for a component without getters)
- `pub fn has_mutable_getters(component: &dyn Component) -> bool` (method, since 0.4.0): Whether a component has &mut self getters.
  - `component` &dyn Component: The component to inspect.
  - returns bool: true if there is a &mut self getter.
- `pub fn insert_anchored_getters(getter_list: &mut Vec<ReflectedGetter>, anchored_getter_list: Vec<(&'static str, ReflectedGetter)>)` (method, since 0.4.0): Inserts getters with a set place. (right after the preceding getter, in stored order for the same preceding getter. At the end if the preceding getter is missing, not inserted if the same name already exists)
  - `getter_list` &mut Vec<ReflectedGetter>: List to insert into.
  - `anchored_getter_list` Vec<(&'static str, ReflectedGetter)>: List of (preceding original getter name, getter). (inserted in stored order)

  Example: Inserting right after the preceding getter

  ```rust
  use vanilla::core::reflection::{insert_anchored_getters, push_reflected_getter};
  use vanilla::{ReflectedGetter, ReflectValue};
  
  let mut getter_list: Vec<ReflectedGetter> = Vec::new();
  push_reflected_getter(&mut getter_list, "getText", ReflectValue::String(String::from("hi")));
  push_reflected_getter(&mut getter_list, "getFontSize", ReflectValue::Number(16.0));
  let anchored_getter_list = vec![("getText", ReflectedGetter {
  	name: "getFittedFontSize",
  	value: ReflectValue::Number(12.0),
  })];
  insert_anchored_getters(&mut getter_list, anchored_getter_list);
  let getter_names: Vec<&str> = getter_list.iter().map(|getter| getter.name).collect();
  assert_eq!(getter_names, ["getText", "getFittedFontSize", "getFontSize"]);
  ```

- `pub fn push_reflected_getter(getter_list: &mut Vec<ReflectedGetter>, name: &'static str, value: ReflectValue)` (method, since 0.4.0): Adds one getter. (not added if the same name already exists. seenNames of the original getComponentProperties, the subclass's comes first)
  - `getter_list` &mut Vec<ReflectedGetter>: List to add to.
  - `name` &'static str: Original function name. (getLineWidth)
  - `value` ReflectValue: Getter value.
- `pub fn read_shortest_number(number: f32) -> f64` (method, since 0.4.0): f64 read from the shortest decimal of an f32. (0.22 becomes 0.22, the same as writing that value as a JavaScript number in the original)
  - `number` f32: Number to convert.
  - returns f64: f64 read from the shortest decimal text. (infinities and NaN are widened as is)

  Example: Widening f32 0.22 does not give 0.22, but reading its shortest decimal gives 0.22

  ```rust
  use vanilla::core::reflection::read_shortest_number;
  
  assert_eq!(read_shortest_number(0.22), 0.22);
  assert_ne!(0.22f32 as f64, 0.22);
  ```

- `pub fn read_short_type_name<T: ?Sized>() -> String` (method, since 0.4.0): Short name of a type. (strips the module path and wrapper types. `&alloc::rc::Rc<vanilla::core::image::Image>` → Image)
  - returns String: Name with the module path, leading &, and wrapper types (Rc, Arc, Box, RefCell, Cell) stripped. (paths of inner generic arguments are stripped too)

  Example: Stripping wrapper types and paths

  ```rust
  use std::cell::RefCell;
  use std::rc::Rc;
  use vanilla::core::reflection::read_short_type_name;
  use vanilla::{Color, Vector2};
  
  assert_eq!(read_short_type_name::<Rc<RefCell<Color>>>(), "Color");
  assert_eq!(read_short_type_name::<&Vector2>(), "Vector2");
  assert_eq!(read_short_type_name::<Option<Color>>(), "Option<Color>");
  ```

## ReflectValue (enum) (Clone, Debug, PartialEq)

Reflection value. (in place of the JavaScript values the original getters return)
- Null is the original null (Rust None), List is an array read from a document, Object is the type name of a value that cannot be carried over. (`[name]` in the inspector)

- `Null`: No value. (the original null, Rust None)
- `Boolean` = `Boolean(bool)`: Boolean.
- `Number` = `Number(f64)`: Number. (f32 and integer types are converted to this too)
- `String` = `String(String)`: Text. (also enums that are text in the original)
- `Vector2` = `Vector2(Vector2)`: 2D vector.
- `Color` = `Color(Color)`: Color.
- `Rect` = `Rect(Rect)`: Rect.
- `Node` = `Node(NodeId)`: Node handle.
- `List` = `List(Vec<ReflectValue>)`: Array read from a document. (Vector2 is taken as two numbers, Color as four)
- `Object` = `Object(String)`: Type name of a value that cannot be carried over. (`[name]` in the inspector)
