diff --git a/src/collections/mod.rs b/src/collections/mod.rs new file mode 100644 index 00000000..e20a321a --- /dev/null +++ b/src/collections/mod.rs @@ -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::*; diff --git a/src/collections/vec_deque/extend.rs b/src/collections/vec_deque/extend.rs new file mode 100644 index 00000000..b8884d81 --- /dev/null +++ b/src/collections/vec_deque/extend.rs @@ -0,0 +1,18 @@ +use std::pin::Pin; +use std::collections::VecDeque; + +use crate::prelude::*; +use crate::stream::{Extend, IntoStream}; + +impl Extend for VecDeque { + fn stream_extend<'a, S: IntoStream + 'a>( + &'a mut self, + stream: S, + ) -> Pin + '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))) + } +} diff --git a/src/collections/vec_deque/from_stream.rs b/src/collections/vec_deque/from_stream.rs new file mode 100644 index 00000000..ef92a49c --- /dev/null +++ b/src/collections/vec_deque/from_stream.rs @@ -0,0 +1,24 @@ +use std::pin::Pin; +use std::collections::VecDeque; + +use crate::stream::{Extend, FromStream, IntoStream}; + +impl FromStream for VecDeque { + #[inline] + fn from_stream<'a, S: IntoStream>( + stream: S, + ) -> Pin + 'a>> + where + ::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 + }) + } +} diff --git a/src/collections/vec_deque/mod.rs b/src/collections/vec_deque/mod.rs new file mode 100644 index 00000000..b03d5e6d --- /dev/null +++ b/src/collections/vec_deque/mod.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 3b0aa699..839a0ec2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,6 +70,7 @@ cfg_if! { mod result; mod option; mod string; + mod collections; } }