CodexMap

Struct CodexMap 

Source
pub struct CodexMap { /* private fields */ }
Expand description

Type-indexed map storage - multiple values per type, indexed by keys.

CodexMap stores multiple values of type V indexed by keys of type K, similar to how a HashMap stores key-value pairs, but with type-level isolation where different (K, V) type pairs are stored separately.

Useful for sessions, caches, per-user data, etc.

§Cloning

CodexMap is cheaply cloneable via Arc. All clones share the same storage:

use magi_codex::CodexMap;

let map = CodexMap::new();
map.insert("user_1", 100u32);

let map2 = map.clone();
assert_eq!(map2.get_cloned::<&str, u32>(&"user_1"), Some(100u32));

Implementations§

Source§

impl CodexMap

Source

pub fn new() -> Self

Create a new empty map.

Source

pub fn insert<K, V>(&self, key: K, value: V)
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static,

Insert or replace a key-value pair.

§Example
use magi_codex::CodexMap;

#[derive(Clone)]
struct Session { user_id: String }

let map = CodexMap::new();
map.insert("user_123", Session { user_id: "123".into() });
Source

pub fn with<K, V, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static, F: FnOnce(&V) -> R,

Access a value immutably via a closure.

The closure runs while holding the lock on the value.

Source

pub fn with_mut<K, V, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static, F: FnOnce(&mut V) -> R,

Access a value mutably via a closure.

The closure runs while holding the lock on the value.

Source

pub fn get_cloned<K, V>(&self, key: &K) -> Option<V>
where K: Hash + Eq + Send + Sync + 'static, V: Clone + Send + Sync + 'static,

Clone and return a value (requires Clone).

Source

pub fn remove<K, V>(&self, key: &K) -> Option<(K, V)>
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static,

Remove and return a key-value pair.

Source

pub fn contains<K, V>(&self, key: &K) -> bool
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static,

Returns true if a key exists.

Source

pub fn for_each<K, V, F>(&self, f: F)
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static, F: FnMut(&K, &V),

Iterate over all key-value pairs via a closure.

Source

pub fn len<K, V>(&self) -> usize
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static,

Returns the number of entries for a given type pair.

Source

pub fn is_empty<K, V>(&self) -> bool
where K: Hash + Eq + Send + Sync + 'static, V: Send + Sync + 'static,

Returns true if there are no values of the given type.

Source

pub fn contains2<K, V1, V2>(&self, key: &K) -> bool
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static,

Check if a key has values of two types.

Source

pub fn contains3<K, V1, V2, V3>(&self, key: &K) -> bool
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static,

Check if a key has values of three types.

Source

pub fn contains4<K, V1, V2, V3, V4>(&self, key: &K) -> bool
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static, V4: Send + Sync + 'static,

Check if a key has values of four types.

Source

pub fn with2<K, V1, V2, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, F: FnOnce(&V1, &V2) -> R,

Access two value types for the same key via a closure.

Source

pub fn with3<K, V1, V2, V3, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static, F: FnOnce(&V1, &V2, &V3) -> R,

Access three value types for the same key via a closure.

Source

pub fn with4<K, V1, V2, V3, V4, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static, V4: Send + Sync + 'static, F: FnOnce(&V1, &V2, &V3, &V4) -> R,

Access four value types for the same key via a closure.

Source

pub fn with2_mut<K, V1, V2, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, F: FnOnce(&mut V1, &mut V2) -> R,

Mutably access two value types for the same key via a closure.

Source

pub fn with3_mut<K, V1, V2, V3, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static, F: FnOnce(&mut V1, &mut V2, &mut V3) -> R,

Mutably access three value types for the same key via a closure.

Source

pub fn with4_mut<K, V1, V2, V3, V4, F, R>(&self, key: &K, f: F) -> Option<R>
where K: Hash + Eq + Send + Sync + 'static, V1: Send + Sync + 'static, V2: Send + Sync + 'static, V3: Send + Sync + 'static, V4: Send + Sync + 'static, F: FnOnce(&mut V1, &mut V2, &mut V3, &mut V4) -> R,

Mutably access four value types for the same key via a closure.

Source

pub fn get2_cloned<K, V1, V2>(&self, key: &K) -> Option<(V1, V2)>
where K: Hash + Eq + Send + Sync + 'static, V1: Clone + Send + Sync + 'static, V2: Clone + Send + Sync + 'static,

Clone and return two value types for the same key.

Source

pub fn get3_cloned<K, V1, V2, V3>(&self, key: &K) -> Option<(V1, V2, V3)>
where K: Hash + Eq + Send + Sync + 'static, V1: Clone + Send + Sync + 'static, V2: Clone + Send + Sync + 'static, V3: Clone + Send + Sync + 'static,

Clone and return three value types for the same key.

Source

pub fn get4_cloned<K, V1, V2, V3, V4>( &self, key: &K, ) -> Option<(V1, V2, V3, V4)>
where K: Hash + Eq + Send + Sync + 'static, V1: Clone + Send + Sync + 'static, V2: Clone + Send + Sync + 'static, V3: Clone + Send + Sync + 'static, V4: Clone + Send + Sync + 'static,

Clone and return four value types for the same key.

Trait Implementations§

Source§

impl Clone for CodexMap

Source§

fn clone(&self) -> CodexMap

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for CodexMap

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.