diff --git a/src/stream/double_ended/from_iter.rs b/src/stream/double_ended/from_iter.rs new file mode 100644 index 0000000..616724f --- /dev/null +++ b/src/stream/double_ended/from_iter.rs @@ -0,0 +1,38 @@ +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, +} + +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/mod.rs b/src/stream/double_ended/mod.rs index 3267ae5..b8b78b2 100644 --- a/src/stream/double_ended/mod.rs +++ b/src/stream/double_ended/mod.rs @@ -3,12 +3,14 @@ mod nth_back; mod rfind; mod rfold; mod try_rfold; +mod from_iter; use next_back::NextBackFuture; use nth_back::NthBackFuture; use rfind::RFindFuture; use rfold::RFoldFuture; use try_rfold::TryRFoldFuture; +pub use from_iter::{from_iter, FromIter}; extension_trait! { use crate::stream::Stream; @@ -113,10 +115,9 @@ extension_trait! { ``` # fn main() { async_std::task::block_on(async { # - use async_std::stream::Sample; - use async_std::stream::double_ended::DoubleEndedStreamExt; + use async_std::stream::double_ended::{self, DoubleEndedStreamExt}; - let mut s = Sample::from(vec![7u8]); + let mut s = double_ended::from_iter(vec![7u8]); assert_eq!(s.next_back().await, Some(7)); assert_eq!(s.next_back().await, None); @@ -141,10 +142,9 @@ extension_trait! { ``` # fn main() { async_std::task::block_on(async { # - use async_std::stream::Sample; - use async_std::stream::double_ended::DoubleEndedStreamExt; + use async_std::stream::double_ended::{self, DoubleEndedStreamExt}; - let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]); + let mut s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]); let second = s.nth_back(1).await; assert_eq!(second, Some(4)); @@ -172,10 +172,9 @@ extension_trait! { ``` # fn main() { async_std::task::block_on(async { # - use async_std::stream::Sample; - use async_std::stream::double_ended::DoubleEndedStreamExt; + use async_std::stream::double_ended::{self, DoubleEndedStreamExt}; - let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]); + let mut s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]); let second = s.rfind(|v| v % 2 == 0).await; assert_eq!(second, Some(4)); @@ -202,10 +201,9 @@ extension_trait! { ``` # fn main() { async_std::task::block_on(async { # - use async_std::stream::Sample; - use async_std::stream::double_ended::DoubleEndedStreamExt; + use async_std::stream::double_ended::{self, DoubleEndedStreamExt}; - let s = Sample::from(vec![1, 2, 3, 4, 5]); + let s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]); let second = s.rfold(0, |acc, v| v + acc).await; @@ -237,10 +235,9 @@ extension_trait! { ``` # fn main() { async_std::task::block_on(async { # - use async_std::stream::Sample; - use async_std::stream::double_ended::DoubleEndedStreamExt; + use async_std::stream::double_ended::{self, DoubleEndedStreamExt}; - let s = Sample::from(vec![1, 2, 3, 4, 5]); + let s = double_ended::from_iter(vec![1u8, 2, 3, 4, 5]); let sum = s.try_rfold(0, |acc, v| { if (acc+v) % 2 == 1 { Ok(v+3) diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 6ce9aac..f6b6497 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -307,7 +307,6 @@ pub use once::{once, Once}; pub use repeat::{repeat, Repeat}; pub use repeat_with::{repeat_with, RepeatWith}; pub use stream::*; -pub use crate::stream::sample::Sample; pub mod stream; @@ -317,7 +316,6 @@ mod from_iter; mod once; mod repeat; mod repeat_with; -mod sample; cfg_unstable! { pub mod double_ended; diff --git a/src/stream/sample.rs b/src/stream/sample.rs deleted file mode 100644 index 90caeed..0000000 --- a/src/stream/sample.rs +++ /dev/null @@ -1,33 +0,0 @@ -use crate::stream::Stream; - -use std::pin::Pin; -use std::task::{Context, Poll}; -use crate::stream::DoubleEndedStream; - -pub struct Sample { - inner: Vec, -} - -impl From> for Sample { - fn from(data: Vec) -> Self { - Sample { inner: data } - } -} - -impl Unpin for Sample {} - -impl Stream for Sample { - 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 Sample { - fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(self.inner.pop()) - } -}