@@ -55,10 +55,6 @@ pub trait GetTypeRegistration {
5555 ///
5656 /// This method is called by [`TypeRegistry::register`] to register any other required types.
5757 /// 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.
6258 #[ allow( unused_variables) ]
6359 fn register_type_dependencies ( registry : & mut TypeRegistry ) { }
6460}
@@ -103,39 +99,53 @@ impl TypeRegistry {
10399 registry
104100 }
105101
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:
107108 /// ```rust,ignore
108109 /// #[derive(Reflect)]
109110 /// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
111+ /// struct Foo;
110112 /// ```
111113 pub fn register < T > ( & mut self )
112114 where
113115 T : GetTypeRegistration ,
114116 {
115- self . add_registration ( T :: get_type_registration ( ) ) ;
117+ if !self . add_registration ( T :: get_type_registration ( ) ) {
118+ return ;
119+ }
120+
116121 T :: register_type_dependencies ( self ) ;
117122 }
118123
119- /// Attempts to register the type `T` if it has not yet been registered already .
124+ /// Attempts to register the type described by `registration` .
120125 ///
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.
122127 ///
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
130139 }
131140 }
132141
133142 /// 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 ) {
139149 let short_name = registration. short_name . to_string ( ) ;
140150 if self . short_name_to_id . contains_key ( & short_name)
141151 || self . ambiguous_names . contains ( & short_name)
0 commit comments