Skip to content

Commit 735d604

Browse files
committed
Adds stream::repeat_with
1 parent 6fe958f commit 735d604

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/stream/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ use cfg_if::cfg_if;
2626
pub use empty::{empty, Empty};
2727
pub use once::{once, Once};
2828
pub use repeat::{repeat, Repeat};
29+
pub use repeat_with::{repeat_with, RepeatWith};
2930
pub use stream::{Chain, Filter, Fuse, Inspect, Scan, Skip, SkipWhile, StepBy, Stream, Take, Zip};
3031

3132
pub(crate) mod stream;
3233

3334
mod empty;
3435
mod once;
3536
mod repeat;
37+
mod repeat_with;
3638

3739
cfg_if! {
3840
if #[cfg(any(feature = "unstable", feature = "docs"))] {

src/stream/repeat_with.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use std::marker::PhantomData;
2+
use std::pin::Pin;
3+
4+
use crate::future::Future;
5+
use crate::stream::Stream;
6+
use crate::task::{Context, Poll};
7+
8+
/// A stream that repeats elements of type `T` endlessly by applying a provided clousre.
9+
///
10+
/// This stream is constructed by the [`repeat_with`] function.
11+
///
12+
/// [`repeat_with`]: fn.repeat_with.html
13+
#[derive(Debug)]
14+
pub struct RepeatWith<F, Fut, A> {
15+
f: F,
16+
future: Option<Fut>,
17+
__a: PhantomData<A>,
18+
}
19+
20+
/// Creates a new stream that repeats elements of type `A` endlessly by applying the provided closure.
21+
///
22+
/// # Examples
23+
///
24+
/// Basic usage:
25+
///
26+
/// ```
27+
/// # fn main() { async_std::task::block_on(async {
28+
/// #
29+
/// use async_std::prelude::*;
30+
/// use async_std::stream;
31+
///
32+
/// let s = stream::repeat_with(|| async { 1 });
33+
///
34+
///
35+
/// pin_utils::pin_mut!(s);
36+
///
37+
/// assert_eq!(s.next().await, Some(1));
38+
/// assert_eq!(s.next().await, Some(1));
39+
/// assert_eq!(s.next().await, Some(1));
40+
/// assert_eq!(s.next().await, Some(1));
41+
///
42+
/// # }) }
43+
/// ```
44+
///
45+
/// Going finite:
46+
///
47+
/// ```
48+
/// # fn main() { async_std::task::block_on(async {
49+
/// #
50+
/// use async_std::prelude::*;
51+
/// use async_std::stream;
52+
///
53+
/// let s = stream::repeat_with(|| async { 1u8 }).take(2);
54+
///
55+
///
56+
/// pin_utils::pin_mut!(s);
57+
///
58+
/// assert_eq!(s.next().await, Some(1));
59+
/// assert_eq!(s.next().await, Some(1));
60+
/// assert_eq!(s.next().await, None);
61+
///
62+
/// # }) }
63+
/// ```
64+
pub fn repeat_with<F, Fut, A>(repeater: F) -> RepeatWith<F, Fut, A> {
65+
RepeatWith {
66+
f: repeater,
67+
future: None,
68+
__a: PhantomData,
69+
}
70+
}
71+
72+
impl<F, Fut, A> RepeatWith<F, Fut, A> {
73+
pin_utils::unsafe_unpinned!(f: F);
74+
pin_utils::unsafe_pinned!(future: Option<Fut>);
75+
}
76+
77+
impl<F, Fut, A> Stream for RepeatWith<F, Fut, A>
78+
where
79+
F: FnMut() -> Fut,
80+
Fut: Future<Output = A>,
81+
{
82+
type Item = A;
83+
84+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
85+
loop {
86+
match self.future.is_some() {
87+
true => {
88+
let res =
89+
futures_core::ready!(self.as_mut().future().as_pin_mut().unwrap().poll(cx));
90+
91+
self.as_mut().future().set(None);
92+
93+
return Poll::Ready(Some(res));
94+
}
95+
false => {
96+
let fut = (self.as_mut().f())();
97+
98+
self.as_mut().future().set(Some(fut));
99+
}
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)