Struct toml_edit::OccupiedEntry
source · pub struct OccupiedEntry<'a> { /* private fields */ }
Expand description
A view into a single occupied location in a IndexMap
.
Implementations§
source§impl<'a> OccupiedEntry<'a>
impl<'a> OccupiedEntry<'a>
sourcepub fn key(&self) -> &str
pub fn key(&self) -> &str
Gets a reference to the entry key
Examples
use toml_edit::Table;
let mut map = Table::new();
assert_eq!("foo", map.entry("foo").key());
sourcepub fn into_mut(self) -> &'a mut Item
pub fn into_mut(self) -> &'a mut Item
Converts the OccupiedEntry into a mutable reference to the value in the entry with a lifetime bound to the map itself
Examples found in repository?
src/table.rs (line 630)
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
pub fn or_insert(self, default: Item) -> &'a mut Item {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default),
}
}
/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
pub fn or_insert_with<F: FnOnce() -> Item>(self, default: F) -> &'a mut Item {
match self {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => entry.insert(default()),
}
}
More examples
src/parser/state.rs (line 161)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
pub(crate) fn finalize_table(&mut self) -> Result<(), CustomError> {
let mut table = std::mem::take(&mut self.current_table);
let path = std::mem::take(&mut self.current_table_path);
let root = self.document.as_table_mut();
if path.is_empty() {
assert!(root.is_empty());
std::mem::swap(&mut table, root);
} else if self.current_is_array {
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
let entry = parent_table
.entry_format(key)
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
let array = entry
.as_array_of_tables_mut()
.ok_or_else(|| CustomError::duplicate_key(&path, path.len() - 1))?;
array.push(table);
} else {
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
let key = &path[path.len() - 1];
let entry = parent_table.entry_format(key);
match entry {
crate::Entry::Occupied(entry) => {
match entry.into_mut() {
// if [a.b.c] header preceded [a.b]
Item::Table(ref mut t) if t.implicit => {
std::mem::swap(t, &mut table);
}
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}
crate::Entry::Vacant(entry) => {
let item = Item::Table(table);
entry.insert(item);
}
}
}
Ok(())
}