forked from mirror/async-std
Cycle over a known set of values.
This commit is contained in:
parent
a096d5ec2d
commit
486f9a964c
4 changed files with 66 additions and 39 deletions
62
src/stream/cycle.rs
Normal file
62
src/stream/cycle.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use std::pin::Pin;
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::stream::Stream;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
pin_project! {
|
||||
/// A stream that will repeatedly yield the same list of elements
|
||||
pub struct Cycle<T> {
|
||||
source: Vec<T>,
|
||||
index: usize,
|
||||
len: usize,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> Stream for Cycle<T> {
|
||||
type Item = T;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let value = self.source[self.index];
|
||||
|
||||
let next = self.index + 1;
|
||||
|
||||
if next >= self.len {
|
||||
self.as_mut().index = 0;
|
||||
} else {
|
||||
self.as_mut().index = next;
|
||||
}
|
||||
|
||||
Poll::Ready(Some(value))
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// # async_std::task::block_on(async {
|
||||
/// #
|
||||
/// use async_std::prelude::*;
|
||||
/// use async_std::stream;
|
||||
///
|
||||
/// let mut s = stream::cycle(vec![1,2,3]);
|
||||
///
|
||||
/// assert_eq!(s.next().await, Some(1));
|
||||
/// assert_eq!(s.next().await, Some(2));
|
||||
/// assert_eq!(s.next().await, Some(3));
|
||||
/// assert_eq!(s.next().await, Some(1));
|
||||
/// assert_eq!(s.next().await, Some(2));
|
||||
/// #
|
||||
/// # })
|
||||
/// ```
|
||||
pub fn cycle<T: Copy>(source: Vec<T>) -> impl Stream<Item = T> {
|
||||
let len = source.len();
|
||||
Cycle {
|
||||
source,
|
||||
index: 0,
|
||||
len,
|
||||
}
|
||||
}
|
|
@ -300,6 +300,7 @@
|
|||
//! [`take`]: trait.Stream.html#method.take
|
||||
//! [`min`]: trait.Stream.html#method.min
|
||||
|
||||
pub use cycle::{cycle, Cycle};
|
||||
pub use empty::{empty, Empty};
|
||||
pub use from_fn::{from_fn, FromFn};
|
||||
pub use from_iter::{from_iter, FromIter};
|
||||
|
@ -312,6 +313,7 @@ pub use stream::{
|
|||
|
||||
pub(crate) mod stream;
|
||||
|
||||
mod cycle;
|
||||
mod empty;
|
||||
mod from_fn;
|
||||
mod from_iter;
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
use std::pin::Pin;
|
||||
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
use crate::stream::Stream;
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
/// A stream that will repeatedly yield the same list of elements
|
||||
pub struct Cycle<T> {
|
||||
source: Vec<T>,
|
||||
index: usize,
|
||||
}
|
||||
|
||||
impl<T: Copy> Stream for Cycle<T> {
|
||||
type Item = T;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||
///
|
||||
/// let values = vec![1,2,3];
|
||||
///
|
||||
/// # Ok(()) }) }
|
||||
///```
|
||||
fn cycle<T: Copy>(values: Vec<T>) -> impl Stream<Item = T> {
|
||||
Cycle {
|
||||
source: values,
|
||||
index: 0,
|
||||
}
|
||||
}
|
|
@ -24,10 +24,11 @@
|
|||
mod all;
|
||||
mod any;
|
||||
mod chain;
|
||||
mod enumerate;
|
||||
mod cmp;
|
||||
mod cycle;
|
||||
mod copied;
|
||||
mod cmp;
|
||||
mod enumerate;
|
||||
mod eq;
|
||||
mod filter;
|
||||
mod filter_map;
|
||||
|
|
Loading…
Reference in a new issue