Skip to main content

Dictionary

Struct Dictionary 

Source
pub struct Dictionary<'a> { /* private fields */ }
Expand description

A validated Dictionary. Every member is kept in order; a repeated key’s last value wins in get and index_into.

§Examples

use parse_net::sfv::{self, BareItem, Member};

let d = sfv::parse_dictionary(b"a=1, b")?;
assert_eq!(d.len(), 2);
assert!(matches!(d.get("b"), Some(Member::Item(i)) if i.bare == BareItem::Boolean(true)));

Implementations§

Source§

impl Dictionary<'_>

Source

pub fn to_owned(&self) -> OwnedDictionary

Available on crate feature alloc only.

Copies the dictionary (one allocation).

§Examples
use parse_net::sfv;

let d = sfv::parse_dictionary(b"a=1, b")?;
assert_eq!(d.to_owned().as_view(), d);
Source§

impl<'a> Dictionary<'a>

Source

pub fn iter(&self) -> DictIter<'a> ⓘ

The members with their keys, in order (repeats included).

§Examples
use parse_net::sfv;

let d = sfv::parse_dictionary(b"a=1, b=2, a=3")?;
assert!(d.iter().map(|(k, _)| k).eq(["a", "b", "a"]));
Source

pub fn get(&self, key: &str) -> Option<Member<'a>>

The member for key: the last occurrence.

§Examples
use parse_net::sfv::{self, BareItem, Member};

let d = sfv::parse_dictionary(b"a=1, b=(x y)")?;
assert!(matches!(d.get("b"), Some(Member::InnerList(l)) if l.len() == 2));
assert!(d.get("c").is_none());
Source

pub fn index_into<'i>( &self, slots: &'i mut [(&'a str, Member<'a>)], ) -> Result<&'i [(&'a str, Member<'a>)], ParseError>

Fills slots with one entry per distinct key (the last value for a repeated key), in first-seen order, and returns the filled part: for callers that look up many keys.

§Errors

ErrorKind::BufferTooSmall when there are more distinct keys than slots.

§Examples
use parse_net::sfv::{self, BareItem, Item, Member};

let d = sfv::parse_dictionary(b"a=1, b=2, a=3")?;
let mut slots = [("", Member::Item(Item::new(BareItem::Boolean(false)))); 4];
let index = d.index_into(&mut slots)?;
assert_eq!(index.len(), 2);
assert!(matches!(index[0], ("a", Member::Item(i)) if i.bare == BareItem::Integer(3)));
Source

pub const fn len(&self) -> usize

The number of members, repeats included.

§Examples
assert_eq!(parse_net::sfv::parse_dictionary(b"a=1, a=2")?.len(), 2);
Source

pub const fn is_empty(&self) -> bool

Whether the dictionary is empty.

§Examples
assert!(!parse_net::sfv::parse_dictionary(b"a")?.is_empty());

Trait Implementations§

Source§

impl<'a> Clone for Dictionary<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Dictionary<'a>

Source§

impl<'a> Debug for Dictionary<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Eq for Dictionary<'a>

Source§

impl<'a> Hash for Dictionary<'a>

Source§

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

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
Source§

impl<'a> PartialEq for Dictionary<'a>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for Dictionary<'a>

Auto Trait Implementations§

§

impl<'a> Forget for Dictionary<'a>

§

impl<'a> Freeze for Dictionary<'a>

§

impl<'a> RefUnwindSafe for Dictionary<'a>

§

impl<'a> Send for Dictionary<'a>

§

impl<'a> Sync for Dictionary<'a>

§

impl<'a> Unpin for Dictionary<'a>

§

impl<'a> UnsafeUnpin for Dictionary<'a>

§

impl<'a> UnwindSafe for Dictionary<'a>

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
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.