pub struct Variables { /* private fields */ }Expand description
Ordered map of variable names to values, shared on clone.
Iteration follows insertion order; inserting an existing name replaces its
value in place. Cloning is a reference-count bump and an empty map holds no
allocation. Lookups scan the entries, so the map suits the handful of
variables a node produces, not large dictionaries (use a
Value::Object for those).
use graphrt_core::variables::Variables;
use graphrt_core::Value;
let mut outputs = Variables::new();
outputs.insert("text", Value::from("hi"));
let shared = outputs.clone(); // no copy
assert_eq!(shared.get("text"), Some(&Value::from("hi")));
assert_eq!(outputs["missing"], Value::Null);Implementations§
Source§impl Variables
impl Variables
pub const fn new() -> Self
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn get(&self, name: &str) -> Option<&Value>
pub fn get_key_value(&self, name: &str) -> Option<(&VarName, &Value)>
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut Value>
pub fn get_mut(&mut self, name: &str) -> Option<&mut Value>
Mutable access to a value (copies the entries first if they are shared).
pub fn contains_key(&self, name: &str) -> bool
Sourcepub fn insert(
&mut self,
name: impl Into<VarName>,
value: Value,
) -> Option<Value>
pub fn insert( &mut self, name: impl Into<VarName>, value: Value, ) -> Option<Value>
Insert or replace. A replaced value keeps its position and is returned.
Sourcepub fn insert_str(&mut self, name: &str, value: Value) -> Option<Value>
pub fn insert_str(&mut self, name: &str, value: Value) -> Option<Value>
Variables::insert for a borrowed name: the name is only copied when
it is new, so overwriting an existing variable allocates nothing.
Sourcepub fn remove(&mut self, name: &str) -> Option<Value>
pub fn remove(&mut self, name: &str) -> Option<Value>
Remove a variable, keeping the order of the others.
pub fn keys( &self, ) -> impl DoubleEndedIterator<Item = &str> + ExactSizeIterator + '_
pub fn values( &self, ) -> impl DoubleEndedIterator<Item = &Value> + ExactSizeIterator + '_
Sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Whether both maps share the same storage (a clone of one another, unchanged since).
Sourcepub fn to_index_map(&self) -> IndexMap<String, Value>
pub fn to_index_map(&self) -> IndexMap<String, Value>
Copy into an IndexMap with owned names.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Variables
impl<'de> Deserialize<'de> for Variables
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<K: Into<VarName>> Extend<(K, Value)> for Variables
impl<K: Into<VarName>> Extend<(K, Value)> for Variables
Source§fn extend<I: IntoIterator<Item = (K, Value)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, Value)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl Index<&str> for Variables
Indexing a missing name yields Value::Null instead of panicking.
impl Index<&str> for Variables
Indexing a missing name yields Value::Null instead of panicking.