@@ -38,10 +38,6 @@ pub trait GetTypeRegistration {
38
38
///
39
39
/// This method is called by [`TypeRegistry::register`] to register any other required types.
40
40
/// Often, this is done for fields of structs and enum variants to ensure all types are properly registered.
41
- ///
42
- /// If manually implementing, it is _highly_ recommended to use [`TypeRegistry::try_register`] for these dependent types.
43
- /// Using this method allows the type to be skipped if it has already been registered, thus preventing any
44
- /// undesired overwrites and reducing registration costs.
45
41
#[ allow( unused_variables) ]
46
42
fn register_type_dependencies ( registry : & mut TypeRegistry ) { }
47
43
}
@@ -84,39 +80,53 @@ impl TypeRegistry {
84
80
registry
85
81
}
86
82
87
- /// Registers the type `T`, adding reflect data as specified in the [`Reflect`] derive:
83
+ /// Attempts to register the type `T` if it has not yet been registered already.
84
+ ///
85
+ /// If the registration for type `T` already exists, it will not be registered again.
86
+ /// To register the type, overwriting any existing registration, use [register](Self::register) instead.
87
+ ///
88
+ /// Additionally, this will add the reflect [data](TypeData) as specified in the [`Reflect`] derive:
88
89
/// ```rust,ignore
89
90
/// #[derive(Reflect)]
90
91
/// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
92
+ /// struct Foo;
91
93
/// ```
92
94
pub fn register < T > ( & mut self )
93
95
where
94
96
T : GetTypeRegistration ,
95
97
{
96
- self . add_registration ( T :: get_type_registration ( ) ) ;
98
+ if !self . add_registration ( T :: get_type_registration ( ) ) {
99
+ return ;
100
+ }
101
+
97
102
T :: register_type_dependencies ( self ) ;
98
103
}
99
104
100
- /// Attempts to register the type `T` if it has not yet been registered already .
105
+ /// Attempts to register the type described by `registration` .
101
106
///
102
- /// If the registration for type `T` already exists, it will not be registered again.
107
+ /// If the registration for the type already exists, it will not be registered again.
103
108
///
104
- /// To register the type, overwriting any existing registration, use [register](Self::register) instead.
105
- pub fn try_register < T > ( & mut self )
106
- where
107
- T : GetTypeRegistration + ' static ,
108
- {
109
- if !self . contains ( TypeId :: of :: < T > ( ) ) {
110
- self . register :: < T > ( ) ;
109
+ /// To forcibly register the type, overwriting any existing registration, use the
110
+ /// [`force_add_registration`](Self::force_add_registration) method instead.
111
+ ///
112
+ /// Returns `true` if the registration was successfully added,
113
+ /// or `false` if it already exists.
114
+ pub fn add_registration ( & mut self , registration : TypeRegistration ) -> bool {
115
+ if self . contains ( registration. type_id ( ) ) {
116
+ false
117
+ } else {
118
+ self . force_add_registration ( registration) ;
119
+ true
111
120
}
112
121
}
113
122
114
123
/// Registers the type described by `registration`.
115
- pub fn add_registration ( & mut self , registration : TypeRegistration ) {
116
- if self . registrations . contains_key ( & registration. type_id ( ) ) {
117
- return ;
118
- }
119
-
124
+ ///
125
+ /// If the registration for the type already exists, it will be overwritten.
126
+ ///
127
+ /// To avoid overwriting existing registrations, it's recommended to use the
128
+ /// [`register`](Self::register) or [`add_registration`](Self::add_registration) methods instead.
129
+ pub fn force_add_registration ( & mut self , registration : TypeRegistration ) {
120
130
let short_name = registration. short_name . to_string ( ) ;
121
131
if self . short_name_to_id . contains_key ( & short_name)
122
132
|| self . ambiguous_names . contains ( & short_name)
0 commit comments