kittycad_modeling_cmds/shared/point/
convert.rs

1use super::{Point2d, Point3d, Point4d};
2
3macro_rules! impl_convert {
4    ($typ:ident, $n:literal, $($i:ident),*) => {
5        impl<T> From<[T; $n]> for $typ<T> {
6            fn from([$($i, )*]: [T; $n]) -> Self {
7                Self { $($i, )* }
8            }
9        }
10
11        impl<T> From<$typ<T>> for [T; $n]{
12            fn from($typ{$($i, )*}: $typ<T>) -> Self {
13                [ $($i, )* ]
14            }
15        }
16    };
17}
18
19impl_convert!(Point2d, 2, x, y);
20impl_convert!(Point3d, 3, x, y, z);
21impl_convert!(Point4d, 4, x, y, z, w);
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26
27    #[test]
28    fn array_to_point() {
29        assert_eq!(Point2d { x: 1, y: 2 }, Point2d::from([1, 2]));
30    }
31
32    #[test]
33    fn point_to_array() {
34        let lhs: [u32; 2] = Point2d { x: 1, y: 2 }.into();
35        let rhs = [1u32, 2];
36        assert_eq!(lhs, rhs);
37    }
38}