pub struct VarMap { /* private fields */ }Expand description
Heterogeneous map keyed by Key.
Optimized for compile-time key names via var!. Supports at most 65 536 distinct keys.
See the crate-level documentation for the intended write-once-read-many usage model.
VarMap::new is const, so empty maps can be constructed in const / static contexts.
VarMap is Sync. Use Self::as_readonly for concurrent reads, or
Self::into_shared when threads must also write.
Implementations§
Source§impl VarMap
impl VarMap
Sourcepub const fn new() -> VarMap
pub const fn new() -> VarMap
Creates an empty map.
This constructor is const, so it can be used to initialize maps in const or static
contexts (for example behind a Mutex or other sync wrapper).
use varmap::VarMap;
const EMPTY: VarMap = VarMap::new();Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all keys and resets the arena offset.
Clears the hash table and value list; retained heap capacity may be reused on later inserts.
Sourcepub fn set<T>(&mut self, key: Key, value: T)where
T: VarMapValue,
pub fn set<T>(&mut self, key: Key, value: T)where
T: VarMapValue,
Inserts or overwrites key with value.
Overwriting a key does not reclaim arena memory from the previous value. See the crate-level documentation for details.
Sourcepub fn update<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> boolwhere
T: VarMapValue,
pub fn update<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> boolwhere
T: VarMapValue,
Updates the numeric value at key in place when supported for T.
Returns false if key is missing, the stored type is not T, or T does not support
in-place updates (see VarMapValue::update).
Sourcepub fn update_or_default<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> boolwhere
T: VarMapValue + Default,
pub fn update_or_default<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> boolwhere
T: VarMapValue + Default,
Updates the value at key in place, or inserts Default::default if key is missing.
T must implement Default.
- If
keyis missing,T::default()is inserted and this returnstrue.fis not called. - If
keyis present, this is the same asSelf::update:fruns when the stored type isTandTsupports in-place updates (seeVarMapValue::update).
Returns false if key is present but the stored type is not T, or T does not support
in-place updates.
use varmap::{var, Key, VarMap};
let mut map = VarMap::new();
assert!(map.update_or_default::<u32>(var!("count"), |_| {}));
assert_eq!(map.get_u32(var!("count")), Some(0));
assert!(map.update_or_default::<u32>(var!("count"), |n| *n += 1));
assert_eq!(map.get_u32(var!("count")), Some(1));Sourcepub fn get<'a, V>(&'a self, key: Key) -> Option<<V as VarMapValue>::Decoded<'a>>where
V: VarMapValue,
pub fn get<'a, V>(&'a self, key: Key) -> Option<<V as VarMapValue>::Decoded<'a>>where
V: VarMapValue,
Returns the value for key decoded as V.
Returns None if key is missing or the stored type does not match V.
Sourcepub fn get_ipv4(&self, key: Key) -> Option<Ipv4Addr>
pub fn get_ipv4(&self, key: Key) -> Option<Ipv4Addr>
Returns the value as Ipv4Addr. See Self::get.
Sourcepub fn get_ipv6(&self, key: Key) -> Option<Ipv6Addr>
pub fn get_ipv6(&self, key: Key) -> Option<Ipv6Addr>
Returns the value as Ipv6Addr. See Self::get.
Sourcepub fn get_duration(&self, key: Key) -> Option<Duration>
pub fn get_duration(&self, key: Key) -> Option<Duration>
Returns the value as Duration. See Self::get.
Sourcepub fn allocated_size(&self) -> usize
pub fn allocated_size(&self) -> usize
Returns the total allocated size of the map.
Sourcepub fn as_readonly(&self) -> Readonly<'_, VarMap>
pub fn as_readonly(&self) -> Readonly<'_, VarMap>
Returns a Sync read-only view of this map.
The view is Copy, Send, and Sync. Pass copies to other threads for concurrent
reads (getters and contains only). The borrow checker prevents
mutation of self while any view is still alive.
The view is not 'static; use std::thread::scope when spawning. See Readonly.
Consumes this map and returns a Shared handle for concurrent reads and writes.
Clones of the handle all refer to the same map (Arc<RwLock<_>>). Call
Shared::read or Shared::write from any thread. Unlike Self::as_readonly,
the handle owns the map and is 'static.
See Shared.