diff --git a/src/lib.rs b/src/lib.rs index dfa9a07ac..4904a1506 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,7 @@ mod result; pub mod stream; pub mod sync; pub mod task; -mod vec; +pub mod vec; #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg(feature = "unstable")] diff --git a/src/vec/into_stream.rs b/src/vec/into_stream.rs new file mode 100644 index 000000000..42472a05c --- /dev/null +++ b/src/vec/into_stream.rs @@ -0,0 +1,89 @@ +use std::pin::Pin; +use std::task::{Context, Poll}; + +/// A stream that moves out of a vector. +#[derive(Debug)] +pub struct IntoStream { + iter: std::vec::IntoIter, +} + +impl crate::stream::IntoStream for Vec { + type Item = T; + type IntoStream = IntoStream; + + /// Creates a consuming iterator, that is, one that moves each value out of + /// the vector (from start to end). The vector cannot be used after calling + /// this. + /// + /// # Examples + /// + /// ``` + /// let v = vec!["a".to_string(), "b".to_string()]; + /// for s in v.into_stream() { + /// // s has type String, not &String + /// println!("{}", s); + /// } + /// ``` + #[inline] + fn into_stream(mut self) -> IntoStream { + let iter = self.into_iter(); + IntoStream { iter } + } +} + +impl futures_core::stream::Stream for IntoStream { + type Item = T; + + fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(self.iter.next()) + } +} + +/// Slice stream. +#[derive(Debug)] +pub struct Stream<'a, T: 'a> { + iter: std::slice::Iter<'a, T>, +} + +impl<'a, T: Sync> futures_core::stream::Stream for Stream<'a, T> { + type Item = &'a T; + + fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(self.iter.next()) + } +} + +impl<'a, T: Sync> crate::stream::IntoStream for &'a Vec { + type Item = &'a T; + type IntoStream = Stream<'a, T>; + + fn into_stream(self) -> Stream<'a, T> { + let iter = self.iter(); + Stream { iter } + } +} + +/// Mutable slice stream. +#[derive(Debug)] +pub struct StreamMut<'a, T: 'a> { + iter: std::slice::IterMut<'a, T>, +} + +impl<'a, T: Sync> futures_core::stream::Stream for StreamMut<'a, T> { + type Item = &'a mut T; + + fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(self.iter.next()) + } +} + +impl<'a, T: Send + Sync> crate::stream::IntoStream for &'a mut Vec { + type Item = &'a mut T; + + type IntoStream = StreamMut<'a, T>; + + fn into_stream(self) -> StreamMut<'a, T> { + let iter = self.iter_mut(); + StreamMut { iter } + } +} diff --git a/src/vec/mod.rs b/src/vec/mod.rs index 725fc8f7e..9cd240966 100644 --- a/src/vec/mod.rs +++ b/src/vec/mod.rs @@ -1,9 +1,11 @@ -//! The Rust core allocation and collections library +//! Core allocation and collections library. //! -//! This library provides smart pointers and collections for managing -//! heap-allocated values. +//! This library contains stream types for `std::vec::Vec`. mod from_stream; +mod into_stream; -#[doc(inline)] -pub use std::vec::Vec; +// #[doc(inline)] +// pub use std::vec::Vec; + +pub use into_stream::{IntoStream, Stream, StreamMut};