futures_rx/stream_ext/
inspect_done.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::{
    pin::Pin,
    task::{Context, Poll},
};

use futures::{
    stream::{Fuse, FusedStream},
    Stream, StreamExt,
};
use pin_project_lite::pin_project;

pin_project! {
    /// Stream for the [`inspect_done`](RxStreamExt::inspect_done) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct InspectDone<S: Stream, F> {
        #[pin]
        stream: Fuse<S>,
        f: F,
    }
}

impl<S: Stream, F> InspectDone<S, F> {
    pub(crate) fn new(stream: S, f: F) -> Self {
        Self {
            stream: stream.fuse(),
            f,
        }
    }
}

impl<S: Stream, F> FusedStream for InspectDone<S, F>
where
    F: FnMut(),
{
    fn is_terminated(&self) -> bool {
        self.stream.is_terminated()
    }
}

impl<S: Stream, F> Stream for InspectDone<S, F>
where
    F: FnMut(),
{
    type Item = S::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.project();
        let poll_next = this.stream.poll_next(cx);

        if let Poll::Ready(None) = &poll_next {
            (this.f)();
        }

        poll_next
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.stream.size_hint()
    }
}

#[cfg(test)]
mod test {
    use futures::{executor::block_on, stream, StreamExt};

    use crate::RxExt;

    #[test]
    fn smoke() {
        block_on(async {
            let mut is_done = false;
            let all_events = stream::iter(0..=3)
                .inspect_done(|| is_done = true)
                .collect::<Vec<_>>()
                .await;

            assert_eq!(all_events, [0, 1, 2, 3]);
            assert!(is_done);
        });
    }
}