From e13b5df711e461401dbc0c7855b3620dbf6d8654 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Tue, 5 Dec 2023 18:38:43 +0100 Subject: [PATCH] fix crash if somehow train is too small to fit --- geom/src/polyline3queue.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/geom/src/polyline3queue.rs b/geom/src/polyline3queue.rs index 991bea2fe..88dc97341 100644 --- a/geom/src/polyline3queue.rs +++ b/geom/src/polyline3queue.rs @@ -129,9 +129,11 @@ impl Polyline3Queue { let mut accdist = self.dh; let mut lastp = self.front(); - distances.into_iter().map(move |dist| { + distances.into_iter().map_while(move |dist| { loop { - let newp = *self.data.get(self.frontcounter - counter).unwrap(); + let Some(newp) = self.data.get(self.frontcounter - counter).copied() else { + return None; + }; let d = lastp.distance(newp); if accdist + d >= dist { break; @@ -141,12 +143,12 @@ impl Polyline3Queue { counter -= 1; } - Follower { + Some(Follower { counter, headcounter: self.frontcounter, df: accdist - self.dh, dh: dist, - } + }) }) } } @@ -271,7 +273,7 @@ mod tests { let p = vec![v(3.0), v(1.0), v(0.0)]; let queue = Polyline3Queue::new(p.clone().into_iter(), v(3.25), 10.0); let mut followers = - queue.mk_followers(vec![0.0, 0.125, 0.125, 1.0, 1.5, 3.0, 3.125].into_iter()); + queue.mk_followers(vec![0.0, 0.125, 0.125, 1.0, 1.5, 3.0, 3.125, 100.0].into_iter()); assert_eq!( followers.next().unwrap(), Follower { @@ -335,5 +337,6 @@ mod tests { dh: 3.125, } ); + assert_eq!(followers.next(), None); } }