Skip to content

Commit

Permalink
Merge 1b1eb8a into 9e3a2dd
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Oct 10, 2019
2 parents 9e3a2dd + 1b1eb8a commit 2ccda5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -53,6 +53,7 @@ Stream
- [x] stream::iter
- [x] stream::map
- [x] stream::next
- [x] stream::once
- [x] stream::poll_fn
- [x] stream::repeat
- [x] stream::skip
Expand Down
31 changes: 31 additions & 0 deletions src/stream.rs
Expand Up @@ -38,6 +38,29 @@ where
poll_fn(move |context| Pin::new(&mut stream).poll_next(context))
}

/// Creates a stream of a single element.
///
/// ```
/// # futures::executor::block_on(async {
/// use futures_async_combinators::stream::{once, collect};
///
/// let stream = once(async { 17 });
/// let collection: Vec<i32> = collect(stream).await;
/// assert_eq!(collection, vec![17]);
/// # });
/// ```
pub fn once<F>(future: F) -> impl Stream<Item = F::Output>
where
F: Future,
{
crate::stream::unfold(Some(future), |f| async move {
match f {
Some(f) => Some((f.await, None)),
None => None
}
})
}

/// Collect all of the values of this stream into a vector, returning a
/// future representing the result of that computation.
///
Expand Down Expand Up @@ -875,6 +898,14 @@ mod tests {
assert_eq!(executor::block_on(next(&mut stream)), None);
}

#[test]
fn test_once() {
let stream = once(async { 17 });

let collection: Vec<i32> = executor::block_on(collect(stream));
assert_eq!(collection, vec![17]);
}

#[test]
fn test_collect() {
let stream = iter(1..=5);
Expand Down

0 comments on commit 2ccda5b

Please sign in to comment.