Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for getting the superclasses of an Instance to rbx_reflection #402

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions rbx_reflection/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ 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<HashSet<Cow<'a, str>>> {
// Parts have 4 superclasses, and they're generally what most models
// are composed of so we allocate enough for them.
// On average each class has 2.6 superclasses, so this benefits our
// theoretical 'average' case too.
let mut list = HashSet::with_capacity(5);
let mut current_class = self.classes.get(class_name);
current_class?;

while let Some(class) = current_class {
list.insert(class.name.clone());
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