From c4b9a7f680070c254ed6f9220ad58b7080a93b54 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Tue, 19 Nov 2019 22:20:36 +0000 Subject: [PATCH] Add samples for some of the functions --- src/stream/double_ended/mod.rs | 45 +++++++++++++++++++++++++++++-- src/stream/double_ended_stream.rs | 28 ------------------- src/stream/mod.rs | 4 ++- src/stream/sample.rs | 33 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 src/stream/sample.rs diff --git a/src/stream/double_ended/mod.rs b/src/stream/double_ended/mod.rs index a2f9d266..24917756 100644 --- a/src/stream/double_ended/mod.rs +++ b/src/stream/double_ended/mod.rs @@ -37,10 +37,10 @@ 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; - let mut s = stream::from_iter(vec![1u8, 2, 3, 4, 5]); + let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]); let second = s.nth_back(1).await; assert_eq!(second, Some(4)); @@ -58,6 +58,27 @@ extension_trait! { NthBackFuture::new(self, n) } + #[doc = r#" + Returns the the frist element from the right that matches the predicate. + + # Examples + + Basic usage: + + ``` + # fn main() { async_std::task::block_on(async { + # + use async_std::stream::Sample; + use async_std::stream::double_ended::DoubleEndedStreamExt; + + let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]); + + let second = s.rfind(|v| v % 2 == 0).await; + assert_eq!(second, Some(4)); + # + # }) } + ``` + "#] fn rfind

( &mut self, p: P, @@ -69,6 +90,26 @@ extension_trait! { RFindFuture::new(self, p) } + #[doc = r#" + # Examples + + Basic usage: + + ``` + # fn main() { async_std::task::block_on(async { + # + use async_std::stream::Sample; + use async_std::stream::double_ended::DoubleEndedStreamExt; + + let s = Sample::from(vec![1, 2, 3, 4, 5]); + + let second = s.rfold(0, |acc, v| v + acc).await; + + assert_eq!(second, 15); + # + # }) } + ``` + "#] fn rfold( self, accum: B, diff --git a/src/stream/double_ended_stream.rs b/src/stream/double_ended_stream.rs index 95e47633..129bb1cd 100644 --- a/src/stream/double_ended_stream.rs +++ b/src/stream/double_ended_stream.rs @@ -22,31 +22,3 @@ pub trait DoubleEndedStream: Stream { /// [trait-level]: trait.DoubleEndedStream.html fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>; } - -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()) - } -} diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 318733b2..6ce9aac7 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -307,8 +307,9 @@ 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(crate) mod stream; +pub mod stream; mod empty; mod from_fn; @@ -316,6 +317,7 @@ 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 new file mode 100644 index 00000000..90caeed1 --- /dev/null +++ b/src/stream/sample.rs @@ -0,0 +1,33 @@ +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()) + } +}