From e0891f02a829dcde379182bf3d38b2f7168080a7 Mon Sep 17 00:00:00 2001 From: Yjn024 Date: Wed, 9 Aug 2023 16:14:56 +0800 Subject: [PATCH] cached id namespace --- src/registry/mod.rs | 6 +++--- src/util/mod.rs | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/registry/mod.rs b/src/registry/mod.rs index 233ebd3..a513b08 100644 --- a/src/registry/mod.rs +++ b/src/registry/mod.rs @@ -163,12 +163,12 @@ impl Builder { } /// 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 { + pub fn register(&mut self, value: T, id: Id) -> Option { 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) } } } diff --git a/src/util/mod.rs b/src/util/mod.rs index 688eced..6f099f6 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,6 +3,9 @@ use std::{hash::Hash, ops::Deref}; pub mod collections; pub mod math; +static IDENTIFIER_NAMESPACE_CACHES: crate::collections::Caches = + crate::collections::Caches::new(); + /// An identifier used to identify things. /// /// This is also known as "resource location", "namespaced ID", @@ -11,7 +14,7 @@ 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, } @@ -19,7 +22,7 @@ impl Id { pub fn new(namespace: &str, path: &str) -> anyhow::Result { 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 { @@ -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 {