Skip to content

Commit

Permalink
superclass_iter: simplify implementation and api function signature
Browse files Browse the repository at this point in the history
  • Loading branch information
krakow10 committed Oct 6, 2024
1 parent 5365654 commit 4f4b5cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
18 changes: 7 additions & 11 deletions rbx_reflection/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ pub struct ReflectionDatabase<'a> {

pub struct SuperClassIter<'a> {
database: &'a ReflectionDatabase<'a>,
descriptor: Option<&'a ClassDescriptor<'a>>,
}
impl<'a> SuperClassIter<'a> {
fn next_descriptor(&self) -> Option<&'a ClassDescriptor<'a>> {
let superclass = self.descriptor?.superclass.as_ref()?;
self.database.classes.get(superclass)
}
class_name: &'a str,
}
impl<'a> Iterator for SuperClassIter<'a> {
type Item = &'a ClassDescriptor<'a>;
fn next(&mut self) -> Option<Self::Item> {
let next_descriptor = self.next_descriptor();
std::mem::replace(&mut self.descriptor, next_descriptor)
let next_descriptor = self.database.classes.get(self.class_name)?;
// abuse empty string to create a non-class
self.class_name = next_descriptor.superclass.as_deref().unwrap_or("");
Some(next_descriptor)
}
}

Expand Down Expand Up @@ -79,10 +75,10 @@ impl<'a> ReflectionDatabase<'a> {

/// Returns an iterator of superclasses for the provided ClassDescriptor. This
/// iterator will start with the provided class and end with `Instance`.
pub fn superclasses_iter(&'a self, descriptor: &'a ClassDescriptor<'a>) -> SuperClassIter {
pub fn superclasses_iter(&'a self, class_name: &'a str) -> SuperClassIter {
SuperClassIter {
database: self,
descriptor: Some(descriptor),
class_name,
}
}

Expand Down
5 changes: 2 additions & 3 deletions rbx_reflection_database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ mod test {
#[test]
fn superclasses_iter_test() {
let database = get();
let part_class_descriptor = database.classes.get("Part");
let mut iter = database.superclasses_iter(part_class_descriptor.unwrap());
let mut iter = database.superclasses_iter("Part");
fn class_descriptor_eq(lhs: Option<&ClassDescriptor>, rhs: Option<&ClassDescriptor>) {
let eq = match (lhs, rhs) {
(Some(lhs), Some(rhs)) => lhs.name == rhs.name,
Expand All @@ -36,7 +35,7 @@ mod test {
};
assert!(eq, "{:?} != {:?}", lhs, rhs);
}
class_descriptor_eq(iter.next(), part_class_descriptor);
class_descriptor_eq(iter.next(), database.classes.get("Part"));
class_descriptor_eq(iter.next(), database.classes.get("FormFactorPart"));
class_descriptor_eq(iter.next(), database.classes.get("BasePart"));
class_descriptor_eq(iter.next(), database.classes.get("PVInstance"));
Expand Down

0 comments on commit 4f4b5cd

Please sign in to comment.