Skip to content

Commit c4b9a7f

Browse files
committed
Add samples for some of the functions
1 parent aabfefd commit c4b9a7f

File tree

4 files changed

+79
-31
lines changed

4 files changed

+79
-31
lines changed

src/stream/double_ended/mod.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ extension_trait! {
3737
```
3838
# fn main() { async_std::task::block_on(async {
3939
#
40+
use async_std::stream::Sample;
4041
use async_std::stream::double_ended::DoubleEndedStreamExt;
41-
use async_std::stream;
4242
43-
let mut s = stream::from_iter(vec![1u8, 2, 3, 4, 5]);
43+
let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]);
4444
4545
let second = s.nth_back(1).await;
4646
assert_eq!(second, Some(4));
@@ -58,6 +58,27 @@ extension_trait! {
5858
NthBackFuture::new(self, n)
5959
}
6060

61+
#[doc = r#"
62+
Returns the the frist element from the right that matches the predicate.
63+
64+
# Examples
65+
66+
Basic usage:
67+
68+
```
69+
# fn main() { async_std::task::block_on(async {
70+
#
71+
use async_std::stream::Sample;
72+
use async_std::stream::double_ended::DoubleEndedStreamExt;
73+
74+
let mut s = Sample::from(vec![1u8, 2, 3, 4, 5]);
75+
76+
let second = s.rfind(|v| v % 2 == 0).await;
77+
assert_eq!(second, Some(4));
78+
#
79+
# }) }
80+
```
81+
"#]
6182
fn rfind<P>(
6283
&mut self,
6384
p: P,
@@ -69,6 +90,26 @@ extension_trait! {
6990
RFindFuture::new(self, p)
7091
}
7192

93+
#[doc = r#"
94+
# Examples
95+
96+
Basic usage:
97+
98+
```
99+
# fn main() { async_std::task::block_on(async {
100+
#
101+
use async_std::stream::Sample;
102+
use async_std::stream::double_ended::DoubleEndedStreamExt;
103+
104+
let s = Sample::from(vec![1, 2, 3, 4, 5]);
105+
106+
let second = s.rfold(0, |acc, v| v + acc).await;
107+
108+
assert_eq!(second, 15);
109+
#
110+
# }) }
111+
```
112+
"#]
72113
fn rfold<B, F>(
73114
self,
74115
accum: B,

src/stream/double_ended_stream.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,3 @@ pub trait DoubleEndedStream: Stream {
2222
/// [trait-level]: trait.DoubleEndedStream.html
2323
fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
2424
}
25-
26-
pub struct Sample<T> {
27-
inner: Vec<T>,
28-
}
29-
30-
impl<T> From<Vec<T>> for Sample<T> {
31-
fn from(data: Vec<T>) -> Self {
32-
Sample { inner: data }
33-
}
34-
}
35-
36-
impl<T> Unpin for Sample<T> {}
37-
38-
impl<T> Stream for Sample<T> {
39-
type Item = T;
40-
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
41-
if self.inner.len() > 0 {
42-
return Poll::Ready(Some(self.inner.remove(0)));
43-
}
44-
return Poll::Ready(None);
45-
}
46-
}
47-
48-
impl<T> DoubleEndedStream for Sample<T> {
49-
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
50-
Poll::Ready(self.inner.pop())
51-
}
52-
}

src/stream/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,17 @@ pub use once::{once, Once};
307307
pub use repeat::{repeat, Repeat};
308308
pub use repeat_with::{repeat_with, RepeatWith};
309309
pub use stream::*;
310+
pub use crate::stream::sample::Sample;
310311

311-
pub(crate) mod stream;
312+
pub mod stream;
312313

313314
mod empty;
314315
mod from_fn;
315316
mod from_iter;
316317
mod once;
317318
mod repeat;
318319
mod repeat_with;
320+
mod sample;
319321

320322
cfg_unstable! {
321323
pub mod double_ended;

src/stream/sample.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::stream::Stream;
2+
3+
use std::pin::Pin;
4+
use std::task::{Context, Poll};
5+
use crate::stream::DoubleEndedStream;
6+
7+
pub struct Sample<T> {
8+
inner: Vec<T>,
9+
}
10+
11+
impl<T> From<Vec<T>> for Sample<T> {
12+
fn from(data: Vec<T>) -> Self {
13+
Sample { inner: data }
14+
}
15+
}
16+
17+
impl<T> Unpin for Sample<T> {}
18+
19+
impl<T> Stream for Sample<T> {
20+
type Item = T;
21+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
22+
if self.inner.len() > 0 {
23+
return Poll::Ready(Some(self.inner.remove(0)));
24+
}
25+
return Poll::Ready(None);
26+
}
27+
}
28+
29+
impl<T> DoubleEndedStream for Sample<T> {
30+
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
31+
Poll::Ready(self.inner.pop())
32+
}
33+
}

0 commit comments

Comments
 (0)