Skip to content

Commit

Permalink
Merge pull request #4 from rlane/default
Browse files Browse the repository at this point in the history
Implement Default for vector and matrices
  • Loading branch information
polymonster authored Sep 12, 2023
2 parents 123761d + 6aa37fb commit 0dded2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ macro_rules! mat_impl {
}
}

impl<T> Default for $MatN<T> where T: Number {
fn default() -> Self {
Self::identity()
}
}

impl<T> Eq for $MatN<T> where T: Eq {}
impl<T> PartialEq for $MatN<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,12 @@ macro_rules! vec_impl {
}
}

impl<T> Default for $VecN<T> where T: Number {
fn default() -> Self {
Self::zero()
}
}

//
// ops
//
Expand Down
13 changes: 13 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,13 @@ fn perp_product() {
assert_eq!(pp, vec2f(-0.5, 0.5));
}

#[test]
fn default() {
let vz : Vec3f = Default::default();
let expected = vec3f(0.0, 0.0, 0.0);
assert_eq!(vz, expected);
}

#[test]
fn zero() {
let vz = Vec3f::zero();
Expand Down Expand Up @@ -914,6 +921,12 @@ fn matrix_get_rows_columns() {
assert_eq!(m34.get_column(2), vec3f(2.0, 6.0, 10.0));
}

#[test]
fn matrix_default() {
let m4 : Mat4<f32> = Default::default();
assert_eq!(m4, Mat4f::identity());
}

#[test]
fn matrix_zero() {
let m4 = Mat4f::zero();
Expand Down

0 comments on commit 0dded2c

Please sign in to comment.