Skip to content

Commit 5aadc5e

Browse files
committed
Make cycle a function on the stream type
1 parent b979773 commit 5aadc5e

File tree

3 files changed

+45
-32
lines changed

3 files changed

+45
-32
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: 34 additions & 0 deletions
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 cycle;
2930
mod copied;
@@ -91,6 +92,7 @@ use partial_cmp::PartialCmpFuture;
9192
use position::PositionFuture;
9293
use try_fold::TryFoldFuture;
9394
use try_for_each::TryForEachFuture;
95+
use cycle::Cycle;
9496

9597
pub use chain::Chain;
9698
pub use copied::Copied;
@@ -411,6 +413,38 @@ extension_trait! {
411413
Copied::new(self)
412414
}
413415

416+
#[doc = r#"
417+
Creats a stream that yields the provided values infinitely and in order.
418+
419+
# Examples
420+
421+
Basic usage:
422+
423+
```
424+
# async_std::task::block_on(async {
425+
#
426+
use async_std::prelude::*;
427+
use async_std::stream;
428+
429+
let mut s = stream::once(7).cycle();
430+
431+
assert_eq!(s.next().await, Some(7));
432+
assert_eq!(s.next().await, Some(7));
433+
assert_eq!(s.next().await, Some(7));
434+
assert_eq!(s.next().await, Some(7));
435+
assert_eq!(s.next().await, Some(7));
436+
#
437+
# })
438+
```
439+
"#]
440+
fn cycle(self) -> Cycle<Self, Self::Item>
441+
where
442+
Self: Sized,
443+
Self::Item: Clone,
444+
{
445+
Cycle::new(self)
446+
}
447+
414448
#[doc = r#"
415449
Creates a stream that gives the current element's count as well as the next value.
416450

0 commit comments

Comments
 (0)