From: Ian Jackson Date: Sat, 27 Feb 2021 10:39:52 +0000 (+0000) Subject: PosC::try_from_iter_2 X-Git-Tag: otter-0.4.0~330 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7753675e6ac1947636335b172cd26ee6a962e363;p=otter.git PosC::try_from_iter_2 Signed-off-by: Ian Jackson --- diff --git a/src/spec.rs b/src/spec.rs index a2f8e499..cd85b09d 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -255,16 +255,14 @@ pub mod pos_traits { type Output = Result; #[throws(CoordinateOverflow)] fn add(self, rhs: PosC) -> PosC { - PosC( + PosC::try_from_iter_2( itertools::zip_eq( self.0.iter().cloned(), rhs .0.iter().cloned(), ).map( |(a,b)| a.checked_add(b) ) - .collect::,_>>()? - .into_inner().unwrap() - ) + )? } } @@ -334,6 +332,19 @@ pub mod pos_traits { .map_err(|_| PosCFromIteratorError)? )} } + + impl PosC { + /// Panics if the iterator doesn't yield exactly 2 elements + #[throws(E)] + pub fn try_from_iter_2< + E: Debug, + I: Iterator> + >(i: I) -> Self { PosC( + i + .collect::,E>>()? + .into_inner().unwrap() + )} + } } //---------- Implementation ----------