async_std/option/
from_stream.rs1use std::pin::Pin;
2
3use crate::prelude::*;
4use crate::stream::{FromStream, IntoStream};
5use std::convert::identity;
6
7impl<T, V> FromStream<Option<T>> for Option<V>
8where
9 V: FromStream<T>,
10{
11 #[inline]
15 fn from_stream<'a, S: IntoStream<Item = Option<T>> + 'a>(
16 stream: S,
17 ) -> Pin<Box<dyn Future<Output = Self> + 'a>> {
18 let stream = stream.into_stream();
19
20 Box::pin(async move {
21 let mut found_none = false;
24 let out: V = stream
25 .take_while(|elem| {
26 elem.is_some() || {
27 found_none = true;
28 false
30 }
31 })
32 .filter_map(identity)
33 .collect()
34 .await;
35
36 if found_none { None } else { Some(out) }
37 })
38 }
39}