From: Ian Jackson Date: Sun, 11 Oct 2020 10:18:10 +0000 (+0100) Subject: some_range X-Git-Tag: otter-0.2.0~681 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=76b3f83cc9e238ce985026458bfaec5a949d78af;p=otter.git some_range Signed-off-by: Ian Jackson --- diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index ffa8edf8..67209bb7 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -106,10 +106,20 @@ pub struct ParseError; #[error("Z coordinate range has end before start, cannot iterate")] pub struct RangeBackwards; +#[derive(Error,Clone,Copy,Debug)] +#[error("Z coordinate range has neither end, cannot iterate")] +pub struct TotallyUnboundedRange; + #[derive(Error,Debug,Copy,Clone,Serialize,Deserialize)] #[error("Z coordinate overflow")] pub struct Overflow; +#[derive(Error,Clone,Copy,Debug)] +pub enum LogicError { + #[error("{0}")] RangeTotallyUnbounded(#[from] TotallyUnboundedRange), + #[error("{0}")] RangeBackwards (#[from] RangeBackwards ), +} + //---------- Mutabel ---------- #[derive(Clone,Debug)] @@ -149,8 +159,8 @@ pub trait AddSubOffset { pub struct Sealed(()); -pub struct Inrement; -impl AddSubOffset for Inrement { +pub struct Increment; +impl AddSubOffset for Increment { fn init_delta(&self) -> LimbVal { DELTA } const CARRY_DELTA : LimbVal = ONE; const NEW_LIMBS : LimbVal = ZERO; @@ -207,7 +217,7 @@ impl Mutable { } #[throws(Overflow)] - pub fn increment(&mut self) -> ZCoord { self.addsub(&Inrement)? } + pub fn increment(&mut self) -> ZCoord { self.addsub(&Increment)? } #[throws(Overflow)] pub fn decrement(&mut self) -> ZCoord { self.addsub(&Decrement)? } @@ -346,10 +356,24 @@ impl ExactSizeIterator for IteratorCore { fn len(&self) -> usize { return usize::MAX } } +pub type BoxedIterator = Box>; + impl Mutable { pub fn iter(self, aso: ASO) -> IteratorCore { IteratorCore { current: self, aso } } + #[throws(LogicError)] + pub fn some_range(a: Option<&Mutable>, b: Option<&Mutable>, count: u32) + -> BoxedIterator { + fn mk>(x: T) -> BoxedIterator + { Box::new(x) } + match (a, b) { + (None, None ) => throw!(TotallyUnboundedRange), + (Some(a), None ) => mk( a.clone().iter(Increment) ), + (None, Some(b)) => mk( b.clone().iter(Decrement) ), + (Some(a), Some(b)) => mk( Mutable::range_upto(&a,&b,count)? ), + } + } } //---------- main features of a Zcoord ---------- @@ -644,7 +668,7 @@ mod test { assert_eq!(got.to_string(), exp); self } - fn tinc(self, exp: &str) -> Self { self.tincdec(exp, Inrement) } + fn tinc(self, exp: &str) -> Self { self.tincdec(exp, Increment) } fn tdec(self, exp: &str) -> Self { self.tincdec(exp, Decrement) } } let start : ZCoord = Default::default();