/// 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>>,
}
/// 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(&mutself, value: Weak<T>) { ifself.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() { ifself.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());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.