@@ -55,10 +55,6 @@ pub trait GetTypeRegistration {
55
55
///
56
56
/// This method is called by [`TypeRegistry::register`] to register any other required types.
57
57
/// Often, this is done for fields of structs and enum variants to ensure all types are properly registered.
58
- ///
59
- /// If manually implementing, it is _highly_ recommended to use [`TypeRegistry::try_register`] for these dependent types.
60
- /// Using this method allows the type to be skipped if it has already been registered, thus preventing any
61
- /// undesired overwrites and reducing registration costs.
62
58
#[ allow( unused_variables) ]
63
59
fn register_type_dependencies ( registry : & mut TypeRegistry ) { }
64
60
}
@@ -103,39 +99,53 @@ impl TypeRegistry {
103
99
registry
104
100
}
105
101
106
- /// Registers the type `T`, adding reflect data as specified in the [`Reflect`] derive:
102
+ /// Attempts to register the type `T` if it has not yet been registered already.
103
+ ///
104
+ /// If the registration for type `T` already exists, it will not be registered again.
105
+ /// To register the type, overwriting any existing registration, use [register](Self::register) instead.
106
+ ///
107
+ /// Additionally, this will add the reflect [data](TypeData) as specified in the [`Reflect`] derive:
107
108
/// ```rust,ignore
108
109
/// #[derive(Reflect)]
109
110
/// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
111
+ /// struct Foo;
110
112
/// ```
111
113
pub fn register < T > ( & mut self )
112
114
where
113
115
T : GetTypeRegistration ,
114
116
{
115
- self . add_registration ( T :: get_type_registration ( ) ) ;
117
+ if !self . add_registration ( T :: get_type_registration ( ) ) {
118
+ return ;
119
+ }
120
+
116
121
T :: register_type_dependencies ( self ) ;
117
122
}
118
123
119
- /// Attempts to register the type `T` if it has not yet been registered already .
124
+ /// Attempts to register the type described by `registration` .
120
125
///
121
- /// If the registration for type `T` already exists, it will not be registered again.
126
+ /// If the registration for the type already exists, it will not be registered again.
122
127
///
123
- /// To register the type, overwriting any existing registration, use [register](Self::register) instead.
124
- pub fn try_register < T > ( & mut self )
125
- where
126
- T : GetTypeRegistration + ' static ,
127
- {
128
- if !self . contains ( TypeId :: of :: < T > ( ) ) {
129
- self . register :: < T > ( ) ;
128
+ /// To forcibly register the type, overwriting any existing registration, use the
129
+ /// [`force_add_registration`](Self::force_add_registration) method instead.
130
+ ///
131
+ /// Returns `true` if the registration was successfully added,
132
+ /// or `false` if it already exists.
133
+ pub fn add_registration ( & mut self , registration : TypeRegistration ) -> bool {
134
+ if self . contains ( registration. type_id ( ) ) {
135
+ false
136
+ } else {
137
+ self . force_add_registration ( registration) ;
138
+ true
130
139
}
131
140
}
132
141
133
142
/// Registers the type described by `registration`.
134
- pub fn add_registration ( & mut self , registration : TypeRegistration ) {
135
- if self . registrations . contains_key ( & registration. type_id ( ) ) {
136
- return ;
137
- }
138
-
143
+ ///
144
+ /// If the registration for the type already exists, it will be overwritten.
145
+ ///
146
+ /// To avoid overwriting existing registrations, it's recommended to use the
147
+ /// [`register`](Self::register) or [`add_registration`](Self::add_registration) methods instead.
148
+ pub fn force_add_registration ( & mut self , registration : TypeRegistration ) {
139
149
let short_name = registration. short_name . to_string ( ) ;
140
150
if self . short_name_to_id . contains_key ( & short_name)
141
151
|| self . ambiguous_names . contains ( & short_name)
0 commit comments