Skip to content

Commit 24ff25c

Browse files
committed
FromStream + Extend for VecDeque
1 parent 5ba2539 commit 24ff25c

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

src/collections/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! The Rust standard collections
2+
//!
3+
//! This library provides efficient implementations of the most common general purpose programming
4+
//! data structures.
5+
6+
mod vec_deque;
7+
8+
pub use vec_deque::*;

src/collections/vec_deque/extend.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::pin::Pin;
2+
use std::collections::VecDeque;
3+
4+
use crate::prelude::*;
5+
use crate::stream::{Extend, IntoStream};
6+
7+
impl<T> Extend<T> for VecDeque<T> {
8+
fn stream_extend<'a, S: IntoStream<Item = T> + 'a>(
9+
&'a mut self,
10+
stream: S,
11+
) -> Pin<Box<dyn Future<Output = ()> + 'a>> {
12+
let stream = stream.into_stream();
13+
//TODO: Add this back in when size_hint is added to Stream/StreamExt
14+
//let (lower_bound, _) = stream.size_hint();
15+
//self.reserve(lower_bound);
16+
Box::pin(stream.for_each(move |item| self.push_back(item)))
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::pin::Pin;
2+
use std::collections::VecDeque;
3+
4+
use crate::stream::{Extend, FromStream, IntoStream};
5+
6+
impl<T> FromStream<T> for VecDeque<T> {
7+
#[inline]
8+
fn from_stream<'a, S: IntoStream<Item = T>>(
9+
stream: S,
10+
) -> Pin<Box<dyn core::future::Future<Output = Self> + 'a>>
11+
where
12+
<S as IntoStream>::IntoStream: 'a,
13+
{
14+
let stream = stream.into_stream();
15+
16+
Box::pin(async move {
17+
pin_utils::pin_mut!(stream);
18+
19+
let mut out = VecDeque::new();
20+
out.stream_extend(stream).await;
21+
out
22+
})
23+
}
24+
}

src/collections/vec_deque/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! The Rust double-ended queue, implemented with a growable ring buffer.
2+
3+
mod extend;
4+
mod from_stream;
5+
6+
#[doc(inline)]
7+
pub use std::collections::VecDeque;

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ cfg_if! {
6969
mod vec;
7070
mod result;
7171
mod option;
72+
mod collections;
7273
}
7374
}
7475

0 commit comments

Comments
 (0)