[−][src]Trait async_std::stream::FromStream
This is supported on
unstable
only.Conversion from a Stream
.
By implementing FromStream
for a type, you define how it will be created from a stream.
This is common for types which describe a collection of some kind.
See also: IntoStream
.
Examples
Basic usage:
use async_std::prelude::*; use async_std::stream::{self, FromStream}; let five_fives = stream::repeat(5).take(5); let v = Vec::from_stream(five_fives).await; assert_eq!(v, vec![5, 5, 5, 5, 5]);
Using collect
to implicitly use FromStream
use async_std::prelude::*; use async_std::stream; let five_fives = stream::repeat(5).take(5); let v: Vec<i32> = five_fives.collect().await; assert_eq!(v, vec![5, 5, 5, 5, 5]);
Implementing FromStream
for your type:
use async_std::prelude::*; use async_std::stream::{self, FromStream, IntoStream}; use std::pin::Pin; // A sample collection, that's just a wrapper over Vec<T> #[derive(Debug)] struct MyCollection(Vec<i32>); // Let's give it some methods so we can create one and add things // to it. impl MyCollection { fn new() -> MyCollection { MyCollection(Vec::new()) } fn add(&mut self, elem: i32) { self.0.push(elem); } } // and we'll implement FromIterator impl FromStream<i32> for MyCollection { fn from_stream<'a, S: IntoStream<Item = i32> + 'a>( stream: S, ) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>> { let stream = stream.into_stream(); Box::pin(async move { let mut c = MyCollection::new(); let mut v = vec![]; stream::extend(&mut v, stream).await; for i in v { c.add(i); } c }) } } // Now we can make a new stream... let stream = stream::repeat(5).take(5); // ...and make a MyCollection out of it let c = MyCollection::from_stream(stream).await; assert_eq!(c.0, vec![5, 5, 5, 5, 5]); // collect works too! let stream = stream::repeat(5).take(5); let c: MyCollection = stream.collect().await; assert_eq!(c.0, vec![5, 5, 5, 5, 5]);
Required methods
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T> + 'a>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>>
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>>
This is supported on
unstable
only.Creates a value from a stream.
Examples
Basic usage:
use async_std::prelude::*; use async_std::stream::{self, FromStream}; let five_fives = stream::repeat(5).take(5); let v = Vec::from_stream(five_fives).await; assert_eq!(v, vec![5, 5, 5, 5, 5]);
Implementations on Foreign Types
impl FromStream<()> for ()
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = ()>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for Vec<T>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<'b, T: Clone> FromStream<T> for Cow<'b, [T]>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for Box<[T]>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for Rc<[T]>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T, E, V> FromStream<Result<T, E>> for Result<V, E> where
V: FromStream<T>,
[src]
V: FromStream<T>,
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = Result<T, E>>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
This is supported on
unstable
and unstable
only.Takes each element in the stream: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur, a container with the values of each Result
is returned.
impl<T, V> FromStream<Option<T>> for Option<V> where
V: FromStream<T>,
[src]
V: FromStream<T>,
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = Option<T>>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
This is supported on
unstable
and unstable
only.Takes each element in the stream: if it is None
, no further
elements are taken, and None
is returned. Should no None
occur, a container with the values of each Option
is returned.
impl FromStream<char> for String
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = char>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<'b> FromStream<&'b char> for String
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = &'b char>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<'b> FromStream<&'b str> for String
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = &'b str>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl FromStream<String> for String
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = String>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<'b> FromStream<Cow<'b, str>> for String
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = Cow<'b, str>>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T: Ord> FromStream<T> for BinaryHeap<T>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<K: Ord, V> FromStream<(K, V)> for BTreeMap<K, V>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = (K, V)>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T: Ord> FromStream<T> for BTreeSet<T>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<K, V, H> FromStream<(K, V)> for HashMap<K, V, H> where
K: Eq + Hash,
H: BuildHasher + Default,
[src]
K: Eq + Hash,
H: BuildHasher + Default,
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = (K, V)>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T, H> FromStream<T> for HashSet<T, H> where
T: Eq + Hash,
H: BuildHasher + Default,
[src]
T: Eq + Hash,
H: BuildHasher + Default,
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for LinkedList<T>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for VecDeque<T>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
Implementors
impl<'b, P: AsRef<Path> + 'b> FromStream<P> for PathBuf
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = P>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
impl<T> FromStream<T> for Arc<[T]>
[src]
ⓘImportant traits for Pin<P>fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,
[src]
ⓘImportant traits for Pin<P>
stream: S
) -> Pin<Box<dyn Future<Output = Self> + 'a>> where
<S as IntoStream>::IntoStream: 'a,