/// Trait to represent types that can be created by summing up an iterator.
///
/// This trait is used to implement [`Iterator::sum()`]. Types which implement
/// this trait can be generated by using the [`sum()`] method on an iterator.
/// Like [`FromIterator`], this trait should rarely be called directly.
///
/// [`sum()`]: crate::iter::Iterator::sum
/// [`FromIterator`]: crate::iter::FromIterator
pub trait Sum<A> {
/// Takes an iterator and generates `Self` from the elements by "summing up"
/// the items.
fn sum<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(iter: I) -> A;
}
impl SumAddableTypesImpl<A, +Add<A>, impl ZeroA: core::num::traits::Zero<A>> of Sum<A> {
fn sum<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(mut iter: I) -> A {
iter.fold(ZeroA::zero(), |acc, x| acc + x)
}
}
/// Trait to represent types that can be created by multiplying elements of an
/// iterator.
///
/// This trait is used to implement [`Iterator::product()`]. Types which implement
/// this trait can be generated by using the [`product()`] method on an iterator.
/// Like [`FromIterator`], this trait should rarely be called directly.
///
/// [`product()`]: Iterator::product
/// [`FromIterator`]: crate::iter::FromIterator
pub trait Product<A> {
/// Takes an iterator and generates `Self` from the elements by multiplying the items.
fn product<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(iter: I) -> A;
}
impl ProductMultiplicativeTypesImpl<
A, +Mul<A>, impl OneA: core::num::traits::One<A>,
> of Product<A> {
fn product<I, +Iterator<I>[Item: A], +Destruct<I>, +Destruct<A>>(mut iter: I) -> A {
iter.fold(OneA::one(), |acc, x| acc * x)
}
}