async-std/src/vec/from_stream.rs
Sunjay Varma f968c9a540 rustfmt
2019-10-04 10:09:06 -04:00

98 lines
2.3 KiB
Rust

use std::borrow::Cow;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use crate::stream::{Extend, FromStream, IntoStream};
impl<T> FromStream<T> for Vec<T> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
let mut out = vec![];
out.stream_extend(stream).await;
out
})
}
}
impl<'b, T: Clone> FromStream<T> for Cow<'b, [T]> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
Cow::Owned(FromStream::from_stream(stream).await)
})
}
}
impl<T> FromStream<T> for Box<[T]> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
Vec::from_stream(stream).await.into_boxed_slice()
})
}
}
impl<T> FromStream<T> for Rc<[T]> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
Vec::from_stream(stream).await.into()
})
}
}
impl<T> FromStream<T> for Arc<[T]> {
#[inline]
fn from_stream<'a, S: IntoStream<Item = T>>(
stream: S,
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
where
<S as IntoStream>::IntoStream: 'a,
{
let stream = stream.into_stream();
Box::pin(async move {
pin_utils::pin_mut!(stream);
Vec::from_stream(stream).await.into()
})
}
}