From 2fdb8aee6c36e6ad4ba83cee2861a7152f418aee Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Sat, 5 Oct 2024 00:02:26 +0200 Subject: [PATCH] feat(stepper,autorouter): get rid of Step::finish --- src/autorouter/compare_detours.rs | 44 +++++++------------------------ src/autorouter/execution.rs | 16 +++++------ src/autorouter/invoker.rs | 2 +- src/stepper.rs | 11 -------- 4 files changed, 19 insertions(+), 54 deletions(-) diff --git a/src/autorouter/compare_detours.rs b/src/autorouter/compare_detours.rs index 9b705b0..f4c6479 100644 --- a/src/autorouter/compare_detours.rs +++ b/src/autorouter/compare_detours.rs @@ -3,6 +3,7 @@ //! while providing access to navigation meshes, traces, ghost shapes, and //! obstacles encountered. +use core::task::Poll; use petgraph::graph::EdgeIndex; use crate::{ @@ -11,7 +12,7 @@ use crate::{ geometry::{primitive::PrimitiveShape, shape::MeasureLength}, graph::MakeRef, router::{navmesh::Navmesh, trace::TraceStepper}, - stepper::{Step, StepError}, + stepper::{PollStep, Step, StepError}, }; use super::{ @@ -20,23 +21,6 @@ use super::{ Autorouter, AutorouterError, AutorouterOptions, }; -pub enum CompareDetoursStatus { - Running, - Finished(f64, f64), -} - -impl TryInto<(f64, f64)> for CompareDetoursStatus { - type Error = (); - fn try_into(self) -> Result<(f64, f64), ()> { - match self { - CompareDetoursStatus::Running => Err(()), - CompareDetoursStatus::Finished(total_length1, total_length2) => { - Ok((total_length1, total_length2)) - } - } - } -} - pub struct CompareDetoursExecutionStepper { autoroute: AutorouteExecutionStepper, next_autoroute: Option, @@ -72,22 +56,17 @@ impl StepError for 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> - for CompareDetoursExecutionStepper -{ - fn step( +impl PollStep, (f64, f64)> for CompareDetoursExecutionStepper { + fn poll_step( &mut self, autorouter: &mut Autorouter, - ) -> Result { + ) -> Poll> { if self.done { - return Ok(CompareDetoursStatus::Finished( - self.total_length1, - self.total_length2, - )); + return Poll::Ready(Ok((self.total_length1, self.total_length2))); } match self.autoroute.step(autorouter)? { - AutorouteStatus::Running => Ok(CompareDetoursStatus::Running), + AutorouteStatus::Running => Poll::Pending, AutorouteStatus::Routed(band_termseg) => { let length = band_termseg .ref_(autorouter.board.layout().drawing()) @@ -99,22 +78,19 @@ impl Step, CompareDetoursStatus> self.total_length2 += length; } - Ok(CompareDetoursStatus::Running) + Poll::Pending } AutorouteStatus::Finished => { if let Some(next_autoroute) = self.next_autoroute.take() { autorouter.undo_autoroute_ratlines(vec![self.ratline1, self.ratline2])?; self.autoroute = next_autoroute; - Ok(CompareDetoursStatus::Running) + Poll::Pending } else { self.done = true; autorouter.undo_autoroute_ratlines(vec![self.ratline2, self.ratline1])?; - Ok(CompareDetoursStatus::Finished( - self.total_length1, - self.total_length2, - )) + Poll::Ready(Ok((self.total_length1, self.total_length2))) } } } diff --git a/src/autorouter/execution.rs b/src/autorouter/execution.rs index 1e533a8..f5b6fe5 100644 --- a/src/autorouter/execution.rs +++ b/src/autorouter/execution.rs @@ -10,7 +10,7 @@ use crate::{ use super::{ autoroute::{AutorouteExecutionStepper, AutorouteStatus}, - compare_detours::{CompareDetoursExecutionStepper, CompareDetoursStatus}, + compare_detours::CompareDetoursExecutionStepper, invoker::{Invoker, InvokerError}, measure_length::MeasureLengthExecutionStepper, place_via::PlaceViaExecutionStepper, @@ -59,15 +59,15 @@ impl ExecutionStepper { Poll::Ready("finished removing bands".to_string()) } ExecutionStepper::CompareDetours(compare_detours) => { - match compare_detours.step(autorouter)? { - CompareDetoursStatus::Running => Poll::Pending, - CompareDetoursStatus::Finished(total_length1, total_length2) => { - Poll::Ready(format!( + return compare_detours.poll_step(autorouter).map(|res| { + res.map(|(total_length1, total_length2)| { + format!( "total detour lengths are {} and {}", total_length1, total_length2 - )) - } - } + ) + }) + .map_err(Into::into) + }); } ExecutionStepper::MeasureLength(measure_length) => { let length = measure_length.doit(autorouter)?; diff --git a/src/autorouter/invoker.rs b/src/autorouter/invoker.rs index 74aaa74..0f70a77 100644 --- a/src/autorouter/invoker.rs +++ b/src/autorouter/invoker.rs @@ -10,7 +10,7 @@ use crate::{ drawing::graph::PrimitiveIndex, geometry::primitive::PrimitiveShape, router::{navmesh::Navmesh, trace::TraceStepper}, - stepper::{PollStep, Step}, + stepper::PollStep, }; use super::{ diff --git a/src/stepper.rs b/src/stepper.rs index 0c4909a..5e15753 100644 --- a/src/stepper.rs +++ b/src/stepper.rs @@ -6,17 +6,6 @@ pub trait StepError { pub trait Step: StepError { fn step(&mut self, context: &mut C) -> Result; - - fn finish(&mut self, context: &mut C) -> Result - where - S: TryInto, - { - loop { - if let Ok(outcome) = self.step(context)?.try_into() { - return Ok(outcome); - } - } - } } // Note that PollStep's `S` is usually not the same as Step's `S`.