1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use Box;
use TypeRegistration;
use ;
/// A trait for representing type metadata.
///
/// Type data can be registered to the [`TypeRegistry`] and stored on a type's [`TypeRegistration`].
///
/// While type data is often generated using the [`#[reflect_trait]`](crate::reflect_trait) macro,
/// almost any type that implements [`Clone`] can be considered "type data".
/// This is because it has a blanket implementation over all `T` where `T: Clone + Send + Sync + 'static`.
///
/// For creating your own type data, see the [`CreateTypeData`] trait.
///
/// See the [crate-level documentation] for more information on type data and type registration.
///
/// [`TypeRegistry`]: crate::TypeRegistry
/// [`TypeRegistration`]: crate::TypeRegistration
/// [crate-level documentation]: crate
impl_downcast!;
/// A trait for creating [`TypeData`].
///
/// Normally any type that is `Clone + Send + Sync + 'static` can be used as type data
/// and inserted into a [`TypeRegistration`] using [`TypeRegistration::insert`]
/// However, only types that implement this trait may be registered using [`TypeRegistry::register_type_data`],
/// [`TypeRegistry::register_type_data_with`], or via the `#[reflect(MyTrait)]` attribute with the [`Reflect` derive macro].
///
/// Note that in order to work with the `#[reflect(MyTrait)]` attribute,
/// implementors must be named with the `Reflect` prefix (e.g. `ReflectMyTrait`).
///
/// # Input
///
/// By default, this trait expects no input for creating the type data
/// (the `Input` type parameter defaults to `()`).
///
/// However, implementors may choose to implement this trait with other input types.
/// As long as the implementations don't conflict, multiple different input types can be specified.
///
/// # Example
///
/// ```
/// # use bevy_reflect::{CreateTypeData, Reflect};
/// trait Combine {
/// fn combine(a: f32, b: f32) -> f32;
/// }
///
/// #[derive(Clone)]
/// struct ReflectCombine {
/// multiplier: f32,
/// additional: f32,
/// combine: fn(f32, f32) -> f32,
/// }
///
/// impl ReflectCombine {
/// pub fn combine(&self, a: f32, b: f32) -> f32 {
/// let combined = (self.combine)(a, b);
/// let multiplied = self.multiplier * combined;
/// multiplied + self.additional
/// }
/// }
///
/// // A default implementation for when no input is given
/// impl<T: Combine + Reflect> CreateTypeData<T> for ReflectCombine {
/// fn create_type_data(_: ()) -> Self {
/// Self {
/// multiplier: 1.0,
/// additional: 0.0,
/// combine: T::combine,
/// }
/// }
/// }
///
/// // A custom implementation for when a multiplier is given
/// impl<T: Combine + Reflect> CreateTypeData<T, (f32, f32)> for ReflectCombine {
/// fn create_type_data(input: (f32, f32)) -> Self {
/// Self {
/// multiplier: input.0,
/// additional: input.1,
/// combine: T::combine,
/// }
/// }
/// }
///
/// #[derive(Reflect)]
/// // We can have the `Reflect` derive automatically register `ReflectCombine`:
/// #[reflect(Combine)]
/// struct WithoutMultiplier;
///
/// impl Combine for WithoutMultiplier {
/// fn combine(a: f32, b: f32) -> f32 {
/// a + b
/// }
/// }
///
/// #[derive(Reflect)]
/// // We can also given it some input:
/// #[reflect(Combine(2.0, 4.0))]
/// struct WithMultiplier;
///
/// impl Combine for WithMultiplier {
/// fn combine(a: f32, b: f32) -> f32 {
/// a + b
/// }
/// }
///
/// // Or we can simply create the data manually:
/// let without_multiplier = <ReflectCombine as CreateTypeData<WithoutMultiplier>>::create_type_data(());
/// let with_multiplier = <ReflectCombine as CreateTypeData<WithMultiplier, _>>::create_type_data((2.0, 4.0));
///
/// assert_eq!(without_multiplier.combine(1.0, 2.0), 3.0);
/// assert_eq!(with_multiplier.combine(1.0, 2.0), 10.0);
/// ```
///
/// [`TypeRegistration`]: crate::TypeRegistration
/// [`TypeRegistry::register_type_data`]: crate::TypeRegistry::register_type_data
/// [`TypeRegistry::register_type_data_with`]: crate::TypeRegistry::register_type_data_with
/// [`Reflect` derive macro]: derive@crate::Reflect