feat(stepper,autorouter): get rid of Step::finish

This commit is contained in:
Alain Emilia Anna Zscheile 2024-10-05 00:02:26 +02:00
parent 24c7f66b04
commit 2fdb8aee6c
4 changed files with 19 additions and 54 deletions

View File

@ -3,6 +3,7 @@
//! while providing access to navigation meshes, traces, ghost shapes, and //! while providing access to navigation meshes, traces, ghost shapes, and
//! obstacles encountered. //! obstacles encountered.
use core::task::Poll;
use petgraph::graph::EdgeIndex; use petgraph::graph::EdgeIndex;
use crate::{ use crate::{
@ -11,7 +12,7 @@ use crate::{
geometry::{primitive::PrimitiveShape, shape::MeasureLength}, geometry::{primitive::PrimitiveShape, shape::MeasureLength},
graph::MakeRef, graph::MakeRef,
router::{navmesh::Navmesh, trace::TraceStepper}, router::{navmesh::Navmesh, trace::TraceStepper},
stepper::{Step, StepError}, stepper::{PollStep, Step, StepError},
}; };
use super::{ use super::{
@ -20,23 +21,6 @@ use super::{
Autorouter, AutorouterError, AutorouterOptions, 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 { pub struct CompareDetoursExecutionStepper {
autoroute: AutorouteExecutionStepper, autoroute: AutorouteExecutionStepper,
next_autoroute: Option<AutorouteExecutionStepper>, next_autoroute: Option<AutorouteExecutionStepper>,
@ -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 // 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. // aren't steppable either. It may be useful for debugging later on tho.
impl<M: AccessMesadata> Step<Autorouter<M>, CompareDetoursStatus> impl<M: AccessMesadata> PollStep<Autorouter<M>, (f64, f64)> for CompareDetoursExecutionStepper {
for CompareDetoursExecutionStepper fn poll_step(
{
fn step(
&mut self, &mut self,
autorouter: &mut Autorouter<M>, autorouter: &mut Autorouter<M>,
) -> Result<CompareDetoursStatus, AutorouterError> { ) -> Poll<Result<(f64, f64), AutorouterError>> {
if self.done { if self.done {
return Ok(CompareDetoursStatus::Finished( return Poll::Ready(Ok((self.total_length1, self.total_length2)));
self.total_length1,
self.total_length2,
));
} }
match self.autoroute.step(autorouter)? { match self.autoroute.step(autorouter)? {
AutorouteStatus::Running => Ok(CompareDetoursStatus::Running), AutorouteStatus::Running => Poll::Pending,
AutorouteStatus::Routed(band_termseg) => { AutorouteStatus::Routed(band_termseg) => {
let length = band_termseg let length = band_termseg
.ref_(autorouter.board.layout().drawing()) .ref_(autorouter.board.layout().drawing())
@ -99,22 +78,19 @@ impl<M: AccessMesadata> Step<Autorouter<M>, CompareDetoursStatus>
self.total_length2 += length; self.total_length2 += length;
} }
Ok(CompareDetoursStatus::Running) Poll::Pending
} }
AutorouteStatus::Finished => { AutorouteStatus::Finished => {
if let Some(next_autoroute) = self.next_autoroute.take() { if let Some(next_autoroute) = self.next_autoroute.take() {
autorouter.undo_autoroute_ratlines(vec![self.ratline1, self.ratline2])?; autorouter.undo_autoroute_ratlines(vec![self.ratline1, self.ratline2])?;
self.autoroute = next_autoroute; self.autoroute = next_autoroute;
Ok(CompareDetoursStatus::Running) Poll::Pending
} else { } else {
self.done = true; self.done = true;
autorouter.undo_autoroute_ratlines(vec![self.ratline2, self.ratline1])?; autorouter.undo_autoroute_ratlines(vec![self.ratline2, self.ratline1])?;
Ok(CompareDetoursStatus::Finished( Poll::Ready(Ok((self.total_length1, self.total_length2)))
self.total_length1,
self.total_length2,
))
} }
} }
} }

View File

@ -10,7 +10,7 @@ use crate::{
use super::{ use super::{
autoroute::{AutorouteExecutionStepper, AutorouteStatus}, autoroute::{AutorouteExecutionStepper, AutorouteStatus},
compare_detours::{CompareDetoursExecutionStepper, CompareDetoursStatus}, compare_detours::CompareDetoursExecutionStepper,
invoker::{Invoker, InvokerError}, invoker::{Invoker, InvokerError},
measure_length::MeasureLengthExecutionStepper, measure_length::MeasureLengthExecutionStepper,
place_via::PlaceViaExecutionStepper, place_via::PlaceViaExecutionStepper,
@ -59,15 +59,15 @@ impl ExecutionStepper {
Poll::Ready("finished removing bands".to_string()) Poll::Ready("finished removing bands".to_string())
} }
ExecutionStepper::CompareDetours(compare_detours) => { ExecutionStepper::CompareDetours(compare_detours) => {
match compare_detours.step(autorouter)? { return compare_detours.poll_step(autorouter).map(|res| {
CompareDetoursStatus::Running => Poll::Pending, res.map(|(total_length1, total_length2)| {
CompareDetoursStatus::Finished(total_length1, total_length2) => { format!(
Poll::Ready(format!(
"total detour lengths are {} and {}", "total detour lengths are {} and {}",
total_length1, total_length2 total_length1, total_length2
)) )
} })
} .map_err(Into::into)
});
} }
ExecutionStepper::MeasureLength(measure_length) => { ExecutionStepper::MeasureLength(measure_length) => {
let length = measure_length.doit(autorouter)?; let length = measure_length.doit(autorouter)?;

View File

@ -10,7 +10,7 @@ use crate::{
drawing::graph::PrimitiveIndex, drawing::graph::PrimitiveIndex,
geometry::primitive::PrimitiveShape, geometry::primitive::PrimitiveShape,
router::{navmesh::Navmesh, trace::TraceStepper}, router::{navmesh::Navmesh, trace::TraceStepper},
stepper::{PollStep, Step}, stepper::PollStep,
}; };
use super::{ use super::{

View File

@ -6,17 +6,6 @@ pub trait StepError {
pub trait Step<C, S>: StepError { pub trait Step<C, S>: StepError {
fn step(&mut self, context: &mut C) -> Result<S, Self::Error>; fn step(&mut self, context: &mut C) -> Result<S, Self::Error>;
fn finish<O>(&mut self, context: &mut C) -> Result<O, Self::Error>
where
S: TryInto<O>,
{
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`. // Note that PollStep's `S` is usually not the same as Step's `S`.