Skip to content

Commit

Permalink
Add method for querying whether a given short type path is ambiguous (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
coreh and mockersf authored Feb 19, 2024
1 parent 4134351 commit a475511
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions crates/bevy_reflect/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit a475511

Please sign in to comment.