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
//! Module containing the [`WeakVec`] API.

use std::sync::Weak;

/// An optimized container for `Weak` references of `T` that minimizes reallocations by
/// dropping older elements that no longer have strong references to them.
#[derive(Debug)]
pub(crate) struct WeakVec<T> {
    inner: Vec<Weak<T>>,
}

impl<T> Default for WeakVec<T> {
    fn default() -> Self {
        Self {
            inner: Default::default(),
        }
    }
}

impl<T> WeakVec<T> {
    pub(crate) fn new() -> Self {
        Self { inner: Vec::new() }
    }

    /// Pushes a new element to this collection.
    ///
    /// If the inner Vec needs to be reallocated, we will first drop older elements that
    /// no longer have strong references to them.
    pub(crate) fn push(&mut self, value: Weak<T>) {
        if self.inner.len() == self.inner.capacity() {
            // Iterating backwards has the advantage that we don't do more work than we have to.
            for i in (0..self.inner.len()).rev() {
                if self.inner[i].strong_count() == 0 {
                    self.inner.swap_remove(i);
                }
            }

            // Make sure our capacity is twice the number of live elements.
            // Leaving some spare capacity ensures that we won't re-scan immediately.
            self.inner.reserve_exact(self.inner.len());
        }

        self.inner.push(value);
    }
}

pub(crate) struct WeakVecIter<T> {
    inner: std::vec::IntoIter<Weak<T>>,
}

impl<T> Iterator for WeakVecIter<T> {
    type Item = Weak<T>;
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<T> IntoIterator for WeakVec<T> {
    type Item = Weak<T>;
    type IntoIter = WeakVecIter<T>;
    fn into_iter(self) -> Self::IntoIter {
        WeakVecIter {
            inner: self.inner.into_iter(),
        }
    }
}