@@ -12,8 +12,6 @@ macro_rules! impl_vector_unary_operator {
12
12
(
13
13
// Name of the vector type.
14
14
$Vector: ty,
15
- // Type of each individual component, for example `i32`.
16
- $Scalar: ty,
17
15
// Names of the components, with parentheses, for example `(x, y)`.
18
16
( $( $components: ident) ,* ) ,
19
17
// Name of the operator trait, for example `Neg`.
@@ -38,8 +36,6 @@ macro_rules! impl_vector_vector_binary_operator {
38
36
(
39
37
// Name of the vector type.
40
38
$Vector: ty,
41
- // Type of each individual component, for example `i32`.
42
- $Scalar: ty,
43
39
// Names of the components, with parentheses, for example `(x, y)`.
44
40
( $( $components: ident) ,* ) ,
45
41
// Name of the operator trait, for example `Add`.
@@ -119,8 +115,6 @@ macro_rules! impl_vector_vector_assign_operator {
119
115
(
120
116
// Name of the vector type.
121
117
$Vector: ty,
122
- // Type of each individual component, for example `i32`.
123
- $Scalar: ty,
124
118
// Names of the components, with parentheses, for example `(x, y)`.
125
119
( $( $components: ident) ,* ) ,
126
120
// Name of the operator trait, for example `AddAssign`.
@@ -163,6 +157,38 @@ macro_rules! impl_vector_scalar_assign_operator {
163
157
}
164
158
}
165
159
160
+ /// Implements a reduction (sum or product) over an iterator of vectors.
161
+ macro_rules! impl_iter_vector_reduction {
162
+ (
163
+ // Name of the vector type.
164
+ $Vector: ty,
165
+ // Name of the reduction trait: `Sum` or `Product`.
166
+ $Operator: ident,
167
+ // Name of the function on the operator trait, for example `add`.
168
+ $func: ident
169
+ ) => {
170
+ impl std:: iter:: $Operator<Self > for $Vector {
171
+ #[ doc = concat!( "Element-wise " , stringify!( $func) , " of all vectors in the iterator." ) ]
172
+ fn $func<I >( iter: I ) -> Self
173
+ where
174
+ I : Iterator <Item = Self >,
175
+ {
176
+ Self :: from_glam( iter. map( Self :: to_glam) . $func( ) )
177
+ }
178
+ }
179
+
180
+ impl <' a> std:: iter:: $Operator<& ' a Self > for $Vector {
181
+ #[ doc = concat!( "Element-wise " , stringify!( $func) , " of all vectors in the iterator." ) ]
182
+ fn $func<I >( iter: I ) -> Self
183
+ where
184
+ I : Iterator <Item = & ' a Self >,
185
+ {
186
+ Self :: from_glam( iter. map( |x| Self :: to_glam( * x) ) . $func( ) )
187
+ }
188
+ }
189
+ } ;
190
+ }
191
+
166
192
/// Implements all common arithmetic operators on a built-in vector type.
167
193
macro_rules! impl_vector_operators {
168
194
(
@@ -173,19 +199,21 @@ macro_rules! impl_vector_operators {
173
199
// Names of the components, with parentheses, for example `(x, y)`.
174
200
( $( $components: ident) ,* )
175
201
) => {
176
- impl_vector_unary_operator!( $Vector, $Scalar , ( $( $components) ,* ) , Neg , neg) ;
177
- impl_vector_vector_binary_operator!( $Vector, $Scalar , ( $( $components) ,* ) , Add , add) ;
178
- impl_vector_vector_binary_operator!( $Vector, $Scalar , ( $( $components) ,* ) , Sub , sub) ;
179
- impl_vector_vector_binary_operator!( $Vector, $Scalar , ( $( $components) ,* ) , Mul , mul) ;
202
+ impl_vector_unary_operator!( $Vector, ( $( $components) ,* ) , Neg , neg) ;
203
+ impl_vector_vector_binary_operator!( $Vector, ( $( $components) ,* ) , Add , add) ;
204
+ impl_vector_vector_binary_operator!( $Vector, ( $( $components) ,* ) , Sub , sub) ;
205
+ impl_vector_vector_binary_operator!( $Vector, ( $( $components) ,* ) , Mul , mul) ;
180
206
impl_vector_scalar_binary_operator!( $Vector, $Scalar, ( $( $components) ,* ) , Mul , mul) ;
181
207
impl_scalar_vector_binary_operator!( $Vector, $Scalar, ( $( $components) ,* ) , Mul , mul) ;
182
- impl_vector_vector_binary_operator!( $Vector, $Scalar , ( $( $components) ,* ) , Div , div) ;
208
+ impl_vector_vector_binary_operator!( $Vector, ( $( $components) ,* ) , Div , div) ;
183
209
impl_vector_scalar_binary_operator!( $Vector, $Scalar, ( $( $components) ,* ) , Div , div) ;
184
- impl_vector_vector_assign_operator!( $Vector, $Scalar, ( $( $components) ,* ) , AddAssign , add_assign) ;
185
- impl_vector_vector_assign_operator!( $Vector, $Scalar, ( $( $components) ,* ) , SubAssign , sub_assign) ;
186
- impl_vector_vector_assign_operator!( $Vector, $Scalar, ( $( $components) ,* ) , MulAssign , mul_assign) ;
210
+ impl_iter_vector_reduction!( $Vector, Sum , sum) ;
211
+ impl_iter_vector_reduction!( $Vector, Product , product) ;
212
+ impl_vector_vector_assign_operator!( $Vector, ( $( $components) ,* ) , AddAssign , add_assign) ;
213
+ impl_vector_vector_assign_operator!( $Vector, ( $( $components) ,* ) , SubAssign , sub_assign) ;
214
+ impl_vector_vector_assign_operator!( $Vector, ( $( $components) ,* ) , MulAssign , mul_assign) ;
187
215
impl_vector_scalar_assign_operator!( $Vector, $Scalar, ( $( $components) ,* ) , MulAssign , mul_assign) ;
188
- impl_vector_vector_assign_operator!( $Vector, $Scalar , ( $( $components) ,* ) , DivAssign , div_assign) ;
216
+ impl_vector_vector_assign_operator!( $Vector, ( $( $components) ,* ) , DivAssign , div_assign) ;
189
217
impl_vector_scalar_assign_operator!( $Vector, $Scalar, ( $( $components) ,* ) , DivAssign , div_assign) ;
190
218
}
191
219
}
0 commit comments