Struct Attributes

Source
pub struct Attributes<'bp> {
    pub value: Option<SmallIndex>,
    /* private fields */
}

Fields§

§value: Option<SmallIndex>

Implementations§

Source§

impl<'bp> Attributes<'bp>

Source

pub fn empty() -> Self

Create an empty set of attributes

Source

pub fn set(&mut self, key: &'bp str, value: impl Into<ValueKind<'bp>>)

Set an attribute value.


let mut attributes = Attributes::empty();
attributes.set("name", "Nonsense");
attributes.get_as::<&str>("name").unwrap();
Source

pub fn remove(&mut self, key: &'bp str) -> Option<Value<'bp>>

Remove a value from attributes

Source

pub fn value(&self) -> Option<&ValueKind<'bp>>

Get the Value out of attributes. This is always the first item

Source

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

Source

pub fn get_as<'a, T>(&'a self, key: &str) -> Option<T>
where T: TryFrom<&'a ValueKind<'bp>>,

Get a value as a given type. If the value doesn’t exist or can not be cast to the expected type None is returned.


let mut attributes = Attributes::empty();
attributes.set("num", 123);

assert_eq!(attributes.get_as::<u32>("num").unwrap(), 123);
assert_eq!(attributes.get_as::<i16>("num").unwrap(), 123);
Source

pub fn iter_as<'a, T>(&'a self, key: &str) -> impl Iterator<Item = T>
where T: TryFrom<&'a ValueKind<'bp>>,

Iterate over values of a given type


let mut attributes = Attributes::empty();
let values =
    ValueKind::List([ValueKind::Int(1), ValueKind::Bool(true), ValueKind::Int(2)].into());
attributes.set("mixed_list", values);

let iter = attributes.iter_as::<u32>("mixed_list");
assert_eq!(vec![1u32, 2], iter.collect::<Vec<_>>());
Source

pub fn iter(&self) -> impl Iterator<Item = (&ValueKey<'_>, &ValueKind<'bp>)>

Iterate over attributes. This will skip the value

Trait Implementations§

Source§

impl<'bp> Debug for Attributes<'bp>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'bp> Freeze for Attributes<'bp>

§

impl<'bp> RefUnwindSafe for Attributes<'bp>

§

impl<'bp> Send for Attributes<'bp>

§

impl<'bp> Sync for Attributes<'bp>

§

impl<'bp> Unpin for Attributes<'bp>

§

impl<'bp> UnwindSafe for Attributes<'bp>

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.