From 7753675e6ac1947636335b172cd26ee6a962e363 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 27 Feb 2021 10:39:52 +0000 Subject: [PATCH] PosC::try_from_iter_2 Signed-off-by: Ian Jackson --- src/spec.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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 ---------- -- 2.30.2