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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Utilities (we might need to separate this out to another crate).

use std::{
    hash::Hash,
    ops::Deref,
    rc::{Rc, Weak},
};

pub mod geometry;

/// Wrapper of `futures_util::stream::AbortHandle` that automatically abort the
/// stream when dropped.
#[derive(Debug)]
pub(crate) struct AutoAbort(futures_util::stream::AbortHandle);

impl From<futures_util::stream::AbortHandle> for AutoAbort {
    fn from(value: futures_util::stream::AbortHandle) -> Self {
        Self(value)
    }
}

impl Drop for AutoAbort {
    fn drop(&mut self) {
        self.0.abort();
    }
}

/// A wrapper of `Rc` that implements `Hash` and `Eq` by comparing
/// raw pointer addresses.
#[derive(Debug)]
pub(crate) struct RcPtr<T>(Rc<T>);

#[allow(dead_code)] // anticipating future use
impl<T> RcPtr<T> {
    /// Create a new `RcPtr` from a value.
    pub(crate) fn new(value: T) -> Self {
        Self(Rc::new(value))
    }

    /// Return the inner `Rc`.
    pub(crate) fn into_inner(self) -> Rc<T> {
        self.0
    }
}

impl<T> Hash for RcPtr<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::ptr::hash(Rc::as_ptr(&self.0), state)
    }
}

impl<T> PartialEq for RcPtr<T> {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

impl<T> Eq for RcPtr<T> {}

impl<T> From<Rc<T>> for RcPtr<T> {
    fn from(value: Rc<T>) -> Self {
        Self(value)
    }
}
impl<T> Deref for RcPtr<T> {
    type Target = Rc<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<T> AsRef<Rc<T>> for RcPtr<T> {
    fn as_ref(&self) -> &Rc<T> {
        &self.0
    }
}
impl<T> AsMut<Rc<T>> for RcPtr<T> {
    fn as_mut(&mut self) -> &mut Rc<T> {
        &mut self.0
    }
}

/// A wrapper of `Weak` that implements `Hash` and `Eq` by comparing
/// raw pointer addresses.
pub struct WeakPtr<T>(Weak<T>);

impl<T> std::fmt::Debug for WeakPtr<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WeakPtr")
            .field("ptr", &self.0.as_ptr())
            .finish()
    }
}

impl<T> Clone for WeakPtr<T> {
    fn clone(&self) -> Self {
        Self(Weak::clone(&self.0))
    }
}

impl<T> WeakPtr<T> {
    /// Create a `WeakPtr` by downgrading a `Rc`.
    pub fn from_rc(t: &Rc<T>) -> Self {
        Self(Rc::downgrade(t))
    }

    /// Create an empty `WeakPtr`.
    pub fn new() -> Self {
        Self(Weak::new())
    }

    /// Attempt to upgrade the `WeakPtr` to an `Rc`.
    pub fn upgrade(&self) -> Option<Rc<T>> {
        self.0.upgrade()
    }

    /// Return the inner `Weak`.
    pub fn into_inner(self) -> Weak<T> {
        self.0
    }

    /// Get a reference to the inner `Weak`.
    pub const fn as_weak(&self) -> &Weak<T> {
        &self.0
    }
}

impl<T> PartialEq for WeakPtr<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.ptr_eq(&other.0)
    }
}

impl<T> Eq for WeakPtr<T> {}

impl<T> Hash for WeakPtr<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::ptr::hash(self.0.as_ptr(), state)
    }
}

impl<T> From<Weak<T>> for WeakPtr<T> {
    fn from(value: Weak<T>) -> Self {
        Self(value)
    }
}

pub(crate) mod stream {
    use std::{
        cell::RefCell,
        pin::Pin,
        rc::Rc,
        task::{Context, Poll, Waker},
    };

    use derivative::Derivative;
    use futures_core::{FusedStream, Stream};
    #[derive(Derivative, Debug)]
    #[derivative(Default(bound = ""))]
    struct ReplaceInner<S> {
        stream: Option<S>,
        waker:  Option<Waker>,
    }
    /// A stream whose inner is replaceable.
    ///
    /// The inner can be replaced using the corresponding [`Replace`] handle.
    /// The new stream will be polled the next time the [`Replaceable`]
    /// stream is polled. If the inner is `None`, then `Poll::Pending` is
    /// returned if the corresponding `Replace` handle hasn't been dropped,
    /// otherwise `Poll::Ready(None)` will be returned, since it is no
    /// longer possible to put anything that's not `None` into the inner.
    #[derive(Debug)]
    pub struct Replaceable<S> {
        inner: Rc<RefCell<ReplaceInner<S>>>,
    }

    // TODO: wake up task when inner is replaced
    #[derive(Debug)]
    pub struct Replace<S> {
        inner: Rc<RefCell<ReplaceInner<S>>>,
    }

    impl<S: Stream + Unpin> Stream for Replaceable<S> {
        type Item = S::Item;

        fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
            let strong = Rc::strong_count(&self.inner);
            let mut inner = self.inner.borrow_mut();
            inner.waker.take();

            let p = inner
                .stream
                .as_mut()
                .map(|s| Pin::new(s).poll_next(cx))
                .unwrap_or(Poll::Ready(None));

            if matches!(p, Poll::Ready(Some(_)) | Poll::Pending) {
                // If p is Pending, normally the inner stream would be responsible for waking
                // the task up, but if the inner stream is replaced before the
                // task is woken up, we still need to notify the task.
                if p.is_pending() && strong > 1 {
                    inner.waker = Some(cx.waker().clone());
                }
                return p
            }

            // p == Poll::Ready(None), replace the stream with None
            inner.stream = None;
            if strong == 1 {
                // Replace handle has been dropped
                Poll::Ready(None)
            } else {
                inner.waker = Some(cx.waker().clone());
                Poll::Pending
            }
        }
    }

    impl<S: Stream + Unpin> FusedStream for Replaceable<S> {
        fn is_terminated(&self) -> bool {
            Rc::strong_count(&self.inner) == 1 && self.inner.borrow().stream.is_none()
        }
    }

    impl<S> Replace<S> {
        /// Replace the stream in the corresponding [`Replaceable`] with the
        /// given stream. Returns the old stream.
        ///
        /// Note the new stream will not be immediately polled, so if there is
        /// currently a task waiting on the corresponding
        /// [`Replaceable`], it will not be woken up. You must arrange for it
        /// to be polled.
        pub fn replace(&self, stream: Option<S>) -> Option<S> {
            if stream.is_some() {
                if let Some(waker) = self.inner.borrow_mut().waker.take() {
                    // Wake up the task so the new stream will be polled.
                    waker.wake();
                }
            }
            std::mem::replace(&mut self.inner.borrow_mut().stream, stream)
        }
    }

    impl<S> Drop for Replace<S> {
        fn drop(&mut self) {
            if let Some(waker) = self.inner.borrow_mut().waker.take() {
                // If we drop the Replace, the corresponding Replaceable could return
                // Poll::Ready(None) if polled again, so we need to wake up the task to poll it
                // again.
                waker.wake();
            }
        }
    }

    /// Create a pair of [`Replaceable`] and [`Replace`]. The `Replace` can be
    /// used to replace the stream in the `Replaceable`.
    pub fn replaceable<S>() -> (Replaceable<S>, Replace<S>) {
        let inner = Rc::new(RefCell::new(ReplaceInner::default()));
        (
            Replaceable {
                inner: inner.clone(),
            },
            Replace { inner },
        )
    }
}