chiark / gitweb /
geometry: Some code motion
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Mar 2021 11:10:15 +0000 (11:10 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Mar 2021 11:10:15 +0000 (11:10 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
base/geometry.rs

index f056730b332dea56307bd10e1b4ede6f2ce172a5..9a126888d2b942d4bfd34bcf601927651db11442 100644 (file)
@@ -79,14 +79,45 @@ impl CheckedArithMul<f64> for f64 {
   checked_float!{checked_mul(, rhs: Self)  * rhs }
 }
 
-//---------- Pos ----------
-
 pub trait Mean { fn mean(&self, other: &Self) -> Self; }
 
 impl Mean for i32 { fn mean(&self, other: &Self) -> Self {
   ((*self as i64 + *other as i64) / 2) as i32
 } }
 
+//---------- Pos ----------
+
+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)?
+  )}
+}
+
+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()
+  )}
+}
+
 impl<T:CheckedArith> Add<PosC<T>> for PosC<T> {
   type Output = Result<Self, CoordinateOverflow>;
   #[throws(CoordinateOverflow)]
@@ -164,37 +195,6 @@ impl<T> Mean for PosC<T> where T: Mean + Debug {
   }
 }
 
-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)?
-  )}
-}
-
-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()
-  )}
-}
-
 // ---------- Rect ----------
 
 impl<T> RectC<T> {