use {
crate::unsync::{Iter, WeakList, WeakListData},
alloc::rc::Rc,
core::{
cell::UnsafeCell,
fmt::{Debug, Formatter},
},
};
impl<T> WeakList<T>
where
T: ?Sized,
{
pub fn clear(&self) {
let data = unsafe {
&mut *self.data.get()
};
data.members.clear();
}
pub fn iter(&self) -> Iter<'_, T> {
let data = unsafe {
&mut *self.data.get()
};
if data.active_iterators == 0 {
data.members.compact();
}
data.active_iterators += 1;
Iter {
iter: 0..data.members.index_len(),
data: &self.data,
}
}
}
impl<T> Default for WeakList<T>
where
T: ?Sized,
{
fn default() -> Self {
Self {
data: Rc::new(UnsafeCell::new(WeakListData {
next_id: 0,
active_iterators: 0,
members: Default::default(),
})),
}
}
}
impl<'a, T> IntoIterator for &'a WeakList<T>
where
T: ?Sized,
{
type Item = Rc<T>;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T> Debug for WeakList<T>
where
T: ?Sized,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("WeakList")
.field("id", &Rc::as_ptr(&self.data))
.finish_non_exhaustive()
}
}