Trait FromStream

Source
pub trait FromStream<T: Send> {
    // Required method
    fn from_stream<'a, S: IntoStream<Item = T> + 'a>(
        stream: S,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
       where <S as IntoStream>::IntoStream: Send;
}
Available on unstable only.
Expand description

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 Future<Output = Self> + 'a + Send>>
   where
       <S as IntoStream>::IntoStream: Send,
   {
        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§

Source

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

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]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FromStream<char> for String

Source§

fn from_stream<'a, S: IntoStream<Item = char> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl FromStream<()> for ()

Source§

fn from_stream<'a, S: IntoStream<Item = ()> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl FromStream<String> for String

Source§

fn from_stream<'a, S: IntoStream<Item = String> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<'b> FromStream<&'b char> for String

Source§

fn from_stream<'a, S: IntoStream<Item = &'b char> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<'b> FromStream<&'b str> for String

Source§

fn from_stream<'a, S: IntoStream<Item = &'b str> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<'b> FromStream<Cow<'b, str>> for String

Source§

fn from_stream<'a, S: IntoStream<Item = Cow<'b, str>> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<'b, T: Clone + Send> FromStream<T> for Cow<'b, [T]>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<K, V, H> FromStream<(K, V)> for HashMap<K, V, H>
where K: Eq + Hash + Send, H: BuildHasher + Default + Send, V: Send,

Source§

fn from_stream<'a, S: IntoStream<Item = (K, V)> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<K: Ord + Send, V: Send> FromStream<(K, V)> for BTreeMap<K, V>

Source§

fn from_stream<'a, S: IntoStream<Item = (K, V)> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T, E, V> FromStream<Result<T, E>> for Result<V, E>
where T: Send, E: Send, V: FromStream<T>,

Source§

fn from_stream<'a, S: IntoStream<Item = Result<T, E>> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

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.

§Examples
use async_std::prelude::*;
use async_std::stream;

let v = stream::from_iter(vec![1, 2]);
let res: Result<Vec<u32>, &'static str> = v.map(|x: u32|
    x.checked_add(1).ok_or("Overflow!")
).collect().await;
assert_eq!(res, Ok(vec![2, 3]));
Source§

impl<T, H> FromStream<T> for HashSet<T, H>
where T: Eq + Hash + Send, H: BuildHasher + Default + Send,

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Ord + Send> FromStream<T> for BinaryHeap<T>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Ord + Send> FromStream<T> for BTreeSet<T>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Send> FromStream<T> for Box<[T]>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Send> FromStream<T> for LinkedList<T>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Send> FromStream<T> for VecDeque<T>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Send> FromStream<T> for Rc<[T]>

Source§

fn from_stream<'a, S: IntoStream<Item = T> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

Source§

impl<T: Send> FromStream<T> for Vec<T>

Source§

fn from_stream<'a, S: IntoStream<Item = T>>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: 'a + Send,

Source§

impl<T: Send, V> FromStream<Option<T>> for Option<V>
where V: FromStream<T>,

Source§

fn from_stream<'a, S: IntoStream<Item = Option<T>> + 'a>( stream: S, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'a>>
where <S as IntoStream>::IntoStream: Send,

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.

Implementors§

Source§

impl<'b, P: AsRef<Path> + 'b + Send> FromStream<P> for PathBuf

Source§

impl<T: Send> FromStream<T> for Arc<[T]>