futures_rx/stream_ext/
share.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
82
83
84
85
86
87
use core::pin::Pin;
use core::task::Context;
use core::task::Poll;
use futures::stream::Fuse;
use futures::stream::FusedStream;
use futures::Stream;
use futures::StreamExt;
use pin_project_lite::pin_project;
use std::cell::RefCell;
use std::rc::Rc;

use crate::subject::shareable_subject::ShareableSubject;
use crate::subject::Subject;
use crate::Event;
use crate::Observable;

pin_project! {
    /// Stream for the [`share`](RxStreamExt::share) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct Shared<S: Stream, Sub: Subject<Item = S::Item>> {
        inner: Rc<RefCell<ShareableSubject<S, Sub>>>,
        #[pin]
        stream: Fuse<Observable<S::Item>>,
    }
}

impl<S: Stream, Sub: Subject<Item = S::Item>> Shared<S, Sub> {
    pub(crate) fn new(stream: S, subject: Sub) -> Self {
        let mut subject = ShareableSubject::new(stream, subject);
        let stream = subject.subscribe().fuse();

        Self {
            inner: Rc::new(RefCell::new(subject)),
            stream,
        }
    }
}

impl<S: Stream, Sub: Subject<Item = S::Item>> Clone for Shared<S, Sub> {
    fn clone(&self) -> Self {
        let stream = self.inner.borrow_mut().subscribe().fuse();

        Self {
            inner: Rc::clone(&self.inner),
            stream,
        }
    }
}

impl<S: Stream, Sub: Subject<Item = S::Item>> Stream for Shared<S, Sub> {
    type Item = Event<S::Item>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.inner.borrow_mut().poll_next(cx);
        self.stream.poll_next_unpin(cx)
    }

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

impl<S: Stream, Sub: Subject<Item = S::Item>> FusedStream for Shared<S, Sub> {
    fn is_terminated(&self) -> bool {
        self.stream.is_terminated()
    }
}

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

    use crate::RxExt;

    #[test]
    fn smoke() {
        block_on(async {
            let stream = stream::iter(1usize..=3usize);
            let s1 = stream.share();
            let s2 = s1.clone();
            let (a, b) = join(s1.collect::<Vec<_>>(), s2.collect::<Vec<_>>()).await;

            assert_eq!(a, [1.into(), 2.into(), 3.into()]);
            assert_eq!(b, [1.into(), 2.into(), 3.into()]);
        });
    }
}