From a9e42eef17ac73493d339aaafbcabdc602a12af9 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sat, 12 Oct 2024 00:38:50 +0200 Subject: [PATCH] refactor: make `Step`'s error an associated type This was suggested in https://codeberg.org/topola/topola/pulls/79. --- src/autorouter/autoroute.rs | 6 +++--- src/autorouter/compare_detours.rs | 4 +++- src/autorouter/execution.rs | 4 +++- src/bin/topola-egui/activity.rs | 10 ++++++---- src/bin/topola-egui/interaction.rs | 4 +++- src/router/astar.rs | 6 ++++-- src/router/route.rs | 6 +++--- src/stepper.rs | 8 +++++--- 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/autorouter/autoroute.rs b/src/autorouter/autoroute.rs index 83d5f24..9cec6c8 100644 --- a/src/autorouter/autoroute.rs +++ b/src/autorouter/autoroute.rs @@ -87,9 +87,9 @@ impl AutorouteExecutionStepper { } } -impl Step, AutorouteStatus, AutorouterError, ()> - for AutorouteExecutionStepper -{ +impl Step, AutorouteStatus, ()> for AutorouteExecutionStepper { + type Error = AutorouterError; + fn step(&mut self, autorouter: &mut Autorouter) -> Result { let Some(curr_ratline) = self.curr_ratline else { return Ok(AutorouteStatus::Finished); diff --git a/src/autorouter/compare_detours.rs b/src/autorouter/compare_detours.rs index 635e300..3c3011d 100644 --- a/src/autorouter/compare_detours.rs +++ b/src/autorouter/compare_detours.rs @@ -66,9 +66,11 @@ impl CompareDetoursExecutionStepper { // XXX: Do we really need this to be a stepper? We don't use at the moment, as sorting functions // aren't steppable either. It may be useful for debugging later on tho. -impl Step, CompareDetoursStatus, AutorouterError, (f64, f64)> +impl Step, CompareDetoursStatus, (f64, f64)> for CompareDetoursExecutionStepper { + type Error = AutorouterError; + fn step( &mut self, autorouter: &mut Autorouter, diff --git a/src/autorouter/execution.rs b/src/autorouter/execution.rs index 628986a..df6f272 100644 --- a/src/autorouter/execution.rs +++ b/src/autorouter/execution.rs @@ -74,7 +74,9 @@ impl ExecutionStepper { } } -impl Step, InvokerStatus, InvokerError, ()> for ExecutionStepper { +impl Step, InvokerStatus, ()> for ExecutionStepper { + type Error = InvokerError; + fn step(&mut self, invoker: &mut Invoker) -> Result { match self.step_catch_err(&mut invoker.autorouter) { Ok(InvokerStatus::Running) => Ok(InvokerStatus::Running), diff --git a/src/bin/topola-egui/activity.rs b/src/bin/topola-egui/activity.rs index 2475112..a5e299e 100644 --- a/src/bin/topola-egui/activity.rs +++ b/src/bin/topola-egui/activity.rs @@ -70,9 +70,9 @@ pub enum ActivityStepper { Execution(ExecutionStepper), } -impl Step, ActivityStatus, ActivityError, ()> - for ActivityStepper -{ +impl Step, ActivityStatus, ()> for ActivityStepper { + type Error = ActivityError; + fn step(&mut self, context: &mut ActivityContext) -> Result { match self { ActivityStepper::Interaction(interaction) => { @@ -152,9 +152,11 @@ impl ActivityStepperWithStatus { } } -impl Step, ActivityStatus, ActivityError, ()> +impl Step, ActivityStatus, ()> for ActivityStepperWithStatus { + type Error = ActivityError; + fn step(&mut self, context: &mut ActivityContext) -> Result { let status = self.activity.step(context)?; self.maybe_status = Some(status.clone()); diff --git a/src/bin/topola-egui/interaction.rs b/src/bin/topola-egui/interaction.rs index 7422fde..434152c 100644 --- a/src/bin/topola-egui/interaction.rs +++ b/src/bin/topola-egui/interaction.rs @@ -42,7 +42,9 @@ pub enum InteractionStepper { // - interactively moving a footprint. } -impl Step for InteractionStepper { +impl Step for InteractionStepper { + type Error = InteractionError; + fn step( &mut self, context: &mut InteractionContext, diff --git a/src/router/astar.rs b/src/router/astar.rs index 3c49fbe..ca51fc4 100644 --- a/src/router/astar.rs +++ b/src/router/astar.rs @@ -203,14 +203,16 @@ where } } -impl> - Step, AstarError, (K, Vec, R)> for Astar +impl> Step, (K, Vec, R)> + for Astar where G: GraphBase, G::NodeId: Eq + Hash, for<'a> &'a G: IntoEdges + MakeEdgeRef, K: Measure + Copy, { + type Error = AstarError; + fn step(&mut self, strategy: &mut S) -> Result, AstarError> { if let Some(curr_node) = self.maybe_curr_node { if self.is_probing { diff --git a/src/router/route.rs b/src/router/route.rs index f44043c..b2e14e9 100644 --- a/src/router/route.rs +++ b/src/router/route.rs @@ -73,9 +73,9 @@ impl RouteStepper { } } -impl<'a, R: AccessRules> Step, RouterStatus, AstarError, BandTermsegIndex> - for RouteStepper -{ +impl<'a, R: AccessRules> Step, RouterStatus, BandTermsegIndex> for RouteStepper { + type Error = AstarError; + fn step(&mut self, router: &mut Router) -> Result { let navcorder = Navcorder::new(router.layout_mut()); let target = self.astar.graph.destination(); diff --git a/src/stepper.rs b/src/stepper.rs index 385219d..01a8218 100644 --- a/src/stepper.rs +++ b/src/stepper.rs @@ -1,7 +1,9 @@ -pub trait Step, E, O> { - fn step(&mut self, context: &mut C) -> Result; +pub trait Step, O> { + type Error; - fn finish(&mut self, context: &mut C) -> Result { + fn step(&mut self, context: &mut C) -> Result; + + fn finish(&mut self, context: &mut C) -> Result { loop { if let Ok(outcome) = self.step(context)?.try_into() { return Ok(outcome);