Skip to content

Commit e74c0ce

Browse files
committed
adds stream::step_by combinator
1 parent 9972449 commit e74c0ce

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/stream/stream/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ mod next;
3535
mod nth;
3636
mod scan;
3737
mod skip;
38+
mod step_by;
3839
mod take;
3940
mod zip;
4041

4142
pub use filter::Filter;
4243
pub use fuse::Fuse;
4344
pub use scan::Scan;
4445
pub use skip::Skip;
46+
pub use step_by::StepBy;
4547
pub use take::Take;
4648
pub use zip::Zip;
4749

@@ -228,6 +230,40 @@ pub trait Stream {
228230
}
229231
}
230232

233+
/// Creates a stream that yields each `step`th element.
234+
///
235+
/// # Panics
236+
///
237+
/// This method will panic if the given step is `0`.
238+
///
239+
/// # Examples
240+
///
241+
/// Basic usage:
242+
///
243+
/// ```
244+
/// # fn main() { async_std::task::block_on(async {
245+
/// #
246+
/// use async_std::prelude::*;
247+
/// use std::collections::VecDeque;
248+
///
249+
/// let s: VecDeque<_> = vec![0u8, 1, 2, 3, 4].into_iter().collect();
250+
/// let mut stepped = s.step_by(2);
251+
///
252+
/// assert_eq!(stepped.next().await, Some(0));
253+
/// assert_eq!(stepped.next().await, Some(2));
254+
/// assert_eq!(stepped.next().await, Some(4));
255+
/// assert_eq!(stepped.next().await, None);
256+
///
257+
/// #
258+
/// # }) }
259+
/// ```
260+
fn step_by(self, step: usize) -> StepBy<Self>
261+
where
262+
Self: Sized,
263+
{
264+
StepBy::new(self, step)
265+
}
266+
231267
/// Creates a stream that gives the current element's count as well as the next value.
232268
///
233269
/// # Overflow behaviour.

src/stream/stream/step_by.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::pin::Pin;
2+
3+
use crate::stream::Stream;
4+
use crate::task::{Context, Poll};
5+
6+
/// A stream that steps a given amount of elements of another stream.
7+
#[derive(Debug)]
8+
pub struct StepBy<S> {
9+
stream: S,
10+
step: usize,
11+
i: usize,
12+
}
13+
14+
impl<S> StepBy<S> {
15+
pin_utils::unsafe_pinned!(stream: S);
16+
pin_utils::unsafe_unpinned!(step: usize);
17+
pin_utils::unsafe_unpinned!(i: usize);
18+
19+
pub(crate) fn new(stream: S, step: usize) -> Self {
20+
StepBy {
21+
stream,
22+
step: step.checked_sub(1).unwrap(),
23+
i: 0,
24+
}
25+
}
26+
}
27+
28+
impl<S> Stream for StepBy<S>
29+
where
30+
S: Stream,
31+
{
32+
type Item = S::Item;
33+
34+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
35+
loop {
36+
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
37+
38+
match next {
39+
Some(v) => match self.i {
40+
0 => {
41+
*self.as_mut().i() = self.step;
42+
return Poll::Ready(Some(v));
43+
}
44+
_ => *self.as_mut().i() -= 1,
45+
},
46+
None => return Poll::Ready(None),
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)