From e08a59cd747fcd8cb565bcbd4d8e3b29b00aa628 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Tue, 1 Oct 2024 19:07:40 +0200 Subject: [PATCH] stepper: add `Abort` trait to abort steppers --- src/autorouter/autoroute.rs | 2 +- src/autorouter/command.rs | 2 +- src/autorouter/compare_detours.rs | 2 +- src/autorouter/invoker.rs | 2 +- src/bin/topola-egui/activity.rs | 24 ++++++++++++++++-------- src/bin/topola-egui/menu_bar.rs | 8 ++++---- src/bin/topola-egui/routing.rs | 4 ---- src/lib.rs | 10 +++++++--- src/router/astar.rs | 2 +- src/router/route.rs | 2 +- src/router/router.rs | 2 +- src/router/trace.rs | 6 ++---- src/router/tracer.rs | 2 +- src/{step.rs => stepper.rs} | 6 +++++- 14 files changed, 42 insertions(+), 32 deletions(-) delete mode 100644 src/bin/topola-egui/routing.rs rename src/{step.rs => stepper.rs} (79%) diff --git a/src/autorouter/autoroute.rs b/src/autorouter/autoroute.rs index e7a4744..0884881 100644 --- a/src/autorouter/autoroute.rs +++ b/src/autorouter/autoroute.rs @@ -5,7 +5,7 @@ use crate::{ drawing::{band::BandTermsegIndex, graph::PrimitiveIndex}, geometry::primitive::PrimitiveShape, router::{navmesh::Navmesh, route::RouteStepper, trace::TraceStepper, Router, RouterStatus}, - step::Step, + stepper::Step, }; use super::{ diff --git a/src/autorouter/command.rs b/src/autorouter/command.rs index cae54de..f730d34 100644 --- a/src/autorouter/command.rs +++ b/src/autorouter/command.rs @@ -1,7 +1,7 @@ use enum_dispatch::enum_dispatch; use serde::{Deserialize, Serialize}; -use crate::{board::mesadata::AccessMesadata, layout::via::ViaWeight, step::Step}; +use crate::{board::mesadata::AccessMesadata, layout::via::ViaWeight, stepper::Step}; use super::{ autoroute::{AutorouteExecutionStepper, AutorouteStatus}, diff --git a/src/autorouter/compare_detours.rs b/src/autorouter/compare_detours.rs index 1deb5c6..e06dbc6 100644 --- a/src/autorouter/compare_detours.rs +++ b/src/autorouter/compare_detours.rs @@ -6,7 +6,7 @@ use crate::{ geometry::{primitive::PrimitiveShape, shape::MeasureLength}, graph::MakeRef, router::{navmesh::Navmesh, trace::TraceStepper}, - step::Step, + stepper::Step, }; use super::{ diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index e623ea5..53f1043 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -9,7 +9,7 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{navmesh::Navmesh, trace::TraceStepper}, - step::Step, + stepper::Step, }; use super::{ diff --git a/src/bin/topola-egui/activity.rs b/src/bin/topola-egui/activity.rs index a231707..d78ec32 100644 --- a/src/bin/topola-egui/activity.rs +++ b/src/bin/topola-egui/activity.rs @@ -12,7 +12,7 @@ use topola::{ geometry::primitive::PrimitiveShape, router::{navmesh::Navmesh, trace::TraceStepper}, specctra::mesadata::SpecctraMesadata, - step::Step, + stepper::{Abort, Step}, }; #[derive(Debug, Clone)] @@ -57,16 +57,24 @@ impl Step, ActivityStatus, ActivityError, ()> for Acti invoker: &mut Invoker, ) -> Result { match self { - ActivityStepper::Execution(execute) => Ok(execute.step(invoker)?.into()), + ActivityStepper::Execution(execution) => Ok(execution.step(invoker)?.into()), } } } +impl Abort> for ActivityStepper { + fn abort(&mut self, invoker: &mut Invoker) { + match self { + ActivityStepper::Execution(execution) => execution.finish(invoker), // TODO. + }; + } +} + impl GetMaybeNavmesh for ActivityStepper { /// Implemented manually instead of with `enum_dispatch` because it doesn't work across crates. fn maybe_navmesh(&self) -> Option<&Navmesh> { match self { - ActivityStepper::Execution(execute) => execute.maybe_navmesh(), + ActivityStepper::Execution(execution) => execution.maybe_navmesh(), } } } @@ -75,7 +83,7 @@ impl GetMaybeTrace for ActivityStepper { /// Implemented manually instead of with `enum_dispatch` because it doesn't work across crates. fn maybe_trace(&self) -> Option<&TraceStepper> { match self { - ActivityStepper::Execution(execute) => execute.maybe_trace(), + ActivityStepper::Execution(execution) => execution.maybe_trace(), } } } @@ -84,7 +92,7 @@ impl GetGhosts for ActivityStepper { /// Implemented manually instead of with `enum_dispatch` because it doesn't work across crates. fn ghosts(&self) -> &[PrimitiveShape] { match self { - ActivityStepper::Execution(execute) => execute.ghosts(), + ActivityStepper::Execution(execution) => execution.ghosts(), } } } @@ -93,7 +101,7 @@ impl GetObstacles for ActivityStepper { /// Implemented manually instead of with `enum_dispatch` because it doesn't work across crates. fn obstacles(&self) -> &[PrimitiveIndex] { match self { - ActivityStepper::Execution(execute) => execute.obstacles(), + ActivityStepper::Execution(execution) => execution.obstacles(), } } } @@ -104,9 +112,9 @@ pub struct ActivityWithStatus { } impl ActivityWithStatus { - pub fn new_execute(execute: ExecutionStepper) -> ActivityWithStatus { + pub fn new_execution(execution: ExecutionStepper) -> ActivityWithStatus { Self { - activity: ActivityStepper::Execution(execute), + activity: ActivityStepper::Execution(execution), maybe_status: None, } } diff --git a/src/bin/topola-egui/menu_bar.rs b/src/bin/topola-egui/menu_bar.rs index 18fcd07..380f3a1 100644 --- a/src/bin/topola-egui/menu_bar.rs +++ b/src/bin/topola-egui/menu_bar.rs @@ -300,7 +300,7 @@ impl MenuBar { ) { let selection = overlay.selection().clone(); overlay.clear_selection(); - maybe_activity.insert(ActivityWithStatus::new_execute( + maybe_activity.insert(ActivityWithStatus::new_execution( invoker.execute_stepper(Command::Autoroute( selection.pin_selection, self.autorouter_options, @@ -319,7 +319,7 @@ impl MenuBar { ) { let selection = overlay.selection().clone(); overlay.clear_selection(); - maybe_activity.insert(ActivityWithStatus::new_execute( + maybe_activity.insert(ActivityWithStatus::new_execution( invoker.execute_stepper(Command::RemoveBands( selection.band_selection, ))?, @@ -336,7 +336,7 @@ impl MenuBar { ) { let selection = overlay.selection().clone(); overlay.clear_selection(); - maybe_activity.insert(ActivityWithStatus::new_execute( + maybe_activity.insert(ActivityWithStatus::new_execution( invoker.execute_stepper(Command::MeasureLength( selection.band_selection, ))?, @@ -361,7 +361,7 @@ impl MenuBar { ) { let selection = overlay.selection().clone(); overlay.clear_selection(); - maybe_activity.insert(ActivityWithStatus::new_execute( + maybe_activity.insert(ActivityWithStatus::new_execution( invoker.execute_stepper(Command::CompareDetours( selection.pin_selection, self.autorouter_options, diff --git a/src/bin/topola-egui/routing.rs b/src/bin/topola-egui/routing.rs deleted file mode 100644 index b0e1dcc..0000000 --- a/src/bin/topola-egui/routing.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub struct RouteInteractionStepper { - route: RouteStepper, - options: RouterOptions, -} diff --git a/src/lib.rs b/src/lib.rs index 90d0b78..367b735 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,9 @@ -#![doc(html_favicon_url = "https://codeberg.org/topola/topola/raw/commit/e1b56875edf039aab9f41868826bcd3a92097133/assets/favicon.ico")] -#![doc(html_logo_url = "https://codeberg.org/topola/topola/raw/commit/e1b56875edf039aab9f41868826bcd3a92097133/assets/logo.svg")] +#![doc( + html_favicon_url = "https://codeberg.org/topola/topola/raw/commit/e1b56875edf039aab9f41868826bcd3a92097133/assets/favicon.ico" +)] +#![doc( + html_logo_url = "https://codeberg.org/topola/topola/raw/commit/e1b56875edf039aab9f41868826bcd3a92097133/assets/logo.svg" +)] #![cfg_attr(not(feature = "disable_contracts"), feature(try_blocks))] pub mod graph; @@ -12,5 +16,5 @@ pub mod layout; pub mod math; pub mod router; pub mod specctra; -pub mod step; +pub mod stepper; pub mod triangulation; diff --git a/src/router/astar.rs b/src/router/astar.rs index 4afbeb9..2d6aec0 100644 --- a/src/router/astar.rs +++ b/src/router/astar.rs @@ -15,7 +15,7 @@ use thiserror::Error; use std::cmp::Ordering; -use crate::step::Step; +use crate::stepper::Step; #[derive(Copy, Clone, Debug)] pub struct MinScored(pub K, pub T); diff --git a/src/router/route.rs b/src/router/route.rs index 91ba68c..5e64711 100644 --- a/src/router/route.rs +++ b/src/router/route.rs @@ -10,7 +10,7 @@ use crate::{ tracer::Tracer, Router, RouterAstarStrategy, RouterError, RouterStatus, }, - step::Step, + stepper::Step, }; pub struct RouteStepper { diff --git a/src/router/router.rs b/src/router/router.rs index bed16ab..44e2827 100644 --- a/src/router/router.rs +++ b/src/router/router.rs @@ -19,7 +19,7 @@ use crate::{ }, graph::{GetPetgraphIndex, MakeRef}, layout::Layout, - step::{Step, StepBack}, + stepper::{Step, StepBack}, }; use super::{ diff --git a/src/router/trace.rs b/src/router/trace.rs index bcb38a4..644a3a6 100644 --- a/src/router/trace.rs +++ b/src/router/trace.rs @@ -9,7 +9,7 @@ use crate::{ head::{BareHead, CaneHead, Head}, rules::AccessRules, }, - step::{Step, StepBack}, + stepper::{Step, StepBack}, }; use super::{ @@ -129,9 +129,7 @@ impl<'a, 'b, R: AccessRules> Step, TracerStatus, Tra } } -impl<'a, R: AccessRules> StepBack, TracerStatus, TracerException, ()> - for TraceStepper -{ +impl<'a, R: AccessRules> StepBack, TracerStatus, TracerException> for TraceStepper { #[debug_ensures(self.path.len() == old(self.path.len() - 1))] fn step_back(&mut self, tracer: &mut Tracer<'a, R>) -> Result { if let Head::Cane(head) = self.head { diff --git a/src/router/tracer.rs b/src/router/tracer.rs index 7ed24b3..b63b93a 100644 --- a/src/router/tracer.rs +++ b/src/router/tracer.rs @@ -4,7 +4,7 @@ use thiserror::Error; use crate::{ drawing::{band::BandTermsegIndex, dot::FixedDotIndex, rules::AccessRules}, layout::Layout, - step::{Step, StepBack}, + stepper::{Step, StepBack}, }; use super::{ diff --git a/src/step.rs b/src/stepper.rs similarity index 79% rename from src/step.rs rename to src/stepper.rs index e4a5171..385219d 100644 --- a/src/step.rs +++ b/src/stepper.rs @@ -10,6 +10,10 @@ pub trait Step, E, O> { } } -pub trait StepBack, E, O> { +pub trait StepBack { fn step_back(&mut self, context: &mut C) -> Result; } + +pub trait Abort { + fn abort(&mut self, context: &mut C); +}