feat: basic registry

This commit is contained in:
Nova
2022-06-11 22:43:50 -04:00
parent e140d82b1a
commit 30a03f638f
2 changed files with 41 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
pub mod client;
pub mod eventloop;
pub mod registry;
pub mod scenegraph;

40
src/core/registry.rs Normal file
View File

@@ -0,0 +1,40 @@
use anyhow::{anyhow, Result};
use slab::{Iter, Slab};
use std::sync::RwLock;
struct Registry<T>(RwLock<Slab<T>>);
impl<T> Registry<T> {
pub fn add(&self, t: T) -> Result<usize> {
Ok(self
.0
.write()
.ok()
.ok_or_else(|| anyhow!("Registry has been poisoned"))?
.insert(t))
}
pub fn iterate<F: FnOnce(Iter<'_, T>)>(&self, index: usize, closure: F) -> Result<()> {
closure(
self.0
.read()
.ok()
.ok_or_else(|| anyhow!("Registry has been poisoned"))?
.iter(),
);
Ok(())
}
pub fn remove(&self, index: usize) -> Result<T> {
Ok(self
.0
.write()
.ok()
.ok_or_else(|| anyhow!("Registry has been poisoned"))?
.remove(index))
}
}
impl<T> Default for Registry<T> {
fn default() -> Self {
Registry::<T>(RwLock::new(Slab::new()))
}
}