feat: basic registry
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
pub mod client;
|
||||
pub mod eventloop;
|
||||
pub mod registry;
|
||||
pub mod scenegraph;
|
||||
|
||||
40
src/core/registry.rs
Normal file
40
src/core/registry.rs
Normal 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()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user