Struct Mapping
pub struct Mapping(/* private fields */);Expand description
A YAML mapping (dictionary/object).
This is an ordered map that preserves insertion order, wrapping
IndexMap<String, Value>. It provides a comprehensive API for working with
YAML mappings.
§Examples
use noyalib::{Mapping, Value};
let mut map = Mapping::new();
map.insert("name", Value::from("test"));
map.insert("value", Value::from(42));
assert_eq!(map.len(), 2);
assert_eq!(map.get("name").unwrap().as_str(), Some("test"));Implementations§
§impl Mapping
impl Mapping
pub fn new() -> Mapping
pub fn new() -> Mapping
Creates an empty mapping.
§Examples
use noyalib::Mapping;
let m = Mapping::new();
assert!(m.is_empty());pub fn with_capacity(capacity: usize) -> Mapping
pub fn with_capacity(capacity: usize) -> Mapping
Creates an empty mapping with the specified capacity.
Pre-allocates room for capacity entries to avoid
rehashing during the first inserts.
§Examples
use noyalib::Mapping;
let m = Mapping::with_capacity(16);
assert!(m.capacity() >= 16);pub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of key-value pairs the mapping can hold without reallocating.
§Examples
use noyalib::Mapping;
let m = Mapping::with_capacity(8);
assert!(m.capacity() >= 8);pub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more key-value pairs.
§Examples
use noyalib::Mapping;
let mut m = Mapping::new();
m.reserve(64);
assert!(m.capacity() >= 64);pub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the mapping as much as possible.
§Examples
use noyalib::Mapping;
let mut m = Mapping::with_capacity(64);
m.shrink_to_fit();
// capacity may now be 0 or any small implementation-defined value.pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs in the mapping.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
assert_eq!(m.len(), 1);pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the mapping contains no key-value pairs.
§Examples
use noyalib::Mapping;
assert!(Mapping::new().is_empty());pub fn clear(&mut self)
pub fn clear(&mut self)
Clears the mapping, removing all key-value pairs.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.clear();
assert!(m.is_empty());pub fn insert(&mut self, key: impl Into<String>, value: Value) -> Option<Value>
pub fn insert(&mut self, key: impl Into<String>, value: Value) -> Option<Value>
Inserts a key-value pair into the mapping.
If the mapping already had this key present, the value is updated, and the old value is returned.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
assert_eq!(m.insert("a", Value::from(1_i64)), None);
assert_eq!(m.insert("a", Value::from(2_i64)).and_then(|v| v.as_i64()), Some(1));pub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if the mapping contains the specified key.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
assert!(m.contains_key("a"));
assert!(!m.contains_key("b"));pub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Returns a reference to the value corresponding to the key.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
assert_eq!(m.get("a").and_then(Value::as_i64), Some(1));
assert!(m.get("b").is_none());pub fn get_mut(&mut self, key: &str) -> Option<&mut Value>
pub fn get_mut(&mut self, key: &str) -> Option<&mut Value>
Returns a mutable reference to the value corresponding to the key.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
if let Some(v) = m.get_mut("a") {
*v = Value::from(2_i64);
}
assert_eq!(m.get("a").and_then(Value::as_i64), Some(2));pub fn get_index(&self, index: usize) -> Option<(&String, &Value)>
pub fn get_index(&self, index: usize) -> Option<(&String, &Value)>
Returns a reference to the key-value pair at the given index.
Indexing follows insertion order (this is an IndexMap).
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("first", Value::from(1_i64));
m.insert("second", Value::from(2_i64));
assert_eq!(m.get_index(0).map(|(k, _)| k.as_str()), Some("first"));pub fn get_index_mut(&mut self, index: usize) -> Option<(&String, &mut Value)>
pub fn get_index_mut(&mut self, index: usize) -> Option<(&String, &mut Value)>
Returns a mutable reference to the key-value pair at the given index.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
if let Some((_, v)) = m.get_index_mut(0) {
*v = Value::from(99_i64);
}
assert_eq!(m.get("a").and_then(Value::as_i64), Some(99));pub fn remove(&mut self, key: &str) -> Option<Value>
pub fn remove(&mut self, key: &str) -> Option<Value>
Removes a key from the mapping, returning the value if the key was present.
This operation preserves the order of remaining elements
(uses shift_remove semantics, O(n)). For order-agnostic
O(1) removal, see Mapping::swap_remove.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
assert_eq!(m.remove("a").and_then(|v| v.as_i64()), Some(1));
assert!(m.remove("a").is_none());pub fn remove_entry(&mut self, key: &str) -> Option<(String, Value)>
pub fn remove_entry(&mut self, key: &str) -> Option<(String, Value)>
Removes a key from the mapping, returning the key-value pair if present.
This operation preserves the order of remaining elements.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
let (k, v) = m.remove_entry("a").unwrap();
assert_eq!(k, "a");
assert_eq!(v.as_i64(), Some(1));pub fn swap_remove(&mut self, key: &str) -> Option<Value>
pub fn swap_remove(&mut self, key: &str) -> Option<Value>
Removes a key by swapping it with the last element.
This is O(1) but does not preserve order. For
order-preserving removal, see Mapping::remove or
Mapping::shift_remove.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
m.insert("c", Value::from(3_i64));
m.swap_remove("a");
// Order is no longer guaranteed; "c" might now sit where "a" was.
assert_eq!(m.len(), 2);pub fn shift_remove(&mut self, key: &str) -> Option<Value>
pub fn shift_remove(&mut self, key: &str) -> Option<Value>
Removes a key by shifting all elements after it.
This preserves order but is O(n). Equivalent to
Mapping::remove.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
m.shift_remove("a");
assert_eq!(m.iter().next().map(|(k, _)| k.as_str()), Some("b"));pub fn entry(&mut self, key: impl Into<String>) -> Entry<'_, String, Value>
pub fn entry(&mut self, key: impl Into<String>) -> Entry<'_, String, Value>
Gets the entry for the given key for in-place manipulation.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.entry("counter").or_insert(Value::from(0_i64));
if let Some(Value::Number(n)) = m.get_mut("counter") {
if let Some(c) = n.as_i64() { *n = noyalib::Number::Integer(c + 1); }
}
assert_eq!(m.get("counter").and_then(Value::as_i64), Some(1));pub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the key-value pairs specified by the predicate.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
m.insert("c", Value::from(3_i64));
m.retain(|_k, v| v.as_i64().unwrap_or(0) >= 2);
assert_eq!(m.len(), 2);
assert!(!m.contains_key("a"));pub fn iter(&self) -> Iter<'_, String, Value>
pub fn iter(&self) -> Iter<'_, String, Value>
Returns an iterator over the key-value pairs in insertion order.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
let total: i64 = m.iter().filter_map(|(_, v)| v.as_i64()).sum();
assert_eq!(total, 3);pub fn iter_mut(&mut self) -> IterMut<'_, String, Value>
pub fn iter_mut(&mut self) -> IterMut<'_, String, Value>
Returns a mutable iterator over the key-value pairs in insertion order.
§Examples
use noyalib::{Mapping, Number, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
for (_, v) in m.iter_mut() {
if let Value::Number(Number::Integer(n)) = v { *n *= 2; }
}
assert_eq!(m.get("a").and_then(Value::as_i64), Some(2));pub fn keys(&self) -> Keys<'_, String, Value>
pub fn keys(&self) -> Keys<'_, String, Value>
Returns an iterator over the keys in insertion order.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
let keys: Vec<&str> = m.keys().map(String::as_str).collect();
assert_eq!(keys, &["a", "b"]);pub fn values(&self) -> Values<'_, String, Value>
pub fn values(&self) -> Values<'_, String, Value>
Returns an iterator over the values in insertion order.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
let sum: i64 = m.values().filter_map(Value::as_i64).sum();
assert_eq!(sum, 3);pub fn values_mut(&mut self) -> ValuesMut<'_, String, Value>
pub fn values_mut(&mut self) -> ValuesMut<'_, String, Value>
Returns a mutable iterator over the values in insertion order.
§Examples
use noyalib::{Mapping, Number, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(10_i64));
m.insert("b", Value::from(20_i64));
for v in m.values_mut() {
if let Value::Number(Number::Integer(n)) = v { *n /= 10; }
}
assert_eq!(m.get("a").and_then(Value::as_i64), Some(1));pub fn first(&self) -> Option<(&String, &Value)>
pub fn first(&self) -> Option<(&String, &Value)>
Returns the first key-value pair in insertion order.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
assert_eq!(m.first().map(|(k, _)| k.as_str()), Some("a"));pub fn first_mut(&mut self) -> Option<(&String, &mut Value)>
pub fn first_mut(&mut self) -> Option<(&String, &mut Value)>
Returns a mutable reference to the first key-value pair.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
if let Some((_, v)) = m.first_mut() { *v = Value::from(99_i64); }
assert_eq!(m.get("a").and_then(Value::as_i64), Some(99));pub fn last(&self) -> Option<(&String, &Value)>
pub fn last(&self) -> Option<(&String, &Value)>
Returns the last key-value pair in insertion order.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
assert_eq!(m.last().map(|(k, _)| k.as_str()), Some("b"));pub fn last_mut(&mut self) -> Option<(&String, &mut Value)>
pub fn last_mut(&mut self) -> Option<(&String, &mut Value)>
Returns a mutable reference to the last key-value pair.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
if let Some((_, v)) = m.last_mut() { *v = Value::from(99_i64); }
assert_eq!(m.get("b").and_then(Value::as_i64), Some(99));pub fn pop_first(&mut self) -> Option<(String, Value)>
pub fn pop_first(&mut self) -> Option<(String, Value)>
Removes and returns the first key-value pair.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
let (k, _) = m.pop_first().unwrap();
assert_eq!(k, "a");
assert_eq!(m.len(), 1);pub fn pop_last(&mut self) -> Option<(String, Value)>
pub fn pop_last(&mut self) -> Option<(String, Value)>
Removes and returns the last key-value pair.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
let (k, _) = m.pop_last().unwrap();
assert_eq!(k, "b");pub fn sort_keys(&mut self)
pub fn sort_keys(&mut self)
Sorts the mapping by keys (lexicographic order).
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("c", Value::from(3_i64));
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
m.sort_keys();
let keys: Vec<&str> = m.keys().map(String::as_str).collect();
assert_eq!(keys, &["a", "b", "c"]);pub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of key-value pairs.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
m.insert("b", Value::from(2_i64));
m.reverse();
assert_eq!(m.first().map(|(k, _)| k.as_str()), Some("b"));pub fn extend<I>(&mut self, iter: I)
pub fn extend<I>(&mut self, iter: I)
Extends the mapping with the contents of an iterator.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.extend([
("a".to_owned(), Value::from(1_i64)),
("b".to_owned(), Value::from(2_i64)),
]);
assert_eq!(m.len(), 2);pub fn into_inner(self) -> IndexMap<String, Value>
pub fn into_inner(self) -> IndexMap<String, Value>
Consumes the mapping and returns its contents as an IndexMap.
§Examples
use noyalib::{Mapping, Value};
let mut m = Mapping::new();
m.insert("a", Value::from(1_i64));
let inner = m.into_inner();
assert_eq!(inner.len(), 1);pub fn from_inner(map: IndexMap<String, Value>) -> Mapping
pub fn from_inner(map: IndexMap<String, Value>) -> Mapping
Creates a mapping from an IndexMap.
§Examples
use indexmap::IndexMap;
use noyalib::{Mapping, Value};
let mut src = IndexMap::new();
src.insert("a".to_owned(), Value::from(1_i64));
let m = Mapping::from_inner(src);
assert_eq!(m.get("a").and_then(Value::as_i64), Some(1));Trait Implementations§
§impl<'de> Deserialize<'de> for Mapping
impl<'de> Deserialize<'de> for Mapping
§fn deserialize<D>(
deserializer: D,
) -> Result<Mapping, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Mapping, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl FromIterator<(String, Value)> for Mapping
impl FromIterator<(String, Value)> for Mapping
§impl<'a> IntoIterator for &'a Mapping
impl<'a> IntoIterator for &'a Mapping
§impl<'a> IntoIterator for &'a mut Mapping
impl<'a> IntoIterator for &'a mut Mapping
§impl IntoIterator for Mapping
impl IntoIterator for Mapping
§impl Ord for Mapping
impl Ord for Mapping
§impl PartialOrd for Mapping
impl PartialOrd for Mapping
§impl Serialize for Mapping
impl Serialize for Mapping
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Eq for Mapping
impl StructuralPartialEq for Mapping
Auto Trait Implementations§
impl Freeze for Mapping
impl RefUnwindSafe for Mapping
impl Send for Mapping
impl Sync for Mapping
impl Unpin for Mapping
impl UnsafeUnpin for Mapping
impl UnwindSafe for Mapping
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.