Add samples for some of the functions

split-by-pattern
Felipe Sere 5 years ago
parent aabfefd015
commit c4b9a7f680

@ -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<P>(
&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<B, F>(
self,
accum: B,

@ -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<Option<Self::Item>>;
}
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())
}
}

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

@ -0,0 +1,33 @@
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