async_std/stream/
sum.rs

1use core::future::Future;
2use core::pin::Pin;
3
4use crate::stream::Stream;
5
6/// Trait to represent types that can be created by summing up a stream.
7///
8/// This trait is used to implement the [`sum`] method on streams. Types which
9/// implement the trait can be generated by the [`sum`] method. Like
10/// [`FromStream`] this trait should rarely be called directly and instead
11/// interacted with through [`Stream::sum`].
12///
13/// [`sum`]: trait.Sum.html#tymethod.sum
14/// [`FromStream`]: trait.FromStream.html
15/// [`Stream::sum`]: trait.Stream.html#method.sum
16#[cfg(feature = "unstable")]
17#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
18pub trait Sum<A = Self>: Sized {
19    /// Method which takes a stream and generates `Self` from the elements by
20    /// "summing up" the items.
21    fn sum<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'a>>
22    where
23        S: Stream<Item = A> + 'a;
24}
25
26use crate::stream::stream::StreamExt;
27use core::num::Wrapping;
28use core::ops::Add;
29
30macro_rules! num_sum {
31    ($zero:expr, $($a:ty)*) => ($(
32        impl Sum for $a {
33            fn sum<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self>+ 'a>>
34            where
35                S: Stream<Item = $a> + 'a,
36            {
37                Box::pin(async move { stream.fold($zero, Add::add).await } )
38            }
39        }
40        impl<'a> Sum<&'a $a> for $a {
41            fn sum<'b, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'b>>
42            where
43                S: Stream<Item = &'a $a> + 'b,
44            {
45                Box::pin(async move { stream.fold($zero, Add::add).await } )
46            }
47        }
48    )*);
49}
50
51macro_rules! integer_sum {
52    ($($a:ty)*) => (
53        num_sum!(0, $($a)*);
54        num_sum!(Wrapping(0), $(Wrapping<$a>)*);
55    );
56}
57
58macro_rules! float_sum {
59    ($($a:ty)*) => (
60        num_sum!(0.0, $($a)*);
61    );
62}
63
64integer_sum! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
65float_sum! { f32 f64 }