async_std/option/
sum.rs

1use std::pin::Pin;
2
3use crate::prelude::*;
4use crate::stream::{Stream, Sum};
5use std::convert::identity;
6
7impl<T, U> Sum<Option<U>> for Option<T>
8where
9    T: Sum<U>,
10{
11    #[doc = r#"
12        Takes each element in the `Iterator`: if it is a `None`, no further
13        elements are taken, and the `None` is returned. Should no `None` occur,
14        the sum of all elements is returned.
15
16        # Examples
17
18        This sums up the position of the character 'a' in a vector of strings,
19        if a word did not have the character 'a' the operation returns `None`:
20
21        ```
22        # fn main() { async_std::task::block_on(async {
23        #
24        use async_std::prelude::*;
25        use async_std::stream;
26
27        let words = stream::from_iter(vec!["have", "a", "great", "day"]);
28        let total: Option<usize> = words.map(|w| w.find('a')).sum().await;
29        assert_eq!(total, Some(5));
30        #
31        # }) }
32        ```
33    "#]
34    fn sum<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Option<T>> + 'a>>
35    where
36        S: Stream<Item = Option<U>> + 'a,
37    {
38        Box::pin(async move {
39            // Using `take_while` here because it is able to stop the stream early
40            // if a failure occurs
41            let mut found_none = false;
42            let out = <T as Sum<U>>::sum(
43                stream
44                    .take_while(|elem| {
45                        elem.is_some() || {
46                            found_none = true;
47                            // Stop processing the stream on `None`
48                            false
49                        }
50                    })
51                    .filter_map(identity),
52            )
53            .await;
54
55            if found_none { None } else { Some(out) }
56        })
57    }
58}