From 6af6c38f4035cbff21cc1db0983c20569139d2d7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 2 Apr 2021 22:16:24 +0100 Subject: [PATCH] geometry: Promote len2 Signed-off-by: Ian Jackson --- base/geometry.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/base/geometry.rs b/base/geometry.rs index 4040207d..ab5527aa 100644 --- a/base/geometry.rs +++ b/base/geometry.rs @@ -104,6 +104,15 @@ impl PosC { pub fn zero() -> Self where T: num_traits::Zero + Copy { PosC::both(::zero()) } + + #[throws(CoordinateOverflow)] + pub fn len2(self) -> f64 where PosC: PosPromote { + self.promote().coords.iter() + .try_fold(0., |b, &c| { + let c2 = c.checked_mul(c)?; + b.checked_add(c2) + })? + } } impl PosC where T: Copy { pub fn x(self) -> T { self.coords[0] } -- 2.30.2