22
33//! Generic Field Traits
44
5- use core:: { fmt, hash, ops} ;
5+ use core:: { fmt, hash, iter , ops} ;
66
77/// A generic field.
88pub trait Field :
@@ -13,6 +13,8 @@ pub trait Field:
1313 + hash:: Hash
1414 + fmt:: Debug
1515 + fmt:: Display
16+ + iter:: Sum
17+ + for < ' a > iter:: Sum < & ' a Self >
1618 + ops:: Add < Self , Output = Self >
1719 + ops:: Sub < Self , Output = Self >
1820 + ops:: AddAssign
@@ -29,7 +31,7 @@ pub trait Field:
2931 + for < ' a > ops:: MulAssign < & ' a Self >
3032 + for < ' a > ops:: Div < & ' a Self , Output = Self >
3133 + for < ' a > ops:: DivAssign < & ' a Self >
32- + ops:: Neg
34+ + ops:: Neg < Output = Self >
3335{
3436 /// The zero constant of the field.
3537 const ZERO : Self ;
@@ -362,6 +364,19 @@ macro_rules! impl_ops_for_fe {
362364 self . _neg( )
363365 }
364366 }
367+
368+ // sum
369+ impl core:: iter:: Sum for $op {
370+ fn sum<I : Iterator <Item = Self >>( iter: I ) -> Self {
371+ iter. fold( crate :: primitives:: Field :: ZERO , |i, acc| i + acc)
372+ }
373+ }
374+
375+ impl <' s> core:: iter:: Sum <& ' s Self > for $op {
376+ fn sum<I : Iterator <Item = & ' s Self >>( iter: I ) -> Self {
377+ iter. fold( crate :: primitives:: Field :: ZERO , |i, acc| i + acc)
378+ }
379+ }
365380 } ;
366381}
367382pub ( super ) use impl_ops_for_fe;
0 commit comments