Skip to content

Commit

Permalink
Add core::iter::Sum for Vector2d and Vector3d (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided authored Jul 25, 2024
1 parent 2e01dcc commit 1d97b4d
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, Vector3d};
use core::{
iter::FromIterator,
iter::{FromIterator, Sum},
ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign},
};

Expand Down Expand Up @@ -251,6 +251,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 + 'a,
{
/// 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 @@ -2,6 +2,7 @@

use super::{Component, Vector, Vector2d};
use crate::F32;
use core::iter::Sum;
use core::{
iter::FromIterator,
ops::{Add, AddAssign, Index, Mul, MulAssign, Sub, SubAssign},
Expand Down Expand Up @@ -268,6 +269,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 + 'a,
{
/// 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 1d97b4d

Please sign in to comment.