From a475511f433a0fb51db716438cea33ee16f31a9c Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Mon, 19 Feb 2024 13:47:11 -0300 Subject: [PATCH] Add method for querying whether a given short type path is ambiguous (#11840) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Currently, the `ambiguous_names` hash set in `TypeRegistry` is used to keep track of short type names that are ambiguous, and to require the use of long type names for these types. However, there's no way for the consumer of `TypeRegistry` to known whether a given call to `get_with_short_type_path()` or `get_with_short_type_path_mut()` failed because a type was not registered at all, or because the short name is ambiguous. This can be used, for example, for better error reporting to the user by an editor tool. Here's some code that uses this, from my remote protocol exploration branch: ```rust let type_registration = type_registry .get_with_type_path(component_name) .or_else(|| registry.get_with_short_type_path(component_name)) .ok_or_else(|| { if type_registry.is_ambiguous(component_name) { BrpError::ComponentAmbiguous(component_name.clone()) } else { BrpError::MissingTypeRegistration(component_name.clone()) } })? ``` ## Solution - Introduces a `is_ambiguous()` method. - Also drive-by fixes two documentation comments that had broken links. --- ## Changelog - Added a `TypeRegistry::is_ambiguous()` method, for checking whether a given short type path is ambiguous (e.g. `MyType` potentially matching either `some_crate::MyType` or `another_crate::MyType`) --------- Co-authored-by: François --- crates/bevy_reflect/src/type_registry.rs | 30 ++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/type_registry.rs b/crates/bevy_reflect/src/type_registry.rs index 8118a7824e..a67678e836 100644 --- a/crates/bevy_reflect/src/type_registry.rs +++ b/crates/bevy_reflect/src/type_registry.rs @@ -211,7 +211,7 @@ impl TypeRegistry { /// If the short type path is ambiguous, or if no type with the given path /// has been registered, returns `None`. /// - /// [type path]: TypePath::short_type_path + /// [short type path]: TypePath::short_type_path pub fn get_with_short_type_path(&self, short_type_path: &str) -> Option<&TypeRegistration> { self.short_path_to_id .get(short_type_path) @@ -224,7 +224,7 @@ impl TypeRegistry { /// If the short type path is ambiguous, or if no type with the given path /// has been registered, returns `None`. /// - /// [type path]: TypePath::short_type_path + /// [short type path]: TypePath::short_type_path pub fn get_with_short_type_path_mut( &mut self, short_type_path: &str, @@ -234,6 +234,32 @@ impl TypeRegistry { .and_then(|id| self.registrations.get_mut(id)) } + /// Returns `true` if the given [short type path] is ambiguous, that is, it matches multiple registered types. + /// + /// # Example + /// ``` + /// # use bevy_reflect::TypeRegistry; + /// # mod foo { + /// # use bevy_reflect::Reflect; + /// # #[derive(Reflect)] + /// # pub struct MyType; + /// # } + /// # mod bar { + /// # use bevy_reflect::Reflect; + /// # #[derive(Reflect)] + /// # pub struct MyType; + /// # } + /// let mut type_registry = TypeRegistry::default(); + /// type_registry.register::(); + /// type_registry.register::(); + /// assert_eq!(type_registry.is_ambiguous("MyType"), true); + /// ``` + /// + /// [short type path]: TypePath::short_type_path + pub fn is_ambiguous(&self, short_type_path: &str) -> bool { + self.ambiguous_names.contains(short_type_path) + } + /// Returns a reference to the [`TypeData`] of type `T` associated with the given [`TypeId`]. /// /// The returned value may be used to downcast [`Reflect`] trait objects to