diff --git a/src/quaternion.rs b/src/quaternion.rs index 7e1be14..0a740ad 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -274,6 +274,18 @@ impl From for (f32, f32, f32, f32) { } } +impl From<[f32; 4]> for Quaternion { + fn from(q: [f32; 4]) -> Quaternion { + Self::new(q[0], q[1], q[2], q[3]) + } +} + +impl From for [f32; 4] { + fn from(q: Quaternion) -> [f32; 4] { + q.to_array() + } +} + impl Mul for Quaternion { type Output = Self; @@ -453,4 +465,24 @@ mod tests { assert!((v1_r - v2).magnitude() < 1e-1); } + + #[test] + fn from_tuple() { + let quat: Quaternion = (1.0, 2.0, 3.0, 4.0).into(); + let (a, b, c, d) = quat.into(); + assert_eq!(a, 1.0); + assert_eq!(b, 2.0); + assert_eq!(c, 3.0); + assert_eq!(d, 4.0); + } + + #[test] + fn from_array() { + let quat: Quaternion = [1.0, 2.0, 3.0, 4.0].into(); + let quat: [f32; 4] = quat.into(); + assert_eq!(quat[0], 1.0); + assert_eq!(quat[1], 2.0); + assert_eq!(quat[2], 3.0); + assert_eq!(quat[3], 4.0); + } } diff --git a/src/vector/vector2d.rs b/src/vector/vector2d.rs index 9d62ae7..9b9ceae 100644 --- a/src/vector/vector2d.rs +++ b/src/vector/vector2d.rs @@ -101,6 +101,36 @@ where } } +impl From> for (C, C) +where + C: Component, +{ + fn from(vector: Vector2d) -> (C, C) { + (vector.x, vector.y) + } +} + +impl From<[C; 2]> for Vector2d +where + C: Component, +{ + fn from(vector: [C; 2]) -> Self { + Self { + x: vector[0], + y: vector[1], + } + } +} + +impl From> for [C; 2] +where + C: Component, +{ + fn from(vector: Vector2d) -> [C; 2] { + vector.to_array() + } +} + impl Index for Vector2d where C: Component, @@ -231,3 +261,30 @@ where defmt::write!(fmt, "({}, {})", self.x, self.y) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_tuple() { + let vec: Vector2d<_> = (1, 2).into(); + assert_eq!(vec[0], 1); + assert_eq!(vec[1], 2); + + let (x, y) = vec.into(); + assert_eq!(x, 1); + assert_eq!(y, 2); + } + + #[test] + fn from_array() { + let vec: Vector2d<_> = [1, 2].into(); + assert_eq!(vec[0], 1); + assert_eq!(vec[1], 2); + + let arr: [_; 2] = vec.into(); + assert_eq!(arr[0], 1); + assert_eq!(arr[1], 2); + } +} diff --git a/src/vector/vector3d.rs b/src/vector/vector3d.rs index aa929e9..3212c11 100644 --- a/src/vector/vector3d.rs +++ b/src/vector/vector3d.rs @@ -108,6 +108,37 @@ where } } +impl From> for (C, C, C) +where + C: Component, +{ + fn from(vector: Vector3d) -> (C, C, C) { + (vector.x, vector.y, vector.z) + } +} + +impl From<[C; 3]> for Vector3d +where + C: Component, +{ + fn from(vector: [C; 3]) -> Self { + Self { + x: vector[0], + y: vector[1], + z: vector[2], + } + } +} + +impl From> for [C; 3] +where + C: Component, +{ + fn from(vector: Vector3d) -> [C; 3] { + vector.to_array() + } +} + impl Index for Vector3d where C: Component, @@ -273,3 +304,34 @@ where defmt::write!(fmt, "({}, {}, {})", self.x, self.y, self.z) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_tuple() { + let vec: Vector3d<_> = (1, 2, 3).into(); + assert_eq!(vec[0], 1); + assert_eq!(vec[1], 2); + assert_eq!(vec[2], 3); + + let (x, y, z) = vec.into(); + assert_eq!(x, 1); + assert_eq!(y, 2); + assert_eq!(z, 3); + } + + #[test] + fn from_array() { + let vec: Vector3d<_> = [1, 2, 3].into(); + assert_eq!(vec[0], 1); + assert_eq!(vec[1], 2); + assert_eq!(vec[2], 3); + + let arr: [_; 3] = vec.into(); + assert_eq!(arr[0], 1); + assert_eq!(arr[1], 2); + assert_eq!(arr[2], 3); + } +}