From f56fbda134d23d3813f933c8d28ac8cd80fbf599 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 18 Sep 2019 23:44:29 +0200 Subject: [PATCH 1/2] IntoStream for vec Signed-off-by: Yoshua Wuyts --- src/lib.rs | 2 +- src/stream/into_stream.rs | 16 +++---- src/vec/into_stream.rs | 89 +++++++++++++++++++++++++++++++++++++++ src/vec/mod.rs | 12 +++--- 4 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 src/vec/into_stream.rs 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/stream/into_stream.rs b/src/stream/into_stream.rs index b2913170a..bcadb1c5b 100644 --- a/src/stream/into_stream.rs +++ b/src/stream/into_stream.rs @@ -25,12 +25,12 @@ pub trait IntoStream { fn into_stream(self) -> Self::IntoStream; } -impl IntoStream for I { - type Item = I::Item; - type IntoStream = I; +// impl IntoStream for I { +// type Item = I::Item; +// type IntoStream = I; - #[inline] - fn into_stream(self) -> I { - self - } -} +// #[inline] +// fn into_stream(self) -> I { +// self +// } +// } 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}; From f8e16950077ff0f74d2a715d1236bf29b1bbf25c Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 18 Sep 2019 23:53:28 +0200 Subject: [PATCH 2/2] uncomment out the into-stream failures Signed-off-by: Yoshua Wuyts --- src/stream/into_stream.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/stream/into_stream.rs b/src/stream/into_stream.rs index bcadb1c5b..b2913170a 100644 --- a/src/stream/into_stream.rs +++ b/src/stream/into_stream.rs @@ -25,12 +25,12 @@ pub trait IntoStream { fn into_stream(self) -> Self::IntoStream; } -// impl IntoStream for I { -// type Item = I::Item; -// type IntoStream = I; +impl IntoStream for I { + type Item = I::Item; + type IntoStream = I; -// #[inline] -// fn into_stream(self) -> I { -// self -// } -// } + #[inline] + fn into_stream(self) -> I { + self + } +}