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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use event_listener::{Event, EventListener};
use std::{
    fmt::{Debug, Formatter, Result},
    future::{Future, IntoFuture},
    pin::{pin, Pin},
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    task::{ready, Context, Poll},
};

pub struct CloneCounterInner {
    count: AtomicUsize,
    event: Event,
}

impl CloneCounterInner {
    fn new(start: usize) -> Self {
        Self {
            count: AtomicUsize::new(start),
            event: Event::new(),
        }
    }
}

impl Debug for CloneCounterInner {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.debug_struct("CloneCounterInner")
            .field("count", &self.count)
            .finish()
    }
}

/**
# an atomic counter that increments on clone & decrements on drop

One-indexed, because the first CloneCounter is included. If you don't
want the original to count, construct a [`CloneCounterObserver`]
instead and use [`CloneCounterObserver::counter`] to increment.

Awaiting a [`CloneCounter`] will be pending until it is the only remaining
counter and resolve to `()` when the count is 1.

*/

#[derive(Debug)]
pub struct CloneCounter(Arc<CloneCounterInner>);

impl Default for CloneCounter {
    fn default() -> Self {
        Self(Arc::new(CloneCounterInner::new(1)))
    }
}

impl CloneCounter {
    /// Constructs a new CloneCounter. Identical to CloneCounter::default()
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the current count. The first CloneCounter is one, so
    /// this can either be considered a one-indexed count of the
    /// total number of CloneCounters in memory
    pub fn current(&self) -> usize {
        self.0.current()
    }

    /// Manually decrement the count. This is useful when taking a
    /// clone of the counter that does not represent an increase in
    /// the underlying property or resource being counted. This is
    /// called automatically on drop and is usually unnecessary to
    /// call directly
    pub fn decrement(&self) {
        let previously = self.0.count.fetch_sub(1, Ordering::SeqCst);
        debug_assert!(previously > 0);
        self.0.wake();
        if previously > 0 {
            log::trace!("decrementing from {} -> {}", previously, previously - 1);
        } else {
            log::trace!("decrementing from 0");
        }
    }

    /// Manually increment the count. unless paired with a decrement,
    /// this will prevent the clone counter from ever reaching
    /// zero. This is called automatically on clone.
    pub fn increment(&self) {
        let previously = self.0.count.fetch_add(1, Ordering::SeqCst);
        log::trace!("incrementing from {} -> {}", previously, previously + 1);
    }

    /// Returns an observer that can be cloned any number of times
    /// without modifying the clone counter. See
    /// [`CloneCounterObserver`] for more.
    pub fn observer(&self) -> CloneCounterObserver {
        CloneCounterObserver(Arc::clone(&self.0))
    }
}

impl IntoFuture for CloneCounter {
    type Output = ();

    type IntoFuture = CloneCounterFuture;

    fn into_future(self) -> Self::IntoFuture {
        CloneCounterFuture {
            inner: Arc::clone(&self.0),
            listener: EventListener::new(),
        }
    }
}

impl Future for &CloneCounter {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut listener = pin!(EventListener::new());
        loop {
            if 1 == self.0.current() {
                return Poll::Ready(());
            }

            if listener.is_listening() {
                ready!(listener.as_mut().poll(cx));
            } else {
                listener.as_mut().listen(&self.0.event)
            }
        }
    }
}
impl Clone for CloneCounter {
    fn clone(&self) -> Self {
        self.increment();
        Self(self.0.clone())
    }
}

impl Drop for CloneCounter {
    fn drop(&mut self) {
        self.decrement();
    }
}

impl CloneCounterInner {
    fn current(&self) -> usize {
        self.count.load(Ordering::SeqCst)
    }

    fn wake(&self) {
        self.event.notify(usize::MAX);
    }
}

impl PartialEq<usize> for CloneCounter {
    fn eq(&self, other: &usize) -> bool {
        self.current() == *other
    }
}

/**
An observer that can be cloned without modifying the clone
counter, but can be used to inspect its state and awaited

Zero-indexed, but each [`CloneCounter`] retrieved with
[`CloneCounterObserver::counter`] increments the count by 1.

Awaiting a [`CloneCounterObserver`] will be pending until all
associated [`CloneCounter`]s have been dropped, and will resolve to
`()` when the count is 0.

*/

#[derive(Debug)]
pub struct CloneCounterObserver(Arc<CloneCounterInner>);

impl Clone for CloneCounterObserver {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl Default for CloneCounterObserver {
    fn default() -> Self {
        Self(Arc::new(CloneCounterInner::new(0)))
    }
}

impl PartialEq<usize> for CloneCounterObserver {
    fn eq(&self, other: &usize) -> bool {
        self.current() == *other
    }
}

impl CloneCounterObserver {
    /// returns a new observer with a zero count. use [`CloneCounterObserver::counter`] to
    pub fn new() -> Self {
        Self::default()
    }
    /// returns the current counter value
    pub fn current(&self) -> usize {
        self.0.current()
    }

