From 182fe6896f628efde8bdd7b5d00923c7ea0629e5 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 12 Dec 2019 20:46:03 +0100 Subject: [PATCH] No need for a custom impl for FromIter for DoubleEndedStream --- src/stream/double_ended_stream/from_iter.rs | 57 --------------------- src/stream/double_ended_stream/mod.rs | 2 - src/stream/from_iter.rs | 9 ++++ 3 files changed, 9 insertions(+), 59 deletions(-) delete mode 100644 src/stream/double_ended_stream/from_iter.rs diff --git a/src/stream/double_ended_stream/from_iter.rs b/src/stream/double_ended_stream/from_iter.rs deleted file mode 100644 index 29a3e7d8..00000000 --- a/src/stream/double_ended_stream/from_iter.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::stream::Stream; - -use std::pin::Pin; -use std::task::{Context, Poll}; -use crate::stream::DoubleEndedStream; - -/// A double-ended stream that was created from iterator. -/// -/// This stream is created by the [`from_iter`] function. -/// See it documentation for more. -/// -/// [`from_iter`]: fn.from_iter.html -#[derive(Debug)] -pub struct FromIter { - inner: Vec, -} - -/// Converts an iterator into a double-ended stream. -/// -/// # Examples -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::stream::double_ended_stream::{self, DoubleEndedStream}; -/// -/// let mut s = double_ended_stream::from_iter(vec![0, 1, 2, 3]); -/// -/// assert_eq!(s.next_back().await, Some(3)); -/// assert_eq!(s.next_back().await, Some(2)); -/// assert_eq!(s.next_back().await, Some(1)); -/// assert_eq!(s.next_back().await, Some(0)); -/// assert_eq!(s.next_back().await, None); -/// # -/// # }) -/// ``` -pub fn from_iter(iter: I) -> FromIter { - FromIter { inner: iter.into_iter().collect() } -} - -impl Unpin for FromIter {} - -impl Stream for FromIter { - type Item = T; - fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - if self.inner.len() > 0 { - return Poll::Ready(Some(self.inner.remove(0))); - } - return Poll::Ready(None); - } -} - -impl DoubleEndedStream for FromIter { - fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(self.inner.pop()) - } -} diff --git a/src/stream/double_ended_stream/mod.rs b/src/stream/double_ended_stream/mod.rs index 92136615..dc2a45c9 100644 --- a/src/stream/double_ended_stream/mod.rs +++ b/src/stream/double_ended_stream/mod.rs @@ -3,14 +3,12 @@ use crate::stream::Stream; use std::pin::Pin; use std::task::{Context, Poll}; -mod from_iter; mod next_back; mod nth_back; mod rfind; mod rfold; mod try_rfold; -pub use from_iter::{from_iter, FromIter}; use next_back::NextBackFuture; use nth_back::NthBackFuture; use rfind::RFindFuture; diff --git a/src/stream/from_iter.rs b/src/stream/from_iter.rs index d7a31d6c..705d1504 100644 --- a/src/stream/from_iter.rs +++ b/src/stream/from_iter.rs @@ -3,6 +3,8 @@ use std::pin::Pin; use pin_project_lite::pin_project; use crate::stream::Stream; +#[cfg(feature = "unstable")] +use crate::stream::double_ended_stream::DoubleEndedStream; use crate::task::{Context, Poll}; pin_project! { @@ -51,3 +53,10 @@ impl Stream for FromIter { Poll::Ready(self.iter.next()) } } + +#[cfg(feature = "unstable")] +impl DoubleEndedStream for FromIter { + fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(self.iter.next_back()) + } +}