diff --git a/src/angle.rs b/src/angle.rs index 2d3d21c..fd02f61 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -17,6 +17,7 @@ use std::fmt; use std::f64; +use std::iter; use std::ops::*; use rand::{Rand, Rng}; @@ -72,6 +73,20 @@ macro_rules! impl_angle { } } + impl iter::Sum<$Angle> for $Angle { + #[inline] + fn sum>>(iter: I) -> $Angle { + iter.fold($Angle::zero(), Add::add) + } + } + + impl<'a, S: 'a + BaseFloat> iter::Sum<&'a $Angle> for $Angle { + #[inline] + fn sum>>(iter: I) -> $Angle { + iter.fold($Angle::zero(), Add::add) + } + } + impl Angle for $Angle { type Unitless = S; diff --git a/src/structure.rs b/src/structure.rs index 697df52..7ee159d 100644 --- a/src/structure.rs +++ b/src/structure.rs @@ -551,6 +551,8 @@ pub trait Angle where Self: Mul<::Unitless, Output = Self>, Self: Div::Unitless>, Self: Div<::Unitless, Output = Self>, + + Self: iter::Sum, { type Unitless: BaseFloat; diff --git a/tests/angle.rs b/tests/angle.rs index 02c21bb..58c8faa 100644 --- a/tests/angle.rs +++ b/tests/angle.rs @@ -20,7 +20,7 @@ extern crate cgmath; use cgmath::{Rad, Deg}; #[test] -fn conv() { +fn test_conv() { let angle: Rad<_> = Deg(-5.0f64).into(); let angle: Deg<_> = angle.into(); assert_ulps_eq!(&angle, &Deg(-5.0f64)); @@ -37,3 +37,23 @@ fn conv() { let angle: Rad<_> = angle.into(); assert_ulps_eq!(&angle, &Rad(30.0f64)); } + +mod rad { + use cgmath::Rad; + + #[test] + fn test_iter_sum() { + assert_eq!(Rad(2.0) + Rad(3.0) + Rad(4.0), [Rad(2.0), Rad(3.0), Rad(4.0)].iter().sum()); + assert_eq!(Rad(2.0) + Rad(3.0) + Rad(4.0), [Rad(2.0), Rad(3.0), Rad(4.0)].iter().cloned().sum()); + } +} + +mod deg { + use cgmath::Deg; + + #[test] + fn test_iter_sum() { + assert_eq!(Deg(2.0) + Deg(3.0) + Deg(4.0), [Deg(2.0), Deg(3.0), Deg(4.0)].iter().sum()); + assert_eq!(Deg(2.0) + Deg(3.0) + Deg(4.0), [Deg(2.0), Deg(3.0), Deg(4.0)].iter().cloned().sum()); + } +} diff --git a/tests/quaternion.rs b/tests/quaternion.rs index e27dbbf..89bd531 100644 --- a/tests/quaternion.rs +++ b/tests/quaternion.rs @@ -59,6 +59,7 @@ mod operators { let q3 = Quaternion::from(Euler { x: Rad(1f32), y: Rad(1f32), z: Rad(2f32) }); assert_eq!(q1 + q2 + q3, [q1, q2, q3].iter().sum()); + assert_eq!(q1 + q2 + q3, [q1, q2, q3].iter().cloned().sum()); } #[test] @@ -68,6 +69,7 @@ mod operators { let q3 = Quaternion::from(Euler { x: Rad(1f32), y: Rad(1f32), z: Rad(2f32) }); assert_eq!(q1 * q2 * q3, [q1, q2, q3].iter().product()); + assert_eq!(q1 * q2 * q3, [q1, q2, q3].iter().cloned().product()); } }