From b999449b832d3f3b48f1f4fe448528f721a9999b Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 31 Oct 2025 15:55:27 +0100 Subject: [PATCH] refactor: generalize apply_to_mutations --- codes/eoa_lib/src/perturbation/mod.rs | 48 ++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/codes/eoa_lib/src/perturbation/mod.rs b/codes/eoa_lib/src/perturbation/mod.rs index 1034188184c8cdddc1de0df40747953ee93203b8..513432684c8b5d672f494050991b27c474ac8490 100644 --- a/codes/eoa_lib/src/perturbation/mod.rs +++ b/codes/eoa_lib/src/perturbation/mod.rs @@ -251,14 +251,6 @@ impl>> } } - pub fn inner(&self) -> &T { - &self.perturbation - } - - pub fn inner_mut(&mut self) -> &mut T { - &mut self.perturbation - } - fn within_bounds(&self, chromosome: &SVector) -> bool { chromosome.iter() .zip(self.min_max.iter()) @@ -327,23 +319,33 @@ impl MutationPerturbation { } } - pub fn apply_to_mutations(base_perturbation: &mut dyn PerturbationOperator, apply: &mut dyn FnMut(&mut Self)) { - if let Some(mutation) = base_perturbation.as_any_mut().downcast_mut::>() { - apply(mutation); - } + pub fn apply_to_mutations( + base_perturbation: &mut dyn PerturbationOperator, + apply: &mut dyn FnMut(&mut MutationPerturbation) + ) { + apply_to_perturbations(base_perturbation, apply); + } +} - match base_perturbation.wrapped_mut() { - WrappedMut::Single => (), - WrappedMut::Wrapped(wrapped) => { - Self::apply_to_mutations(wrapped, apply); - }, - WrappedMut::ListWrapped(wrapped) => { - for wrapped in wrapped { - Self::apply_to_mutations(wrapped, apply); - } - } - }; +pub fn apply_to_perturbations>( + base_perturbation: &mut dyn PerturbationOperator, + apply: &mut dyn FnMut(&mut U) +) { + if let Some(mutation) = base_perturbation.as_any_mut().downcast_mut::() { + apply(mutation); } + + match base_perturbation.wrapped_mut() { + WrappedMut::Single => (), + WrappedMut::Wrapped(wrapped) => { + apply_to_perturbations(wrapped, apply); + }, + WrappedMut::ListWrapped(wrapped) => { + for wrapped in wrapped { + apply_to_perturbations(wrapped, apply); + } + } + }; } impl PerturbationOperator for MutationPerturbation {