Skip to content

Commit

Permalink
cached id namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Aug 9, 2023
1 parent e0b77dc commit e0891f0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ impl<T: Registration> Builder<T> {
}

/// Register a new value and its id into this builder and return its raw id.
pub fn register(&mut self, value: T, id: Id) -> anyhow::Result<usize> {
pub fn register(&mut self, value: T, id: Id) -> Option<usize> {
if self.entries.iter().any(|e| e.1 == id) {
Err(anyhow::anyhow!("Registration with id {id} already exist!"))
None
} else {
self.entries.push((value, id));
Ok(self.entries.len() - 1)
Some(self.entries.len() - 1)
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use std::{hash::Hash, ops::Deref};
pub mod collections;
pub mod math;

static IDENTIFIER_NAMESPACE_CACHES: crate::collections::Caches<String> =
crate::collections::Caches::new();

/// An identifier used to identify things.
///
/// This is also known as "resource location", "namespaced ID",
Expand All @@ -11,15 +14,15 @@ pub mod math;
/// using a combination of namespace and path.
#[derive(PartialEq, Eq, Clone, Hash)]
pub struct Id {
namespace: String,
namespace: Ref<'static, String>,
path: String,
}

impl Id {
pub fn new(namespace: &str, path: &str) -> anyhow::Result<Self> {
if Self::is_namespace_valid(namespace) && Self::is_path_valid(path) {
Ok(Self {
namespace: namespace.to_string(),
namespace: Ref(IDENTIFIER_NAMESPACE_CACHES.get(namespace.to_string())),
path: path.to_string(),
})
} else {
Expand Down Expand Up @@ -70,8 +73,8 @@ impl Id {
true
}

pub fn namespace(&self) -> &str {
&self.namespace
pub fn namespace(&self) -> &'static str {
self.namespace.0
}

pub fn path(&self) -> &str {
Expand Down

0 comments on commit e0891f0

Please sign in to comment.