    /// creates a new CloneCounter from this observer, incrementing the count
    pub fn counter(&self) -> CloneCounter {
        let counter = CloneCounter(Arc::clone(&self.0));
        counter.increment();
        counter
    }
}

impl IntoFuture for CloneCounterObserver {
    type Output = ();

    type IntoFuture = CloneCounterFuture;

    fn into_future(self) -> Self::IntoFuture {
        CloneCounterFuture {
            listener: EventListener::new(),
            inner: self.0,
        }
    }
}

impl From<CloneCounter> for CloneCounterObserver {
    fn from(value: CloneCounter) -> Self {
        // value will be decremented on drop of the original
        Self(Arc::clone(&value.0))
    }
}

impl From<CloneCounterObserver> for CloneCounter {
    fn from(value: CloneCounterObserver) -> Self {
        let counter = Self(value.0);
        counter.increment();
        counter
    }
}

pin_project_lite::pin_project! {
    /// A future that waits for the clone counter to decrement to zero
    #[derive(Debug)]
    pub struct CloneCounterFuture {
        inner: Arc<CloneCounterInner>,
        #[pin]
        listener: EventListener,
    }
}

impl Clone for CloneCounterFuture {
    fn clone(&self) -> Self {
        let listener = EventListener::new();
        Self {
            inner: Arc::clone(&self.inner),
            listener,
        }
    }
}

impl Future for CloneCounterFuture {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();
        loop {
            if 0 == this.inner.current() {
                return Poll::Ready(());
            };
            if this.listener.is_listening() {
                ready!(this.listener.as_mut().poll(cx));
            } else {
                this.listener.as_mut().listen(&this.inner.event);
            }
        }
    }
}

#[cfg(test)]
mod test {
    use crate::clone_counter::CloneCounterObserver;

    use super::CloneCounter;
    use futures_lite::future::poll_once;
    use std::future::{Future, IntoFuture};
    use test_harness::test;

    fn block_on<F, Fut>(test: F)
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = ()>,
    {
        trillium_testing::block_on(test());
    }

    #[test(harness = block_on)]
    async fn doctest_example() {
        let counter = CloneCounter::new();
        assert_eq!(counter.current(), 1);
        counter.await; // ready immediately

        let counter = CloneCounter::new();
        assert_eq!(counter.current(), 1);
        let clone = counter.clone();
        assert_eq!(counter.current(), 2);
        let clone2 = counter.clone();
        assert_eq!(counter.current(), 3);
        assert_eq!(poll_once(clone2.into_future()).await, None); // pending
        assert_eq!(counter.current(), 2);
        drop(clone);

        assert_eq!(counter.current(), 1);
        counter.await; // ready
    }

    #[test(harness = block_on)]
    async fn observer_into_and_from() {
        let counter = CloneCounter::new();
        assert_eq!(counter, 1);
        assert_eq!(counter.clone(), 2);
        assert_eq!(counter, 1);
        let observer = CloneCounterObserver::from(counter);
        assert_eq!(poll_once(observer.clone().into_future()).await, Some(()));
        assert_eq!(observer, 0);
        let counter = CloneCounter::from(observer);
        assert_eq!(counter, 1);
        assert_eq!(poll_once(counter.into_future()).await, Some(()));
    }

    #[test(harness = block_on)]
    async fn observer_test() {
        let counter = CloneCounter::new();
        assert_eq!(counter.current(), 1);
        counter.await; // ready immediately

        let counter = CloneCounter::new();
        let mut clones = Vec::new();
        let observer = counter.observer();
        assert_eq!(observer.current(), 1);
        for i in 1..=10 {
            clones.push(counter.clone());
            assert_eq!(counter.current(), 1 + i);
            assert_eq!(observer.current(), 1 + i);
        }

        let _observers = std::iter::repeat_with(|| observer.clone())
            .take(10)
            .collect::<Vec<_>>();
        assert_eq!(observer.current(), 11); // unchanged,

        let _observers = std::iter::repeat_with(|| counter.observer())
            .take(10)
            .collect::<Vec<_>>();
        assert_eq!(observer.current(), 11); // unchanged,

        for (i, clone) in clones.drain(..).enumerate() {
            assert_eq!(clone.current(), 11 - i);
            assert_eq!(observer.current(), 11 - i);
            assert_eq!(poll_once(&clone).await, None); // pending
            assert_eq!(poll_once(observer.clone().into_future()).await, None); // pending
            drop(clone);
            assert_eq!(counter.current(), 10 - i);
            assert_eq!(observer.current(), 10 - i);
        }

        assert_eq!(counter.current(), 1);
        assert_eq!(poll_once(counter.into_future()).await, Some(()));
        assert_eq!(observer.current(), 0);
        assert_eq!(poll_once(observer.into_future()).await, Some(()));
    }
}