Add method for querying whether a given short type path is ambiguous (#11840)

# 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 <mockersf@gmail.com>
This commit is contained in:
Marco Buono 2024-02-19 13:47:11 -03:00 committed by GitHub
parent 4134351d9b
commit a475511f43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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::<foo::MyType>();
/// type_registry.register::<bar::MyType>();
/// 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