Skip to content

Commit 2dee289

Browse files
committed
Add FlatMap struct
1 parent bb14164 commit 2dee289

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/stream/stream/flatten.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
11
use std::pin::Pin;
22

3+
use crate::prelude::*;
4+
use crate::stream::stream::map::Map;
35
use crate::stream::{IntoStream, Stream};
46
use crate::task::{Context, Poll};
57

8+
#[allow(missing_debug_implementations)]
9+
pub struct FlatMap<S: Stream, U: IntoStream, F> {
10+
inner: FlattenCompat<Map<S, F, S::Item, U>, U>,
11+
}
12+
13+
impl<S, U, F> FlatMap<S, U, F>
14+
where
15+
S: Stream,
16+
U: IntoStream,
17+
F: FnMut(S::Item) -> U,
18+
{
19+
pin_utils::unsafe_pinned!(inner: FlattenCompat<Map<S, F, S::Item, U>, U>);
20+
21+
pub fn new(stream: S, f: F) -> FlatMap<S, U, F> {
22+
FlatMap {
23+
inner: FlattenCompat::new(stream.map(f)),
24+
}
25+
}
26+
}
27+
28+
impl<S, U, F> Stream for FlatMap<S, U, F>
29+
where
30+
S: Stream<Item: IntoStream<IntoStream = U, Item = U::Item>> + std::marker::Unpin,
31+
S::Item: std::marker::Unpin,
32+
U: Stream + std::marker::Unpin,
33+
F: FnMut(S::Item) -> U + std::marker::Unpin,
34+
{
35+
type Item = U::Item;
36+
37+
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
38+
self.as_mut().inner().poll_next(cx)
39+
}
40+
}
41+
642
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to
743
/// this type.
844
#[derive(Clone, Debug)]
945
struct FlattenCompat<S, U> {
1046
stream: S,
1147
frontiter: Option<U>,
1248
}
49+
1350
impl<S, U> FlattenCompat<S, U> {
1451
pin_utils::unsafe_unpinned!(stream: S);
1552
pin_utils::unsafe_unpinned!(frontiter: Option<U>);

0 commit comments

Comments
 (0)