pub struct CodexSet { /* private fields */ }Expand description
Type-indexed set storage - one value per type.
CodexSet stores exactly one value per type T, similar to how a HashSet
stores unique values, but indexed by TypeId instead of value.
Useful for global configuration, shared state, or singleton resources.
§Cloning
CodexSet is cheaply cloneable via Arc. All clones share the same storage:
use magi_codex::CodexSet;
let set = CodexSet::new();
set.insert(42u32);
let set2 = set.clone();
assert_eq!(set2.get_cloned::<u32>(), Some(42));Implementations§
Source§impl CodexSet
impl CodexSet
Sourcepub fn insert<T>(&self, value: T)
pub fn insert<T>(&self, value: T)
Insert or replace a value.
§Example
use magi_codex::CodexSet;
struct Config { debug: bool }
let set = CodexSet::new();
set.insert(Config { debug: true });Sourcepub fn with<T, F, R>(&self, f: F) -> Option<R>
pub fn with<T, F, R>(&self, f: F) -> Option<R>
Access a value immutably via a closure.
The closure runs while holding the lock on the value.
§Example
use magi_codex::CodexSet;
struct Config { debug: bool }
let set = CodexSet::new();
set.insert(Config { debug: true });
let debug = set.with::<Config, _, _>(|config| config.debug).unwrap();
assert_eq!(debug, true);Sourcepub fn with_mut<T, F, R>(&self, f: F) -> Option<R>
pub fn with_mut<T, F, R>(&self, f: F) -> Option<R>
Access a value mutably via a closure.
The closure runs while holding the lock on the value.
§Example
use magi_codex::CodexSet;
struct Config { debug: bool }
let set = CodexSet::new();
set.insert(Config { debug: false });
set.with_mut::<Config, _, _>(|config| {
config.debug = true;
});
let debug = set.with::<Config, _, _>(|config| config.debug).unwrap();
assert_eq!(debug, true);Sourcepub fn get_cloned<T>(&self) -> Option<T>
pub fn get_cloned<T>(&self) -> Option<T>
Clone and return a value.
§Example
use magi_codex::CodexSet;
let set = CodexSet::new();
set.insert(42u32);
assert_eq!(set.get_cloned::<u32>(), Some(42));Trait Implementations§
Auto Trait Implementations§
impl Freeze for CodexSet
impl !RefUnwindSafe for CodexSet
impl Send for CodexSet
impl Sync for CodexSet
impl Unpin for CodexSet
impl !UnwindSafe for CodexSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more