chiark / gitweb /
utils: Provide PosC::from_iter
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 22:30:50 +0000 (22:30 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 22:30:50 +0000 (22:30 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/spec.rs

index c8d0e8ebbc3a7b8bf9970c59e82a30fabf533848..e1f7f9a6a76352070cd8104333c389cf25d0ff20 100644 (file)
@@ -321,6 +321,20 @@ pub mod pos_traits {
   impl PosC<Coord> {
     pub fn promote(&self) -> PosC<f64> { self.map(|v| v as f64) }
   }
+
+  #[derive(Error,Debug,Copy,Clone,Serialize,Deserialize)]
+  pub struct PosCFromIteratorError;
+  display_as_debug!{PosCFromIteratorError}
+
+  impl<T> PosC<T> {
+    #[throws(PosCFromIteratorError)]
+    pub fn from_iter<I: Iterator<Item=T>>(i: I) -> Self { PosC(
+      i
+        .collect::<ArrayVec<_>>()
+        .into_inner()
+        .map_err(|_| PosCFromIteratorError)?
+    )}
+  }
 }
 
 //---------- Implementation ----------