fix(registry): remove won't return result

This commit is contained in:
Nova
2022-06-20 08:07:51 -04:00
parent 9c6e4e49ba
commit 0c9dafe158
2 changed files with 8 additions and 9 deletions

View File

@@ -8,9 +8,12 @@ pub struct Registry<T>(RwLock<Slab<Weak<T>>>);
impl<T> Registry<T> {
pub fn add(&self, t: T) -> Arc<T> {
let t_arc = Arc::new(t);
self.0.write().insert(Arc::downgrade(&t_arc));
self.add_raw(&t_arc);
t_arc
}
pub fn add_raw(&self, t: &Arc<T>) {
self.0.write().insert(Arc::downgrade(t));
}
pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
self.0
.read()
@@ -18,21 +21,17 @@ impl<T> Registry<T> {
.filter_map(|(_, item)| item.upgrade())
.collect()
}
pub fn remove(&self, t: &T) -> Result<()> {
pub fn remove(&self, t: &T) {
let mut del_idx: Option<usize> = None;
for item in self.0.read().iter() {
let (idx, item) = item;
if let Some(item) = item.upgrade() {
if std::ptr::eq(item.as_ref(), t) {
del_idx = Some(idx);
self.0.write().remove(idx);
break;
}
}
}
del_idx
.map(|idx| self.0.write().remove(idx))
.ok_or_else(|| anyhow!("Node not found to remove"))?;
Ok(())
}
}

View File

@@ -151,7 +151,7 @@ impl PulseSender {
}
impl Drop for PulseSender {
fn drop(&mut self) {
let _ = PULSE_SENDER_REGISTRY.remove(self);
PULSE_SENDER_REGISTRY.remove(self);
}
}
@@ -219,7 +219,7 @@ impl<'a> PulseReceiver {
impl Drop for PulseReceiver {
fn drop(&mut self) {
let _ = PULSE_RECEIVER_REGISTRY.remove(self);
PULSE_RECEIVER_REGISTRY.remove(self);
}
}