From 703fcb2e7684976addb2eac1956724ef8abe0bf9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 21 Nov 2020 01:46:27 +0000 Subject: [PATCH] wip MutateLast and tests Signed-off-by: Ian Jackson --- zcoord/zcoord.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index d256178a..02dc4dd1 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -307,6 +307,20 @@ impl MutateReturn for MutateFirst { } } +#[derive(Debug)] +pub struct MutateLast; +impl MutateReturn for MutateLast { + fn op U> + (x: &mut T, m: M, o: O) -> U + { + let u = o(x); + m(x); + u + } +} + #[derive(Debug)] pub struct IteratorCore { current: Mutable, @@ -440,7 +454,8 @@ impl Mutable { (None, Some(b)) => mk({ let mut first = b.clone(); first.addsub(&Decrement).unwrap(); - Mutable::range_upto(&first,&b,count)? + let (current, aso) = Mutable::range_core(&first, &b, count-1)?; + IteratorCore { current, aso, mr: MutateLast }.take(count as usize) }), } } @@ -834,4 +849,36 @@ mod test { it.nxt("3333333334_0000000030"); assert_eq!(it.i.next(), None); } + + #[test] + fn some_range(){ + struct It { + i: BoxedIterator, + last: Option, + } + #[throws(LogicError)] + fn mkr(a: Option<&str>, b: Option<&str>, count: RangeCount) -> It { + let a = a.map(|s:&str| s.parse::().unwrap().clone_mut()); + let b = b.map(|s:&str| s.parse::().unwrap().clone_mut()); + let last = a.as_ref().map(|m| m.repack().unwrap()); + let i = Mutable::some_range(a.as_ref(), b.as_ref(), count)?; + It { i, last } + }; + impl It { + fn nxt(&mut self, exp: Option<&str>) { + let got = self.i.next(); + let got_s = got.map(|s| s.to_string()); + assert_eq!(got_s, exp); + if let (Some(got), Some(exp)) = (got, exp) { + assert_eq!(got, bf(exp)); + if let Some(last) = self.last { assert!(got > last); } + } + self.last = got.clone(); + } + } + let mut it = mkr(Some("300000000"),Some("400000000"),3).unwrap(); + it.nxt(Some("3e0000000")); + it.nxt(Some("3g0000000")); + it.nxt(Some("3p0000000")); + } } -- 2.30.2