File tree Expand file tree Collapse file tree 3 files changed +45
-32
lines changed Expand file tree Collapse file tree 3 files changed +45
-32
lines changed Original file line number Diff line number Diff line change 300300//! [`take`]: trait.Stream.html#method.take
301301//! [`min`]: trait.Stream.html#method.min
302302
303- pub use cycle:: { cycle, Cycle } ;
304303pub use empty:: { empty, Empty } ;
305304pub use from_fn:: { from_fn, FromFn } ;
306305pub use from_iter:: { from_iter, FromIter } ;
@@ -313,7 +312,6 @@ pub use stream::{
313312
314313pub ( crate ) mod stream;
315314
316- mod cycle;
317315mod empty;
318316mod from_fn;
319317mod from_iter;
Original file line number Diff line number Diff 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+
2536impl < S , T > Stream for Cycle < S , T >
2637where
2738 S : Stream < Item = T > ,
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- }
Original file line number Diff line number Diff line change 2424mod all;
2525mod any;
2626mod chain;
27+ mod cycle;
2728mod cmp;
2829mod cycle;
2930mod copied;
@@ -91,6 +92,7 @@ use partial_cmp::PartialCmpFuture;
9192use position:: PositionFuture ;
9293use try_fold:: TryFoldFuture ;
9394use try_for_each:: TryForEachFuture ;
95+ use cycle:: Cycle ;
9496
9597pub use chain:: Chain ;
9698pub 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
You can’t perform that action at this time.
0 commit comments