Skip to content

Commit 2ed8c8f

Browse files
committed
Make cycle a function on the stream type
1 parent b8b00a7 commit 2ed8c8f

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/stream/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@
300300
//! [`take`]: trait.Stream.html#method.take
301301
//! [`min`]: trait.Stream.html#method.min
302302
303-
pub use cycle::{cycle, Cycle};
304303
pub use empty::{empty, Empty};
305304
pub use from_fn::{from_fn, FromFn};
306305
pub use from_iter::{from_iter, FromIter};
@@ -313,7 +312,6 @@ pub use stream::{
313312

314313
pub(crate) mod stream;
315314

316-
mod cycle;
317315
mod empty;
318316
mod from_fn;
319317
mod from_iter;

src/stream/cycle.rs renamed to src/stream/stream/cycle.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ enum CycleState {
2222
FromBuffer,
2323
}
2424

25+
impl<T: Clone, S: Stream<Item = T>,> Cycle<S, T> {
26+
pub fn new(source: S) -> Cycle<S, T> {
27+
Cycle {
28+
source,
29+
index: 0,
30+
buffer: Vec::new(),
31+
state: CycleState::FromStream,
32+
}
33+
}
34+
}
35+
2536
impl<S, T> Stream for Cycle<S, T>
2637
where
2738
S: Stream<Item = T>,
@@ -57,33 +68,3 @@ where
5768
}
5869
}
5970

60-
/// Creats a stream that yields the provided values infinitely and in order.
61-
///
62-
/// # Examples
63-
///
64-
/// Basic usage:
65-
///
66-
/// ```
67-
/// # async_std::task::block_on(async {
68-
/// #
69-
/// use async_std::prelude::*;
70-
/// use async_std::stream;
71-
///
72-
/// let mut s = stream::cycle(stream::once(7));
73-
///
74-
/// assert_eq!(s.next().await, Some(7));
75-
/// assert_eq!(s.next().await, Some(7));
76-
/// assert_eq!(s.next().await, Some(7));
77-
/// assert_eq!(s.next().await, Some(7));
78-
/// assert_eq!(s.next().await, Some(7));
79-
/// #
80-
/// # })
81-
/// ```
82-
pub fn cycle<S: Stream<Item = T>, T: Clone>(source: S) -> impl Stream<Item = S::Item> {
83-
Cycle {
84-
source,
85-
index: 0,
86-
buffer: Vec::new(),
87-
state: CycleState::FromStream,
88-
}
89-
}

src/stream/stream/mod.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
mod all;
2525
mod any;
2626
mod chain;
27+
mod cycle;
2728
mod cmp;
2829
mod enumerate;
2930
mod eq;
@@ -86,6 +87,7 @@ use partial_cmp::PartialCmpFuture;
8687
use position::PositionFuture;
8788
use try_fold::TryFoldFuture;
8889
use try_for_each::TryForEachFuture;
90+
use cycle::Cycle;
8991

9092
pub use chain::Chain;
9193
pub use filter::Filter;
@@ -369,6 +371,38 @@ extension_trait! {
369371
Chain::new(self, other)
370372
}
371373

374+
#[doc = r#"
375+
Creats a stream that yields the provided values infinitely and in order.
376+
377+
# Examples
378+
379+
Basic usage:
380+
381+
```
382+
# async_std::task::block_on(async {
383+
#
384+
use async_std::prelude::*;
385+
use async_std::stream;
386+
387+
let mut s = stream::once(7).cycle();
388+
389+
assert_eq!(s.next().await, Some(7));
390+
assert_eq!(s.next().await, Some(7));
391+
assert_eq!(s.next().await, Some(7));
392+
assert_eq!(s.next().await, Some(7));
393+
assert_eq!(s.next().await, Some(7));
394+
#
395+
# })
396+
```
397+
"#]
398+
fn cycle(self) -> Cycle<Self, Self::Item>
399+
where
400+
Self: Sized,
401+
Self::Item: Clone,
402+
{
403+
Cycle::new(self)
404+
}
405+
372406
#[doc = r#"
373407
Creates a stream that gives the current element's count as well as the next value.
374408
@@ -1590,7 +1624,7 @@ extension_trait! {
15901624
}
15911625

15921626
#[doc = r#"
1593-
Searches for an element in a Stream that satisfies a predicate, returning
1627+
Searches for an element in a Stream that satisfies a predicate, returning
15941628
its index.
15951629
15961630
# Examples

0 commit comments

Comments
 (0)