chiark / gitweb /
PosC::try_from_iter_2
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Feb 2021 10:39:52 +0000 (10:39 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Feb 2021 10:40:28 +0000 (10:40 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/spec.rs

index a2f8e4992449cb9e73644db406dedc3310e240fb..cd85b09db04a8e0873241abf2aa3ceea69b80235 100644 (file)
@@ -255,16 +255,14 @@ pub mod pos_traits {
     type Output = Result<Self, CoordinateOverflow>;
     #[throws(CoordinateOverflow)]
     fn add(self, rhs: PosC<T>) -> PosC<T> {
-      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::<Result<ArrayVec<_>,_>>()?
-          .into_inner().unwrap()
-      )
+      )?
     }
   }
 
@@ -334,6 +332,19 @@ pub mod pos_traits {
         .map_err(|_| PosCFromIteratorError)?
     )}
   }
+
+  impl<T:Debug> PosC<T> {
+    /// Panics if the iterator doesn't yield exactly 2 elements
+    #[throws(E)]
+    pub fn try_from_iter_2<
+      E: Debug,
+      I: Iterator<Item=Result<T,E>>
+    >(i: I) -> Self { PosC(
+      i
+        .collect::<Result<ArrayVec<_>,E>>()?
+        .into_inner().unwrap()
+    )}
+  }
 }
 
 //---------- Implementation ----------