splot/
point.rs

1// point.rs
2//
3// Copyright (c) 2021-2024  Douglas P Lau
4//
5
6/// Data point
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct Point {
9    /// `X` value
10    pub x: f32,
11    /// `Y` value
12    pub y: f32,
13}
14
15/// Data which can represent a point
16pub trait IntoPoint: Clone + Copy + Into<Point> {}
17
18impl IntoPoint for Point {}
19
20macro_rules! impl_point_from {
21    ($val:ty) => {
22        impl From<$val> for Point {
23            fn from(item: $val) -> Self {
24                Point {
25                    x: item as f32,
26                    y: 0.0,
27                }
28            }
29        }
30        impl IntoPoint for $val {}
31
32        impl From<&$val> for Point {
33            fn from(item: &$val) -> Self {
34                Point {
35                    x: *item as f32,
36                    y: 0.0,
37                }
38            }
39        }
40        impl IntoPoint for &$val {}
41    };
42}
43
44impl_point_from!(f32);
45impl_point_from!(f64);
46impl_point_from!(isize);
47impl_point_from!(i8);
48impl_point_from!(i16);
49impl_point_from!(i32);
50impl_point_from!(i64);
51impl_point_from!(i128);
52
53macro_rules! impl_point_from_tuple {
54    ($val:ty) => {
55        impl From<($val, $val)> for Point {
56            fn from(item: ($val, $val)) -> Self {
57                Point {
58                    x: item.0 as f32,
59                    y: item.1 as f32,
60                }
61            }
62        }
63        impl IntoPoint for ($val, $val) {}
64
65        impl From<(&$val, &$val)> for Point {
66            fn from(item: (&$val, &$val)) -> Self {
67                Point {
68                    x: *item.0 as f32,
69                    y: *item.1 as f32,
70                }
71            }
72        }
73        impl IntoPoint for (&$val, &$val) {}
74    };
75}
76
77impl_point_from_tuple!(f32);
78impl_point_from_tuple!(f64);
79impl_point_from_tuple!(isize);
80impl_point_from_tuple!(i8);
81impl_point_from_tuple!(i16);
82impl_point_from_tuple!(i32);
83impl_point_from_tuple!(i64);
84impl_point_from_tuple!(i128);
85
86macro_rules! impl_point_from_arr {
87    ($val:ty) => {
88        impl From<[$val; 2]> for Point {
89            fn from(item: [$val; 2]) -> Self {
90                Point {
91                    x: item[0] as f32,
92                    y: item[1] as f32,
93                }
94            }
95        }
96        impl IntoPoint for [$val; 2] {}
97    };
98}
99
100impl_point_from_arr!(f32);
101impl_point_from_arr!(f64);
102impl_point_from_arr!(isize);
103impl_point_from_arr!(i8);
104impl_point_from_arr!(i16);
105impl_point_from_arr!(i32);
106impl_point_from_arr!(i64);
107impl_point_from_arr!(i128);