2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-16 02:39:55 +00:00

FromStream + Extend for VecDeque

This commit is contained in:
Sunjay Varma 2019-10-01 23:11:57 -04:00
parent 6bc3cd0ab2
commit ae146afffc
5 changed files with 58 additions and 0 deletions

8
src/collections/mod.rs Normal file
View file

@ -0,0 +1,8 @@
//! The Rust standard collections
//!
//! This library provides efficient implementations of the most common general purpose programming
//! data structures.
mod vec_deque;
pub use vec_deque::*;

View file

@ -0,0 +1,18 @@
use std::pin::Pin;
use std::collections::VecDeque;
use crate::prelude::*;
use crate::stream::{Extend, IntoStream};
impl<T> Extend<T> for VecDeque<T> {
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
&'a mut self,
stream: S,
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
let stream = stream.into_stream();
//TODO: Add this back in when size_hint is added to Stream/StreamExt
//let (lower_bound, _) = stream.size_hint();
//self.reserve(lower_bound);
Box::pin(stream.for_each(move |item| self.push_back(item)))
}
}

View file

@ -0,0 +1,24 @@
use std::pin::Pin;
use std::collections::VecDeque;
use crate::stream::{Extend, FromStream, IntoStream};
impl<T> FromStream<T> for VecDeque<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 = VecDeque::new();
out.stream_extend(stream).await;
out
})
}
}

View file

@ -0,0 +1,7 @@
//! The Rust double-ended queue, implemented with a growable ring buffer.
mod extend;
mod from_stream;
#[doc(inline)]
pub use std::collections::VecDeque;

View file

@ -70,6 +70,7 @@ cfg_if! {
mod result;
mod option;
mod string;
mod collections;
}
}