forked from mirror/async-std
Make cycle a function on the stream type
This commit is contained in:
parent
b979773505
commit
5aadc5e4e9
3 changed files with 45 additions and 32 deletions
|
@ -300,7 +300,6 @@
|
|||
//! [`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};
|
||||
|
@ -313,7 +312,6 @@ pub use stream::{
|
|||
|
||||
pub(crate) mod stream;
|
||||
|
||||
mod cycle;
|
||||
mod empty;
|
||||
mod from_fn;
|
||||
mod from_iter;
|
||||
|
|
|
@ -22,6 +22,17 @@ enum CycleState {
|
|||
FromBuffer,
|
||||
}
|
||||
|
||||
impl<T: Clone, S: Stream<Item = T>,> Cycle<S, T> {
|
||||
pub fn new(source: S) -> Cycle<S, T> {
|
||||
Cycle {
|
||||
source,
|
||||
index: 0,
|
||||
buffer: Vec::new(),
|
||||
state: CycleState::FromStream,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, T> Stream for Cycle<S, T>
|
||||
where
|
||||
S: Stream<Item = T>,
|
||||
|
@ -57,33 +68,3 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Creats a stream that yields the provided values infinitely and in order.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
///
|
||||
/// ```
|
||||
/// # async_std::task::block_on(async {
|
||||
/// #
|
||||
/// use async_std::prelude::*;
|
||||
/// use async_std::stream;
|
||||
///
|
||||
/// let mut s = stream::cycle(stream::once(7));
|
||||
///
|
||||
/// assert_eq!(s.next().await, Some(7));
|
||||
/// assert_eq!(s.next().await, Some(7));
|
||||
/// assert_eq!(s.next().await, Some(7));
|
||||
/// assert_eq!(s.next().await, Some(7));
|
||||
/// assert_eq!(s.next().await, Some(7));
|
||||
/// #
|
||||
/// # })
|
||||
/// ```
|
||||
pub fn cycle<S: Stream<Item = T>, T: Clone>(source: S) -> impl Stream<Item = S::Item> {
|
||||
Cycle {
|
||||
source,
|
||||
index: 0,
|
||||
buffer: Vec::new(),
|
||||
state: CycleState::FromStream,
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@
|
|||
mod all;
|
||||
mod any;
|
||||
mod chain;
|
||||
mod cycle;
|
||||
mod cmp;
|
||||
mod cycle;
|
||||
mod copied;
|
||||
|
@ -91,6 +92,7 @@ use partial_cmp::PartialCmpFuture;
|
|||
use position::PositionFuture;
|
||||
use try_fold::TryFoldFuture;
|
||||
use try_for_each::TryForEachFuture;
|
||||
use cycle::Cycle;
|
||||
|
||||
pub use chain::Chain;
|
||||
pub use copied::Copied;
|
||||
|
@ -411,6 +413,38 @@ extension_trait! {
|
|||
Copied::new(self)
|
||||
}
|
||||
|
||||
#[doc = r#"
|
||||
Creats a stream that yields the provided values infinitely and in order.
|
||||
|
||||
# Examples
|
||||
|
||||
Basic usage:
|
||||
|
||||
```
|
||||
# async_std::task::block_on(async {
|
||||
#
|
||||
use async_std::prelude::*;
|
||||
use async_std::stream;
|
||||
|
||||
let mut s = stream::once(7).cycle();
|
||||
|
||||
assert_eq!(s.next().await, Some(7));
|
||||
assert_eq!(s.next().await, Some(7));
|
||||
assert_eq!(s.next().await, Some(7));
|
||||
assert_eq!(s.next().await, Some(7));
|
||||
assert_eq!(s.next().await, Some(7));
|
||||
#
|
||||
# })
|
||||
```
|
||||
"#]
|
||||
fn cycle(self) -> Cycle<Self, Self::Item>
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Item: Clone,
|
||||
{
|
||||
Cycle::new(self)
|
||||
}
|
||||
|
||||
#[doc = r#"
|
||||
Creates a stream that gives the current element's count as well as the next value.
|
||||
|
||||
|
|
Loading…
Reference in a new issue