From 4f4b5cdeea475b4fb57a50b749a7465bffc36578 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Sun, 6 Oct 2024 09:40:17 -0700 Subject: [PATCH] superclass_iter: simplify implementation and api function signature --- rbx_reflection/src/database.rs | 18 +++++++----------- rbx_reflection_database/src/lib.rs | 5 ++--- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/rbx_reflection/src/database.rs b/rbx_reflection/src/database.rs index 2add6f0e2..ff9fdf525 100644 --- a/rbx_reflection/src/database.rs +++ b/rbx_reflection/src/database.rs @@ -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 { - 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) } } @@ -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, } } diff --git a/rbx_reflection_database/src/lib.rs b/rbx_reflection_database/src/lib.rs index f693a0125..7a166edb9 100644 --- a/rbx_reflection_database/src/lib.rs +++ b/rbx_reflection_database/src/lib.rs @@ -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, @@ -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"));