1use {
2 crate::sync::{Iter, SyncWeakList, WeakListData},
3 alloc::sync::Arc,
4 core::fmt::{Debug, Formatter},
5 parking_lot::Mutex,
6};
7
8impl<T> SyncWeakList<T>
9where
10 T: ?Sized,
11{
12 pub fn clear(&self) {
29 let data = &mut *self.data.lock();
30 data.members.clear();
31 }
32
33 pub fn iter(&self) -> Iter<'_, T> {
40 let data = &mut *self.data.lock();
41 if data.active_iterators == 0 {
42 data.members.compact();
43 }
44 data.active_iterators += 1;
45 Iter {
46 iter: 0..data.members.index_len(),
47 data: &self.data,
48 }
49 }
50}
51
52impl<T> Default for SyncWeakList<T>
53where
54 T: ?Sized,
55{
56 fn default() -> Self {
57 Self {
58 data: Arc::new(Mutex::new(WeakListData {
59 next_id: 0,
60 active_iterators: 0,
61 members: Default::default(),
62 })),
63 }
64 }
65}
66
67impl<'a, T> IntoIterator for &'a SyncWeakList<T>
68where
69 T: ?Sized,
70{
71 type Item = Arc<T>;
72 type IntoIter = Iter<'a, T>;
73
74 fn into_iter(self) -> Self::IntoIter {
75 self.iter()
76 }
77}
78
79impl<T> Debug for SyncWeakList<T>
80where
81 T: ?Sized,
82{
83 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
84 f.debug_struct("SyncWeakList")
85 .field("id", &Arc::as_ptr(&self.data))
86 .finish_non_exhaustive()
87 }
88}