Skip to content

Commit 3437b24

Browse files
committed
Add stream::into_future implementation
1 parent 744830b commit 3437b24

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Stream
4242
- [ ] stream::fold
4343
- [ ] stream::for_each
4444
- [ ] stream::for_each_concurrent
45-
- [ ] stream::into_future
45+
- [x] stream::into_future
4646
- [ ] stream::iter
4747
- [x] stream::map
4848
- [x] stream::next

src/stream.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ pub fn filter_map<St, Fut, F, U>(stream: St, f: F) -> impl Stream<Item = U>
7272
})
7373
}
7474

75+
pub async fn into_future<St>(stream: St) -> (Option<St::Item>, impl Stream<Item = St::Item>)
76+
where St: Stream + Unpin,
77+
{
78+
let mut stream = stream;
79+
let next_item = await!(next(&mut stream));
80+
(next_item, stream)
81+
}
82+
7583
#[cfg(test)]
7684
mod tests {
7785
use futures::{executor, stream};
@@ -122,4 +130,18 @@ mod tests {
122130

123131
assert_eq!(vec![3, 5, 7, 9, 11], executor::block_on(collect::<_, Vec<_>>(evens)));
124132
}
133+
134+
#[test]
135+
fn test_into_future() {
136+
let stream = stream::iter(1..=2);
137+
138+
let (item, stream) = executor::block_on(into_future(stream));
139+
assert_eq!(Some(1), item);
140+
141+
let (item, stream) = executor::block_on(into_future(stream));
142+
assert_eq!(Some(2), item);
143+
144+
let (item, _) = executor::block_on(into_future(stream));
145+
assert_eq!(None, item);
146+
}
125147
}

0 commit comments

Comments
 (0)