Skip to main content

kcl_api/point/
convert.rs

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