2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-19 20:13:51 +00:00

Make cycle a function on the stream type

This commit is contained in:
Felipe Sere 2019-10-29 17:06:56 -05:00
parent b979773505
commit 5aadc5e4e9
3 changed files with 45 additions and 32 deletions

View file

@ -300,7 +300,6 @@
//! [`take`]: trait.Stream.html#method.take //! [`take`]: trait.Stream.html#method.take
//! [`min`]: trait.Stream.html#method.min //! [`min`]: trait.Stream.html#method.min
pub use cycle::{cycle, Cycle};
pub use empty::{empty, Empty}; pub use empty::{empty, Empty};
pub use from_fn::{from_fn, FromFn}; pub use from_fn::{from_fn, FromFn};
pub use from_iter::{from_iter, FromIter}; pub use from_iter::{from_iter, FromIter};
@ -313,7 +312,6 @@ pub use stream::{
pub(crate) mod stream; pub(crate) mod stream;
mod cycle;
mod empty; mod empty;
mod from_fn; mod from_fn;
mod from_iter; mod from_iter;

View file

@ -22,6 +22,17 @@ enum CycleState {
FromBuffer, 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> impl<S, T> Stream for Cycle<S, T>
where where
S: Stream<Item = T>, 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,
}
}

View file

@ -24,6 +24,7 @@
mod all; mod all;
mod any; mod any;
mod chain; mod chain;
mod cycle;
mod cmp; mod cmp;
mod cycle; mod cycle;
mod copied; mod copied;
@ -91,6 +92,7 @@ use partial_cmp::PartialCmpFuture;
use position::PositionFuture; use position::PositionFuture;
use try_fold::TryFoldFuture; use try_fold::TryFoldFuture;
use try_for_each::TryForEachFuture; use try_for_each::TryForEachFuture;
use cycle::Cycle;
pub use chain::Chain; pub use chain::Chain;
pub use copied::Copied; pub use copied::Copied;
@ -411,6 +413,38 @@ extension_trait! {
Copied::new(self) 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#" #[doc = r#"
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.