mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-28 15:19:41 +00:00
adds stream::step_by combinator
This commit is contained in:
parent
99724497b3
commit
e74c0cec1f
2 changed files with 86 additions and 0 deletions
|
@ -35,6 +35,7 @@ mod next;
|
||||||
mod nth;
|
mod nth;
|
||||||
mod scan;
|
mod scan;
|
||||||
mod skip;
|
mod skip;
|
||||||
|
mod step_by;
|
||||||
mod take;
|
mod take;
|
||||||
mod zip;
|
mod zip;
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ pub use filter::Filter;
|
||||||
pub use fuse::Fuse;
|
pub use fuse::Fuse;
|
||||||
pub use scan::Scan;
|
pub use scan::Scan;
|
||||||
pub use skip::Skip;
|
pub use skip::Skip;
|
||||||
|
pub use step_by::StepBy;
|
||||||
pub use take::Take;
|
pub use take::Take;
|
||||||
pub use zip::Zip;
|
pub use zip::Zip;
|
||||||
|
|
||||||
|
@ -228,6 +230,40 @@ pub trait Stream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a stream that yields each `step`th element.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This method will panic if the given step is `0`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Basic usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use async_std::prelude::*;
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
///
|
||||||
|
/// let s: VecDeque<_> = vec![0u8, 1, 2, 3, 4].into_iter().collect();
|
||||||
|
/// let mut stepped = s.step_by(2);
|
||||||
|
///
|
||||||
|
/// assert_eq!(stepped.next().await, Some(0));
|
||||||
|
/// assert_eq!(stepped.next().await, Some(2));
|
||||||
|
/// assert_eq!(stepped.next().await, Some(4));
|
||||||
|
/// assert_eq!(stepped.next().await, None);
|
||||||
|
///
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
fn step_by(self, step: usize) -> StepBy<Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
StepBy::new(self, step)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a stream that gives the current element's count as well as the next value.
|
/// Creates a stream that gives the current element's count as well as the next value.
|
||||||
///
|
///
|
||||||
/// # Overflow behaviour.
|
/// # Overflow behaviour.
|
||||||
|
|
50
src/stream/stream/step_by.rs
Normal file
50
src/stream/stream/step_by.rs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use crate::stream::Stream;
|
||||||
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
|
/// A stream that steps a given amount of elements of another stream.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct StepBy<S> {
|
||||||
|
stream: S,
|
||||||
|
step: usize,
|
||||||
|
i: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> StepBy<S> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: S);
|
||||||
|
pin_utils::unsafe_unpinned!(step: usize);
|
||||||
|
pin_utils::unsafe_unpinned!(i: usize);
|
||||||
|
|
||||||
|
pub(crate) fn new(stream: S, step: usize) -> Self {
|
||||||
|
StepBy {
|
||||||
|
stream,
|
||||||
|
step: step.checked_sub(1).unwrap(),
|
||||||
|
i: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S> Stream for StepBy<S>
|
||||||
|
where
|
||||||
|
S: Stream,
|
||||||
|
{
|
||||||
|
type Item = S::Item;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
loop {
|
||||||
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
|
||||||
|
match next {
|
||||||
|
Some(v) => match self.i {
|
||||||
|
0 => {
|
||||||
|
*self.as_mut().i() = self.step;
|
||||||
|
return Poll::Ready(Some(v));
|
||||||
|
}
|
||||||
|
_ => *self.as_mut().i() -= 1,
|
||||||
|
},
|
||||||
|
None => return Poll::Ready(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue