Skip to main content

VarMap

Struct VarMap 

Source
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

Source

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();
Source

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.

Source

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.

Source

pub fn update<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> bool
where 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).

Source

pub fn update_or_default<T>(&mut self, key: Key, f: impl FnOnce(&mut T)) -> bool
where T: VarMapValue + Default,

Updates the value at key in place, or inserts Default::default if key is missing.

T must implement Default.

  • If key is missing, T::default() is inserted and this returns true. f is not called.
  • If key is present, this is the same as Self::update: f runs when the stored type is T and T supports in-place updates (see VarMapValue::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));
Source

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.

Source

pub fn get_bool(&self, key: Key) -> Option<bool>

Returns the value as bool. See Self::get.

Source

pub fn get_u8(&self, key: Key) -> Option<u8>

Returns the value as u8. See Self::get.

Source

pub fn get_u16(&self, key: Key) -> Option<u16>

Returns the value as u16. See Self::get.

Source

pub fn get_u32(&self, key: Key) -> Option<u32>

Returns the value as u32. See Self::get.

Source

pub fn get_u64(&self, key: Key) -> Option<u64>

Returns the value as u64. See Self::get.

Source

pub fn get_i8(&self, key: Key) -> Option<i8>

Returns the value as i8. See Self::get.

Source

pub fn get_i16(&self, key: Key) -> Option<i16>

Returns the value as i16. See Self::get.

Source

pub fn get_i32(&self, key: Key) -> Option<i32>

Returns the value as i32. See Self::get.

Source

pub fn get_i64(&self, key: Key) -> Option<i64>

Returns the value as i64. See Self::get.

Source

pub fn get_f32(&self, key: Key) -> Option<f32>

Returns the value as f32. See Self::get.

Source

pub fn get_f64(&self, key: Key) -> Option<f64>

Returns the value as f64. See Self::get.

Source

pub fn get_str(&self, key: Key) -> Option<&str>

Returns the value as &str. See Self::get.

Source

pub fn get_bytes(&self, key: Key) -> Option<&[u8]>

Returns the value as &[u8]. See Self::get.

Source

pub fn get_char(&self, key: Key) -> Option<char>

Returns the value as char. See Self::get.

Source

pub fn get_ip(&self, key: Key) -> Option<IpAddr>

Returns the value as IpAddr. See Self::get.

Source

pub fn get_ipv4(&self, key: Key) -> Option<Ipv4Addr>

Returns the value as Ipv4Addr. See Self::get.

Source

pub fn get_ipv6(&self, key: Key) -> Option<Ipv6Addr>

Returns the value as Ipv6Addr. See Self::get.

Source

pub fn get_duration(&self, key: Key) -> Option<Duration>

Returns the value as Duration. See Self::get.

Source

pub fn contains(&self, key: Key) -> bool

Returns true if key has a value (any type).

Source

pub fn allocated_size(&self) -> usize

Returns the total allocated size of the map.

Source

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.

Source

pub fn into_shared(self) -> Shared<VarMap>

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.

Trait Implementations§

Source§

impl Default for VarMap

Source§

fn default() -> VarMap

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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.