Skip to content

Commit

Permalink
Add core::iter::Sum for Vector2d and Vector3d
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Jul 12, 2024
1 parent f8dd93d commit b408090
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/vector/vector2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::{Component, Vector};
use core::{
iter::FromIterator,
iter::{FromIterator, Sum},
ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign},
};

Expand Down Expand Up @@ -185,6 +185,52 @@ where
}
}

impl<C> Sum<Vector2d<C>> for Vector2d<C>
where
C: Component
{
/// Method which takes an iterator and generates `Self` from the elements by
/// "summing up" the items.
///
/// ## Example
/// ```
/// use micromath::vector::Vector2d;
/// let vectors = [
/// Vector2d { x: 1.0, y: 0.0 },
/// Vector2d { x: 0.0, y: 2.0 },
/// ];
/// let sum: Vector2d<f32> = vectors.iter().copied().sum();
/// assert_eq!(sum.x, 1.0);
/// assert_eq!(sum.y, 2.0);
/// ```
fn sum<I: Iterator<Item = Vector2d<C>>>(iter: I) -> Self {
iter.fold(Vector2d::default(), |prev, current| prev + current)
}
}

impl<'a, C> Sum<&'a Vector2d<C>> for Vector2d<C>
where
C: Component
{
/// Method which takes an iterator and generates `Self` from the elements by
/// "summing up" the items.
///
/// ## Example
/// ```
/// use micromath::vector::Vector2d;
/// let vectors = [
/// Vector2d { x: 1.0, y: 0.0 },
/// Vector2d { x: 0.0, y: 2.0 },
/// ];
/// let sum: Vector2d<f32> = vectors.iter().sum();
/// assert_eq!(sum.x, 1.0);
/// assert_eq!(sum.y, 2.0);
/// ```
fn sum<I: Iterator<Item = &'a Vector2d<C>>>(iter: I) -> Self {
iter.copied().sum()
}
}

impl From<I8x2> for F32x2 {
fn from(vector: I8x2) -> F32x2 {
Self {
Expand Down
49 changes: 49 additions & 0 deletions src/vector/vector3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::{
iter::FromIterator,
ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign},
};
use core::iter::Sum;

/// 3-dimensional XYZ vector of `i8` values
pub type I8x3 = Vector3d<i8>;
Expand Down Expand Up @@ -213,6 +214,54 @@ where
}
}

impl<C> Sum<Vector3d<C>> for Vector3d<C>
where
C: Component
{
/// Method which takes an iterator and generates `Self` from the elements by
/// "summing up" the items.
///
/// ## Example
/// ```
/// use micromath::vector::Vector3d;
/// let vectors = [
/// Vector3d { x: 1.0, y: 0.0, z: -2.0 },
/// Vector3d { x: 0.0, y: 2.0, z: -1.0 },
/// ];
/// let sum: Vector3d<f32> = vectors.iter().copied().sum();
/// assert_eq!(sum.x, 1.0);
/// assert_eq!(sum.y, 2.0);
/// assert_eq!(sum.z, -3.0);
/// ```
fn sum<I: Iterator<Item = Vector3d<C>>>(iter: I) -> Self {
iter.fold(Vector3d::default(), |prev, current| prev + current)
}
}

impl<'a, C> Sum<&'a Vector3d<C>> for Vector3d<C>
where
C: Component
{
/// Method which takes an iterator and generates `Self` from the elements by
/// "summing up" the items.
///
/// ## Example
/// ```
/// use micromath::vector::Vector3d;
/// let vectors = [
/// Vector3d { x: 1.0, y: 0.0, z: -2.0 },
/// Vector3d { x: 0.0, y: 2.0, z: -1.0 },
/// ];
/// let sum: Vector3d<f32> = vectors.iter().copied().sum();
/// assert_eq!(sum.x, 1.0);
/// assert_eq!(sum.y, 2.0);
/// assert_eq!(sum.z, -3.0);
/// ```
fn sum<I: Iterator<Item = &'a Vector3d<C>>>(iter: I) -> Self {
iter.copied().sum()
}
}

impl From<I8x3> for F32x3 {
fn from(vector: I8x3) -> F32x3 {
Self {
Expand Down

0 comments on commit b408090

Please sign in to comment.