fix(event loop, client): better async

This commit is contained in:
Nova
2023-01-14 12:38:05 -05:00
parent 198b342dec
commit 27d75d3098
7 changed files with 171 additions and 133 deletions

View File

@@ -42,3 +42,35 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
self.0.lock().clear();
}
}
pub struct OwnedRegistry<T: Send + Sync + ?Sized>(Lazy<Mutex<FxHashMap<usize, Arc<T>>>>);
impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {
pub const fn new() -> Self {
OwnedRegistry(Lazy::new(|| Mutex::new(FxHashMap::default())))
}
pub fn add(&self, t: T) -> Arc<T>
where
T: Sized,
{
let t_arc = Arc::new(t);
self.add_raw(t_arc.clone());
t_arc
}
pub fn add_raw(&self, t: Arc<T>) {
self.0
.lock()
.insert(Arc::as_ptr(&t) as *const () as usize, t);
}
pub fn get_vec(&self) -> Vec<Arc<T>> {
self.0.lock().values().cloned().collect::<Vec<_>>()
}
pub fn remove(&self, t: &T) {
self.0
.lock()
.remove(&(ptr::addr_of!(*t) as *const () as usize));
}
pub fn clear(&self) {
self.0.lock().clear();
}
}