sum.rs - source (original) (raw)
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, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mo>></mo><mi>f</mi><mi>o</mi><mi>r</mi></mrow><annotation encoding="application/x-tex">a> for </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5782em;vertical-align:-0.0391em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span></span></span></span>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, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>a)*);
54 num_sum!(Wrapping(0), <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo><mi>W</mi><mi>r</mi><mi>a</mi><mi>p</mi><mi>p</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo><</mo></mrow><annotation encoding="application/x-tex">(Wrapping<</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal" style="margin-right:0.13889em;">W</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">a</span><span class="mord mathnormal">pp</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><</span></span></span></span>a>)*);
55 );
56}
57
58macro_rules! float_sum {
59 ($($a:ty)*) => (
60 num_sum!(0.0, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>a)*);
61 );
62}
63
64integer_sum! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
65float_sum! { f32 f64 }