Skip to content

Commit 4a265a0

Browse files
committed
Add stream::concat implementation
1 parent 5f7e5b3 commit 4a265a0

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
@@ -35,7 +35,7 @@ Stream
3535
- [ ] stream::chain
3636
- [ ] stream::chunks
3737
- [x] stream::collect
38-
- [ ] stream::concat
38+
- [x] stream::concat
3939
- [x] stream::filter
4040
- [x] stream::filter_map
4141
- [ ] stream::flatten

src/stream.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ pub fn iter<I>(i: I) -> impl Stream<Item = I::Item>
9292
})
9393
}
9494

95+
pub async fn concat<St>(stream: St) -> St::Item
96+
where St: Stream,
97+
St::Item: Extend<<St::Item as IntoIterator>::Item>,
98+
St::Item: IntoIterator,
99+
St::Item: Default,
100+
{
101+
pin_mut!(stream);
102+
let mut collection = <St::Item>::default();
103+
while let Some(item) = await!(next(&mut stream)) {
104+
collection.extend(item);
105+
}
106+
collection
107+
}
108+
95109
#[cfg(test)]
96110
mod tests {
97111
use futures::executor;
@@ -164,4 +178,12 @@ mod tests {
164178
let collection : Vec<i32> = executor::block_on(collect(stream));
165179
assert_eq!(collection, vec![1, 2, 3, 4, 5]);
166180
}
181+
182+
#[test]
183+
fn test_concat() {
184+
let stream = iter(vec![vec![1, 2], vec![3], vec![4, 5]]);
185+
186+
let collection : Vec<i32> = executor::block_on(concat(stream));
187+
assert_eq!(collection, vec![1, 2, 3, 4, 5]);
188+
}
167189
}

0 commit comments

Comments
 (0)