Replace sample with a hidden from_iter implementation for double-ended-stream

pull/562/head
Felipe Sere 5 years ago
parent abd360893c
commit 892c6008c2

@ -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<T> {
inner: Vec<T>,
}
pub fn from_iter<I: IntoIterator>(iter: I) -> FromIter<I::Item> {
FromIter { inner: iter.into_iter().collect() }
}
impl<T> Unpin for FromIter<T> {}
impl<T> Stream for FromIter<T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.inner.len() > 0 {
return Poll::Ready(Some(self.inner.remove(0)));
}
return Poll::Ready(None);
}
}
impl<T> DoubleEndedStream for FromIter<T> {
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.inner.pop())
}
}

@ -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)

@ -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;

@ -1,33 +0,0 @@
use crate::stream::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::stream::DoubleEndedStream;
pub struct Sample<T> {
inner: Vec<T>,
}
impl<T> From<Vec<T>> for Sample<T> {
fn from(data: Vec<T>) -> Self {
Sample { inner: data }
}
}
impl<T> Unpin for Sample<T> {}
impl<T> Stream for Sample<T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.inner.len() > 0 {
return Poll::Ready(Some(self.inner.remove(0)));
}
return Poll::Ready(None);
}
}
impl<T> DoubleEndedStream for Sample<T> {
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.inner.pop())
}
}
Loading…
Cancel
Save