Skip to main content

Mapping

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

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

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

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)

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)

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

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

Returns true if the mapping contains no key-value pairs.

§Examples
use noyalib::Mapping;
assert!(Mapping::new().is_empty());

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>

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

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>

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>

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)>

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)>

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>

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)>

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>

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>

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>

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)
where F: FnMut(&String, &mut Value) -> bool,

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>

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>

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>

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>

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>

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)>

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)>

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)>

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)>

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)>

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)>

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)

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)

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)
where I: IntoIterator<Item = (String, Value)>,

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>

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

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 Clone for Mapping

§

fn clone(&self) -> Mapping

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
§

impl Debug for Mapping

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for Mapping

§

fn default() -> Mapping

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

impl<'de> Deserialize<'de> for Mapping

§

fn deserialize<D>( deserializer: D, ) -> Result<Mapping, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
§

impl Display for Mapping

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<const N: usize> From<[(String, Value); N]> for Mapping

§

fn from(arr: [(String, Value); N]) -> Mapping

Converts to this type from the input type.
§

impl From<IndexMap<String, Value>> for Mapping

§

fn from(map: IndexMap<String, Value>) -> Mapping

Converts to this type from the input type.
§

impl From<IndexMap<String, Value, FxBuildHasher>> for Mapping

§

fn from(map: IndexMap<String, Value, FxBuildHasher>) -> Mapping

Converts to this type from the input type.
§

impl From<Mapping> for Value

§

fn from(v: Mapping) -> Value

Converts to this type from the input type.
§

impl From<Vec<(String, Value)>> for Mapping

§

fn from(v: Vec<(String, Value)>) -> Mapping

Converts to this type from the input type.
§

impl FromIterator<(String, Value)> for Mapping

§

fn from_iter<I>(iter: I) -> Mapping
where I: IntoIterator<Item = (String, Value)>,

Creates a value from an iterator. Read more
§

impl Hash for Mapping

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl Index<&str> for Mapping

§

fn index(&self, key: &str) -> &<Mapping as Index<&str>>::Output

Index into the mapping by key.

§Panics

Panics if the key is not present in the mapping.

§

type Output = Value

The returned type after indexing.
§

impl IndexMut<&str> for Mapping

§

fn index_mut(&mut self, key: &str) -> &mut <Mapping as Index<&str>>::Output

Mutably index into the mapping by key.

§Panics

Panics if the key is not present in the mapping.

§

impl<'a> IntoIterator for &'a Mapping

§

type Item = (&'a String, &'a Value)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, String, Value>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <&'a Mapping as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl<'a> IntoIterator for &'a mut Mapping

§

type Item = (&'a String, &'a mut Value)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, String, Value>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <&'a mut Mapping as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl IntoIterator for Mapping

§

type Item = (String, Value)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<String, Value>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <Mapping as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
§

impl Ord for Mapping

§

fn cmp(&self, other: &Mapping) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

impl PartialEq for Mapping

§

fn eq(&self, other: &Mapping) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl PartialOrd for Mapping

§

fn partial_cmp(&self, other: &Mapping) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl Serialize for Mapping

§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
§

impl Eq for Mapping

§

impl StructuralPartialEq for Mapping

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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,