diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 449c484..07eecf2 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -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; diff --git a/src/stream/cycle.rs b/src/stream/stream/cycle.rs similarity index 65% rename from src/stream/cycle.rs rename to src/stream/stream/cycle.rs index df8b956..518a804 100644 --- a/src/stream/cycle.rs +++ b/src/stream/stream/cycle.rs @@ -22,6 +22,17 @@ enum CycleState { FromBuffer, } +impl,> Cycle { + pub fn new(source: S) -> Cycle { + Cycle { + source, + index: 0, + buffer: Vec::new(), + state: CycleState::FromStream, + } + } +} + impl Stream for Cycle where S: Stream, @@ -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, T: Clone>(source: S) -> impl Stream { - Cycle { - source, - index: 0, - buffer: Vec::new(), - state: CycleState::FromStream, - } -} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index d46ae87..274992b 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -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 + 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.