@@ -5,7 +5,9 @@ use std::fmt::Debug;
55use std:: ops:: { Add , AddAssign , Mul , MulAssign , Sub , SubAssign } ;
66
77macro_rules! point_impl {
8- ( $( #[ $m: meta] ) * $v: vis struct $s: ident{ $( $f: ident) ,+} ) => {
8+ ( $n: literal, $tuple: tt =>
9+ $( #[ $m: meta] ) * $v: vis struct $s: ident{ $( $i: tt => $f: ident) ,+}
10+ ) => {
911 #[ derive( Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Default ) ]
1012 $( #[ $m] ) * $v struct $s<T : Number > {
1113 $( pub $f: T , ) +
@@ -108,12 +110,44 @@ macro_rules! point_impl {
108110 $( self . $f -= rhs. $f; ) +
109111 }
110112 }
113+
114+ impl <T : Number > From <[ T ; $n] > for $s<T > {
115+ #[ inline]
116+ fn from( arr: [ T ; $n] ) -> Self {
117+ Self { $(
118+ $f: arr[ $i] ,
119+ ) +}
120+ }
121+ }
122+
123+ impl <T : Number > From <$tuple> for $s<T > {
124+ #[ inline]
125+ fn from( arr: $tuple) -> Self {
126+ Self { $(
127+ $f: arr. $i,
128+ ) +}
129+ }
130+ }
131+
132+ impl <T : Number > From <$s<T >> for [ T ; $n] {
133+ #[ inline]
134+ fn from( value: $s<T >) -> Self {
135+ [ $( value. $f) ,+]
136+ }
137+ }
138+
139+ impl <T : Number > From <$s<T >> for $tuple {
140+ #[ inline]
141+ fn from( value: $s<T >) -> Self {
142+ ( $( value. $f) ,+)
143+ }
144+ }
111145 } ;
112146}
113147
114- point_impl ! {
148+ point_impl ! { 2 , ( T , T ) =>
115149 /// Struct representing a 2D point or vector.
116- pub struct Point2D { x , y}
150+ pub struct Point2D { 0 => x , 1 => y}
117151}
118152
119153impl < T : Signed > Point2D < T > {
@@ -155,7 +189,7 @@ impl<T: Signed> Point2D<T> {
155189 }
156190}
157191
158- point_impl ! {
192+ point_impl ! { 3 , ( T , T , T ) =>
159193 /// Struct representing a 3D point or vector.
160- pub struct Point3D { x, y , z}
194+ pub struct Point3D { 0 => x, 1 => y , 2 => z}
161195}
0 commit comments