From 902edc53be8ec5f5b34c68d04f593fe2784c31a5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 25 Mar 2021 11:10:15 +0000 Subject: [PATCH] geometry: Some code motion Signed-off-by: Ian Jackson --- base/geometry.rs | 66 ++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/base/geometry.rs b/base/geometry.rs index f056730b..9a126888 100644 --- a/base/geometry.rs +++ b/base/geometry.rs @@ -79,14 +79,45 @@ impl CheckedArithMul 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 { + pub fn promote(&self) -> PosC { self.map(|v| v as f64) } +} + +#[derive(Error,Debug,Copy,Clone,Serialize,Deserialize)] +pub struct PosCFromIteratorError; +display_as_debug!{PosCFromIteratorError} + +impl PosC { + #[throws(PosCFromIteratorError)] + pub fn from_iter>(i: I) -> Self { PosC( + i + .collect::>() + .into_inner() + .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() + )} +} + impl Add> for PosC { type Output = Result; #[throws(CoordinateOverflow)] @@ -164,37 +195,6 @@ impl Mean for PosC where T: Mean + Debug { } } -impl PosC { - pub fn promote(&self) -> PosC { self.map(|v| v as f64) } -} - -#[derive(Error,Debug,Copy,Clone,Serialize,Deserialize)] -pub struct PosCFromIteratorError; -display_as_debug!{PosCFromIteratorError} - -impl PosC { - #[throws(PosCFromIteratorError)] - pub fn from_iter>(i: I) -> Self { PosC( - i - .collect::>() - .into_inner() - .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() - )} -} - // ---------- Rect ---------- impl RectC { -- 2.30.2