From fbd6177a07ee9986afb206d979a39592a3abf06f Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 4 May 2025 01:19:00 -0700 Subject: [PATCH] Drop unused context Using pointers to refer to elements reduces the need to drag context around. --- app-proto/src/add_remove.rs | 2 +- app-proto/src/assembly.rs | 49 ++++++++++++++++--------------------- app-proto/src/outline.rs | 8 +++--- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/app-proto/src/add_remove.rs b/app-proto/src/add_remove.rs index ea86186..f3bbc97 100644 --- a/app-proto/src/add_remove.rs +++ b/app-proto/src/add_remove.rs @@ -241,7 +241,7 @@ pub fn AddRemove() -> View { .unwrap() ); state.assembly.insert_regulator( - Rc::new(InversiveDistanceRegulator::new(subjects, &state.assembly)) + Rc::new(InversiveDistanceRegulator::new(subjects)) ); state.selection.update(|sel| sel.clear()); } diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index d1d24f4..200bd34 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -44,24 +44,18 @@ pub type ElementColor = [f32; 3]; static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0); pub trait ProblemPoser { - fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab>); + fn pose(&self, problem: &mut ConstraintProblem); } pub trait Element: ProblemPoser + DisplayItem { // the default identifier for an element of this type fn default_id() -> String where Self: Sized; - // create the default example of an element of this type + // the default example of an element of this type fn default(id: String, id_num: u64) -> Self where Self: Sized; - // the regulators that should be created when an element of this type is - // inserted into the given assembly with the given storage key - /* KLUDGE */ - // this organization made sense when regulators identified their subjects by - // storage key, so the element has to be inserted before its regulators - // could be created. now that regulators identify their subjects by pointer, - // we should consider refactoring - fn default_regulators(self: Rc, _assembly: &Assembly) -> Vec> where Self: Sized { + // the default regulators that come with this element + fn default_regulators(self: Rc) -> Vec> { Vec::new() } @@ -165,8 +159,8 @@ impl Element for Sphere { ) } - fn default_regulators(self: Rc, assembly: &Assembly) -> Vec> { - vec![Rc::new(HalfCurvatureRegulator::new(self, assembly))] + fn default_regulators(self: Rc) -> Vec> { + vec![Rc::new(HalfCurvatureRegulator::new(self))] } fn id(&self) -> &String { @@ -199,7 +193,7 @@ impl Element for Sphere { } impl ProblemPoser for Sphere { - fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab>) { + fn pose(&self, problem: &mut ConstraintProblem) { let index = self.column_index().expect( format!("Sphere \"{}\" should be indexed before writing problem data", self.id).as_str() ); @@ -283,7 +277,7 @@ impl Element for Point { } impl ProblemPoser for Point { - fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab>) { + fn pose(&self, problem: &mut ConstraintProblem) { let index = self.column_index().expect( format!("Point \"{}\" should be indexed before writing problem data", self.id).as_str() ); @@ -304,7 +298,7 @@ pub trait Regulator: ProblemPoser + OutlineItem { // preconditioning when the set point is present, and use its return value // to report whether the set is present. the default implementation does no // preconditioning - fn try_activate(&self, _assembly: &Assembly) -> bool { + fn try_activate(&self) -> bool { self.set_point().with(|set_pt| set_pt.is_present()) } } @@ -316,7 +310,7 @@ pub struct InversiveDistanceRegulator { } impl InversiveDistanceRegulator { - pub fn new(subjects: [Rc; 2], assembly: &Assembly) -> InversiveDistanceRegulator { + pub fn new(subjects: [Rc; 2]) -> InversiveDistanceRegulator { let representations = subjects.each_ref().map(|subj| subj.representation()); let measurement = create_memo(move || { representations[0].with(|rep_0| @@ -347,7 +341,7 @@ impl Regulator for InversiveDistanceRegulator { } impl ProblemPoser for InversiveDistanceRegulator { - fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab>) { + fn pose(&self, problem: &mut ConstraintProblem) { self.set_point.with_untracked(|set_pt| { if let Some(val) = set_pt.value { let [row, col] = self.subjects.each_ref().map( @@ -368,7 +362,7 @@ pub struct HalfCurvatureRegulator { } impl HalfCurvatureRegulator { - pub fn new(subject: Rc, assembly: &Assembly) -> HalfCurvatureRegulator { + pub fn new(subject: Rc) -> HalfCurvatureRegulator { let measurement = subject.representation().map( |rep| rep[Sphere::CURVATURE_COMPONENT] ); @@ -392,7 +386,7 @@ impl Regulator for HalfCurvatureRegulator { self.set_point } - fn try_activate(&self, assembly: &Assembly) -> bool { + fn try_activate(&self) -> bool { match self.set_point.with(|set_pt| set_pt.value) { Some(half_curv) => { self.subject.representation().update( @@ -406,7 +400,7 @@ impl Regulator for HalfCurvatureRegulator { } impl ProblemPoser for HalfCurvatureRegulator { - fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab>) { + fn pose(&self, problem: &mut ConstraintProblem) { self.set_point.with_untracked(|set_pt| { if let Some(val) = set_pt.value { let col = self.subject.column_index().expect( @@ -468,11 +462,11 @@ impl Assembly { // insert the element let id = elt.id().clone(); let elt_rc = Rc::new(elt); - let key = self.elements.update(|elts| elts.insert(elt_rc.clone())); /* KLUDGE */ // reorganize to avoid cloning? - self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, elt_rc.clone())); /* KLUDGE */ // reorganize to avoid cloning? + let key = self.elements.update(|elts| elts.insert(elt_rc.clone())); + self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, elt_rc.clone())); // create and insert the element's default regulators - for reg in elt_rc.default_regulators(&self) { + for reg in elt_rc.default_regulators() { self.insert_regulator(reg); } @@ -513,8 +507,7 @@ impl Assembly { ); // add the regulator to each subject's regulator list - let subjects = regulator.subjects(); - let subject_regulators: Vec<_> = subjects.into_iter().map( + let subject_regulators: Vec<_> = regulator.subjects().into_iter().map( |subj| subj.regulators() ).collect(); for regulators in subject_regulators { @@ -531,7 +524,7 @@ impl Assembly { format!("Updated regulator with subjects {:?}", regulator.subjects()) )); - if regulator.try_activate(&self_for_effect) { + if regulator.try_activate() { self_for_effect.realize(); } }); @@ -573,11 +566,11 @@ impl Assembly { let problem = self.elements.with_untracked(|elts| { let mut problem = ConstraintProblem::new(elts.len()); for (_, elt) in elts { - elt.pose(&mut problem, elts); + elt.pose(&mut problem); } self.regulators.with_untracked(|regs| { for (_, reg) in regs { - reg.pose(&mut problem, elts); + reg.pose(&mut problem); } }); problem diff --git a/app-proto/src/outline.rs b/app-proto/src/outline.rs index 5aeb62b..178a1b7 100644 --- a/app-proto/src/outline.rs +++ b/app-proto/src/outline.rs @@ -11,7 +11,6 @@ use crate::{ AppState, assembly::{ Element, - ElementKey, HalfCurvatureRegulator, InversiveDistanceRegulator, Regulator, @@ -96,7 +95,6 @@ pub trait OutlineItem { impl OutlineItem for InversiveDistanceRegulator { fn outline_item(self: Rc, element: Rc) -> View { - let state = use_context::(); let other_subject_label = if self.subjects[0] == element { self.subjects[1].label() } else { @@ -138,7 +136,7 @@ fn RegulatorOutlineItem(regulator_key: RegulatorKey, element: Rc) - // a list item that shows an element in an outline view of an assembly #[component(inline_props)] -fn ElementOutlineItem(key: ElementKey, element: Rc) -> View { +fn ElementOutlineItem(element: Rc) -> View { let state = use_context::(); let class = { let element_for_class = element.clone(); @@ -271,8 +269,8 @@ pub fn Outline() -> View { ) { Keyed( list=element_list, - view=|(key, elt)| view! { - ElementOutlineItem(key=key, element=elt) + view=|(_, elt)| view! { + ElementOutlineItem(element=elt) }, key=|(_, elt)| elt.serial() )