Skip to content

Commit 660f585

Browse files
committed
Add core::stream::from_iter
1 parent b6f3cb9 commit 660f585

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

library/core/src/stream/from_iter.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::pin::Pin;
2+
3+
use crate::stream::Stream;
4+
use crate::task::{Context, Poll};
5+
6+
/// A stream that was created from iterator.
7+
///
8+
/// This stream is created by the [`from_iter`] function.
9+
/// See it documentation for more.
10+
///
11+
/// [`from_iter`]: fn.from_iter.html
12+
#[unstable(feature = "stream_from_iter", issue = "81798")]
13+
#[derive(Clone, Debug)]
14+
pub struct FromIter<I> {
15+
iter: I,
16+
}
17+
18+
#[unstable(feature = "stream_from_iter", issue = "81798")]
19+
impl<I> Unpin for FromIter<I> {}
20+
21+
/// Converts an iterator into a stream.
22+
#[unstable(feature = "stream_from_iter", issue = "81798")]
23+
pub fn from_iter<I: IntoIterator>(iter: I) -> FromIter<I::IntoIter> {
24+
FromIter { iter: iter.into_iter() }
25+
}
26+
27+
#[unstable(feature = "stream_from_iter", issue = "81798")]
28+
impl<I: Iterator> Stream for FromIter<I> {
29+
type Item = I::Item;
30+
31+
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
32+
Poll::Ready(self.iter.next())
33+
}
34+
35+
fn size_hint(&self) -> (usize, Option<usize>) {
36+
self.iter.size_hint()
37+
}
38+
}

library/core/src/stream/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
//! warning: unused result that must be used: streams do nothing unless polled
123123
//! ```
124124
125+
mod from_iter;
125126
mod stream;
126127

128+
pub use from_iter::{from_iter, FromIter};
127129
pub use stream::Stream;

0 commit comments

Comments
 (0)