Skip to content

Commit 10a164e

Browse files
committed
Add stream::iter implementation
1 parent 10ebbec commit 10a164e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Stream
4343
- [ ] stream::for_each
4444
- [ ] stream::for_each_concurrent
4545
- [x] stream::into_future
46-
- [ ] stream::iter
46+
- [x] stream::iter
4747
- [x] stream::map
4848
- [x] stream::next
4949
- [ ] stream::skip

src/stream.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use futures::stream::Stream;
22
use futures::future::Future;
33
use core::pin::Pin;
4+
use core::iter::IntoIterator;
5+
46
use pin_utils::pin_mut;
57

68
pub async fn next<St>(stream: &mut St) -> Option<St::Item>
@@ -80,6 +82,16 @@ pub async fn into_future<St>(stream: St) -> (Option<St::Item>, impl Stream<Item
8082
(next_item, stream)
8183
}
8284

85+
pub fn iter<I>(i: I) -> impl Stream<Item = I::Item>
86+
where I: IntoIterator,
87+
{
88+
use core::task::Poll;
89+
let mut iter = i.into_iter();
90+
futures::stream::poll_fn(move |_| -> Poll<Option<I::Item>> {
91+
Poll::Ready(iter.next())
92+
})
93+
}
94+
8395
#[cfg(test)]
8496
mod tests {
8597
use futures::{executor, stream};
@@ -144,4 +156,12 @@ mod tests {
144156
let (item, _) = executor::block_on(into_future(stream));
145157
assert_eq!(None, item);
146158
}
159+
160+
#[test]
161+
fn test_iter() {
162+
let stream = iter(1..=5);
163+
164+
let collection : Vec<i32> = executor::block_on(collect(stream));
165+
assert_eq!(collection, vec![1, 2, 3, 4, 5]);
166+
}
147167
}

0 commit comments

Comments
 (0)