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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]

use futures::stream::{FusedStream, Stream};
use pin_project::pin_project;
use std::{
    ops::DerefMut,
    pin::{pin, Pin},
    sync::{atomic::AtomicU64, Arc, Mutex},
    task::Poll,
};

mod weak;

pub use weak::*;

pub trait StreamBroadcastExt: FusedStream + Sized {
    fn broadcast(self, size: usize) -> StreamBroadcast<Self>;
}

impl<T: FusedStream + Sized> StreamBroadcastExt for T
where
    T::Item: Clone,
{
    fn broadcast(self, size: usize) -> StreamBroadcast<Self> {
        StreamBroadcast::new(self, size)
    }
}

#[pin_project]
pub struct StreamBroadcast<T: FusedStream> {
    pos: u64,
    id: u64,
    state: Arc<Mutex<Pin<Box<StreamBroadcastState<T>>>>>,
}

impl<T: FusedStream> std::fmt::Debug for StreamBroadcast<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let pending = self.state.lock().unwrap().global_pos - self.pos;
        f.debug_struct("WeakStreamBroadcast")
            .field("pending_messages", &pending)
            .field("strong_count", &Arc::strong_count(&self.state))
            .finish()
    }
}

impl<T: FusedStream> Clone for StreamBroadcast<T> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
            id: create_id(),
            pos: self.pos,
        }
    }
}

impl<T: FusedStream> StreamBroadcast<T>
where
    T::Item: Clone,
{
    pub fn new(outer: T, size: usize) -> Self {
        Self {
            state: Arc::new(Mutex::new(Box::pin(StreamBroadcastState::new(outer, size)))),
            id: create_id(),
            pos: 0,
        }
    }

    /// Creates a weak broadcast which terminates its stream, if all 'strong' [StreamBroadcast] went out of scope
    ///
    /// ```
    /// # #[tokio::main]
    /// # async fn main() {
    /// use futures::StreamExt;
    /// use stream_broadcast::StreamBroadcastExt;
    ///
    /// let stream = futures::stream::iter(0..).fuse().broadcast(5);
    /// let mut weak = std::pin::pin!(stream.weak());
    /// assert_eq!(Some((0, 0)), weak.next().await);
    /// drop(stream);
    /// assert_eq!(None, weak.next().await);
    /// # }
    /// ```
    pub fn downgrade(&self) -> WeakStreamBroadcast<T> {
        WeakStreamBroadcast::new(Arc::downgrade(&self.state), self.pos)
    }

    #[deprecated(since = "0.2.2", note = "please use `downgrade` instead")]
    pub fn weak(&self) -> WeakStreamBroadcast<T> {
        WeakStreamBroadcast::new(Arc::downgrade(&self.state), self.pos)
    }
}

impl<T: FusedStream> Stream for StreamBroadcast<T>
where
    T::Item: Clone,
{
    type Item = (u64, T::Item);

    fn poll_next(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        let this = self.project();
        let mut lock = this.state.lock().unwrap();
        broadast_next(lock.deref_mut().as_mut(), cx, this.pos, *this.id)
    }
}
fn create_id() -> u64 {
    static ID_COUNTER: AtomicU64 = AtomicU64::new(0);
    ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}
fn broadast_next<T: FusedStream>(
    pinned: Pin<&mut StreamBroadcastState<T>>,
    cx: &mut std::task::Context<'_>,
    pos: &mut u64,
    id: u64,
) -> Poll<Option<(u64, T::Item)>>
where
    T::Item: Clone,
{
    match pinned.poll(cx, *pos, id) {
        Poll::Ready(Some((new_pos, x))) => {
            debug_assert!(new_pos > *pos, "Must always grow {} > {}", new_pos, *pos);
            let offset = new_pos - *pos - 1;
            *pos = new_pos;
            Poll::Ready(Some((offset, x)))
        }
        Poll::Ready(None) => {
            *pos += 1;
            Poll::Ready(None)
        }
        Poll::Pending => Poll::Pending,
    }
}

impl<T: FusedStream> FusedStream for StreamBroadcast<T>
where
    T::Item: Clone,
{
    fn is_terminated(&self) -> bool {
        self.state.lock().unwrap().stream.is_terminated()
    }
}

#[pin_project]
struct StreamBroadcastState<T: FusedStream> {
    #[pin]
    stream: T,
    global_pos: u64,
    cache: Vec<T::Item>,
    wakable: Vec<(u64, std::task::Waker)>,
}

impl<T: FusedStream> StreamBroadcastState<T>
where
    T::Item: Clone,
{
    fn new(outer: T, size: usize) -> Self {
        Self {
            stream: outer,
            cache: Vec::with_capacity(size), // Could be improved with  Box<[MaybeUninit<T::Item>]>
            global_pos: Default::default(),
            wakable: Default::default(),
        }
    }
    fn poll(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        request_pos: u64,
        id: u64,
    ) -> Poll<Option<(u64, T::Item)>> {
        let this = self.project();
        if *this.global_pos > request_pos {
            let cap = this.cache.capacity();
            let return_pos = if *this.global_pos - request_pos > cap as u64 {
                *this.global_pos - cap as u64
            } else {
                request_pos
            };

            let result = this.cache[(return_pos % cap as u64) as usize].clone();
            return Poll::Ready(Some((return_pos + 1, result)));
        }

        match this.stream.poll_next(cx) {
            Poll::Ready(Some(x)) => {
                this.wakable.drain(..).for_each(|(k, w)| {
                    if k != id {
                        w.wake();
                    }
                });

                let cap = this.cache.capacity();
                if this.cache.len() < cap {
                    this.cache.push(x.clone());
                } else {
                    this.cache[(*this.global_pos % cap as u64) as usize] = x.clone();
                }
                *this.global_pos += 1;
                let result = (*this.global_pos, x);
                Poll::Ready(Some(result))
            }
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => {
                this.wakable.push((id, cx.waker().clone()));
                Poll::Pending
            }
        }
    }
}