pub struct Table<'de> { /* private fields */ }Expand description
A TOML table, a transient collection of key/value pairs inside an Item.
Table is an intermediate structure for converting between Rust types and
TOML through FromToml and ToToml. It is the top level value
returned by parse and the value inside any [section]
or inline { ... } table.
Entries are stored in insertion order, but the TOML specification defines table keys as unordered. Avoid relying on iteration order for semantic purposes.
§Accessing values
Use table["key"] for index access, which returns a MaybeItem that
never panics on missing keys and supports chained indexing. Use
get or get_mut for Option based access.
For structured extraction, use Item::table_helper
to create a TableHelper.
§Lookup performance
Direct lookups (get, table["key"]) perform a linear scan
over entries, O(n) in the number of keys. For small tables or a handful
of lookups, as is typical in TOML, this is fast enough.
For structured conversion of larger tables, use
TableHelper via
Document::table_helper or
Item::table_helper, which use the
parser’s hash index for O(1) lookups.
§Constructing tables
To build a table programmatically, create one with Table::new and
insert entries with Table::insert. An Arena is
required because entries are arena allocated.
§Iteration
Table implements IntoIterator (both by reference and by value),
yielding (Key, Item) pairs.
Implementations§
Source§impl<'de> Table<'de>
impl<'de> Table<'de>
Sourcepub fn new() -> Table<'de>
pub fn new() -> Table<'de>
Creates an empty table in format-hints mode (no source span).
The table starts with automatic style: normalization will choose
between inline and header based on content. Call set_style
to override.
Source§impl<'de> Table<'de>
impl<'de> Table<'de>
Sourcepub fn insert(&mut self, key: Key<'de>, value: Item<'de>, arena: &'de Arena)
pub fn insert(&mut self, key: Key<'de>, value: Item<'de>, arena: &'de Arena)
Inserts a key-value pair, replacing any existing entry with the same key.
Performs an O(n) linear scan for duplicates. Prefer Table::insert_unique
when the key is known to be absent.
Sourcepub fn insert_unique(
&mut self,
key: Key<'de>,
value: Item<'de>,
arena: &'de Arena,
)
pub fn insert_unique( &mut self, key: Key<'de>, value: Item<'de>, arena: &'de Arena, )
Inserts a key-value pair without checking for duplicates.
Inserting a duplicate key is sound but the behavior is unspecified. It may panic, produce invalid TOML on emit, or cause deserialization errors.
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_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 entries_mut(&mut self) -> &mut [(Key<'de>, Item<'de>)]
pub fn entries_mut(&mut self) -> &mut [(Key<'de>, Item<'de>)]
Returns a slice of all entries.
Sourcepub fn iter(&self) -> Iter<'_, (Key<'de>, Item<'de>)>
pub fn iter(&self) -> Iter<'_, (Key<'de>, Item<'de>)>
Returns an iterator over all entries (key-value pairs).
Source§impl<'de> Table<'de>
impl<'de> Table<'de>
Sourcepub fn style(&self) -> TableStyle
pub fn style(&self) -> TableStyle
Returns the TableStyle recorded on this table.
Sourcepub fn set_style(&mut self, kind: TableStyle)
pub fn set_style(&mut self, kind: TableStyle)
Pins the TableStyle used when this table is emitted.
Tables built programmatically start out with an unresolved style and
emit picks a rendering from their size and contents (inline for small
tables, a header otherwise). Calling this method locks in kind so
the choice survives emission unchanged.
Source§impl<'de> Table<'de>
impl<'de> Table<'de>
Sourcepub fn set_ignore_source_order(&mut self)
Available on crate feature to-toml only.
pub fn set_ignore_source_order(&mut self)
to-toml only.Disables source-position reordering for this table’s immediate entries during emission. Non-recursive: child tables are unaffected.
Sourcepub fn ignore_source_order(&self) -> bool
Available on crate feature to-toml only.
pub fn ignore_source_order(&self) -> bool
to-toml only.Returns true if source-position reordering is disabled for this table.
Sourcepub fn set_ignore_source_style(&mut self)
Available on crate feature to-toml only.
pub fn set_ignore_source_style(&mut self)
to-toml only.Disables copying structural styles (TableStyle/ArrayStyle) from source during reprojection for this table’s immediate entries. Key spans and reprojection indices are still copied. Non-recursive.
Sourcepub fn ignore_source_style(&self) -> bool
Available on crate feature to-toml only.
pub fn ignore_source_style(&self) -> bool
to-toml only.Returns true if source-style copying is disabled for this table.
Sourcepub fn is_auto_style(&self) -> bool
Available on crate feature to-toml only.
pub fn is_auto_style(&self) -> bool
to-toml only.Returns true if this table has automatic style resolution pending.
Trait Implementations§
Source§impl From<&Table<'_>> for OwnedTable
impl From<&Table<'_>> for OwnedTable
Source§impl<'de> FromFlattened<'de> for Table<'de>
Available on crate feature from-toml only.
impl<'de> FromFlattened<'de> for Table<'de>
from-toml only.Source§impl<'a, 'de> IntoIterator for &'a Table<'de>
impl<'a, 'de> IntoIterator for &'a Table<'de>
Source§impl<'a, 'de> IntoIterator for &'a mut Table<'de>
impl<'a, 'de> IntoIterator for &'a mut Table<'de>
Source§impl<'de> IntoIterator for Table<'de>
impl<'de> IntoIterator for Table<'de>
Source§impl ToFlattened for Table<'_>
Available on crate feature to-toml only.
impl ToFlattened for Table<'_>
to-toml only.Source§fn to_flattened<'a>(
&'a self,
arena: &'a Arena,
table: &mut Table<'a>,
) -> Result<(), ToTomlError>
fn to_flattened<'a>( &'a self, arena: &'a Arena, table: &mut Table<'a>, ) -> Result<(), ToTomlError>
Source§impl ToToml for Table<'_>
Available on crate feature to-toml only.
impl ToToml for Table<'_>
to-toml only.