pub struct MaybeItem<'de> { /* private fields */ }Expand description
A nullable reference to a parsed TOML value.
MaybeItem is returned by the index operators ([]) on Item,
Table, Array, and MaybeItem itself. It acts like an
Option<&Item> that can be further indexed without panicking — chained
lookups on missing keys simply propagate the None state.
Use the as_* accessors to extract a value, or call item
to get back an Option<&Item>.
§Examples
use toml_spanner::Arena;
let arena = Arena::new();
let table = toml_spanner::parse(r#"
[server]
host = "localhost"
port = 8080
"#, &arena)?;
// Successful nested lookup.
assert_eq!(table["server"]["host"].as_str(), Some("localhost"));
assert_eq!(table["server"]["port"].as_i64(), Some(8080));
// Missing keys propagate through chained indexing without panicking.
assert_eq!(table["server"]["missing"].as_str(), None);
assert_eq!(table["nonexistent"]["deep"]["path"].as_str(), None);
// Convert back to an Option<&Item> when needed.
assert!(table["server"]["host"].item().is_some());
assert!(table["nope"].item().is_none());Implementations§
Source§impl<'de> MaybeItem<'de>
impl<'de> MaybeItem<'de>
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns an f64 if this is a float or integer value.
Integer values are converted to f64 via as cast (lossy for large
values outside the 2^53 exact-integer range).
Sourcepub fn as_array(&self) -> Option<&Array<'de>>
pub fn as_array(&self) -> Option<&Array<'de>>
Returns a borrowed array if this is an array value.
Sourcepub fn as_table(&self) -> Option<&Table<'de>>
pub fn as_table(&self) -> Option<&Table<'de>>
Returns a borrowed table if this is a table value.
Trait Implementations§
impl Sync for MaybeItem<'_>
Auto Trait Implementations§
impl<'de> Freeze for MaybeItem<'de>
impl<'de> RefUnwindSafe for MaybeItem<'de>
impl<'de> !Send for MaybeItem<'de>
impl<'de> Unpin for MaybeItem<'de>
impl<'de> UnsafeUnpin for MaybeItem<'de>
impl<'de> UnwindSafe for MaybeItem<'de>
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
Mutably borrows from an owned value. Read more