Skip to content

Commit

Permalink
Add method for getting the superclasses of an Instance to rbx_reflect…
Browse files Browse the repository at this point in the history
…ion (#402)
  • Loading branch information
Dekkonot authored Mar 14, 2024
1 parent 06b1fad commit 55dd7e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rbx_reflection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# rbx_reflection Changelog

## Unreleased Changes
* Add `superclasses` method to `ReflectionDatabase` to get a set of superclasses for a given class. ([#402])

[#402]: https://github.com/rojo-rbx/rbx-dom/pull/402

## 4.5.0 (2024-01-16)
* Update to rbx_types 1.8.
Expand Down
18 changes: 18 additions & 0 deletions rbx_reflection/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ impl<'a> ReflectionDatabase<'a> {
enums: HashMap::new(),
}
}

/// Returns a list of superclasses for the provided class name. This list
/// will start with the provided class and end with `Instance` if the class
/// exists.
pub fn superclasses(&self, class_name: &str) -> Option<Vec<&ClassDescriptor>> {
// As of the time of writing (14 March 2024), the class with the most
// superclasses has 6 of them.
let mut list = Vec::with_capacity(6);
let mut current_class = self.classes.get(class_name);
current_class?;

while let Some(class) = current_class {
list.push(class);
current_class = class.superclass.as_ref().and_then(|s| self.classes.get(s));
}

Some(list)
}
}

/// Describes a class of Instance, its properties, and its relation to other
Expand Down

0 comments on commit 55dd7e3

Please sign in to comment.