pub struct Table<'de> { /* private fields */ }Expand description
A TOML table with span information.
A Table is the top-level value returned by parse and is
also the value inside any [section] or inline { ... } table in TOML.
It stores (Key, Item) pairs in insertion order.
§Accessing values
- Index operators —
table["key"]returns aMaybeItemthat never panics on missing keys, and supports chained indexing. get/get_mut— returnOption<&Item>/Option<&mut Item>.required/optional— deserialize and remove a field in one step, used when implementingDeserialize. After extracting all expected fields, callexpect_emptyto reject unknown keys.
§Constructing tables
Tables are normally obtained from parse. To build one
programmatically, create a table with Table::new and insert entries
with Table::insert. An Arena is required for
insertion because entries are arena-allocated.
§Iteration
Table implements IntoIterator (both by reference and by value),
yielding (Key, Item) pairs.
Removal via remove, required, and
optional uses swap-remove and may reorder remaining
entries.
Implementations§
Source§impl<'de> Table<'de>
impl<'de> Table<'de>
Sourcepub fn insert(&mut self, key: Key<'de>, value: Item<'de>, arena: &Arena)
pub fn insert(&mut self, key: Key<'de>, value: Item<'de>, arena: &Arena)
Inserts a key-value pair. Does not check for duplicates.
Sourcepub fn get_key_value(&self, name: &str) -> Option<(&Key<'de>, &Item<'de>)>
pub fn get_key_value(&self, name: &str) -> Option<(&Key<'de>, &Item<'de>)>
Linear scan for a key, returning both key and value references.
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut Item<'de>>
pub fn get_mut(&mut self, name: &str) -> Option<&mut Item<'de>>
Returns a mutable reference to the value for name.
Sourcepub fn contains_key(&self, name: &str) -> bool
pub fn contains_key(&self, name: &str) -> bool
Returns true if the table contains the key.
Sourcepub fn remove(&mut self, name: &str) -> Option<Item<'de>>
pub fn remove(&mut self, name: &str) -> Option<Item<'de>>
Removes the first entry matching name, returning its value.
Uses swap-remove, so the ordering of remaining entries may change.
Sourcepub fn remove_entry(&mut self, name: &str) -> Option<(Key<'de>, Item<'de>)>
pub fn remove_entry(&mut self, name: &str) -> Option<(Key<'de>, Item<'de>)>
Removes the first entry matching name, returning the key-value pair.
Uses swap-remove, so the ordering of remaining entries may change.
Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut Item<'de>>
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Item<'de>>
Returns an iterator over mutable references to the values.
Sourcepub fn into_item(self) -> Item<'de>
pub fn into_item(self) -> Item<'de>
Converts this Table into an Item with the same span and payload.
Sourcepub fn required<T: Deserialize<'de>>(
&mut self,
name: &'static str,
) -> Result<T, Error>
pub fn required<T: Deserialize<'de>>( &mut self, name: &'static str, ) -> Result<T, Error>
Removes and deserializes a field, returning an error if the key is missing.