Struct arithmetic_eval::Object

source ·
pub struct Object<T> { /* private fields */ }
Expand description

Object with zero or more named fields.

An object functions similarly to a HashMap with String keys and Value values. It allows iteration over name-value pairs, random access by field name, inserting / removing fields etc.

§Examples

let mut obj = Object::<u32>::default();
obj.insert("field", Value::Prim(0));
obj.insert("other_field", Value::Bool(false));
assert_eq!(obj.len(), 2);
assert_eq!(obj["field"].value_type(), ValueType::Prim);
assert!(obj.iter().all(|(_, val)| !val.is_void()));

// `Object` implements `FromIterator` / `Extend`.
let fields = vec![
    ("third", Value::Prim(3)),
    ("fourth", Value::native_fn(fns::Assert)),
];
let mut other_obj: Object<u32> = fields.into_iter().collect();
other_obj.extend(obj);
assert_eq!(other_obj.len(), 4);

Implementations§

source§

impl<T> Object<T>

source

pub fn just(field_name: impl Into<String>, value: Value<T>) -> Self

Creates an object with a single field.

source

pub fn len(&self) -> usize

Returns the number of fields in this object.

source

pub fn is_empty(&self) -> bool

Checks if this object is empty (has no fields).

source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Value<T>)> + '_

Iterates over name-value pairs for all fields defined in this object.

source

pub fn field_names(&self) -> impl Iterator<Item = &str> + '_

Iterates over field names.

source

pub fn get(&self, field_name: &str) -> Option<&Value<T>>

Returns the value of a field with the specified name, or None if this object does not contain such a field.

source

pub fn contains_field(&self, field_name: &str) -> bool

Checks whether this object has a field with the specified name.

source

pub fn insert( &mut self, field_name: impl Into<String>, value: Value<T>, ) -> Option<Value<T>>

Inserts a field into this object.

source

pub fn remove(&mut self, field_name: &str) -> Option<Value<T>>

Removes and returns the specified field from this object.

Trait Implementations§

source§

impl<T: Clone> Clone for Object<T>

source§

fn clone(&self) -> Object<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Object<T>

source§

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

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

impl<T> Default for Object<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: Display> Display for Object<T>

source§

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

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

impl<T, S, V> Extend<(S, V)> for Object<T>
where S: Into<String>, V: Into<Value<T>>,

source§

fn extend<I: IntoIterator<Item = (S, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T> From<Object<T>> for Value<T>

source§

fn from(object: Object<T>) -> Self

Converts to this type from the input type.
source§

impl<T, S, V> FromIterator<(S, V)> for Object<T>
where S: Into<String>, V: Into<Value<T>>,

source§

fn from_iter<I: IntoIterator<Item = (S, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T> Index<&str> for Object<T>

§

type Output = Value<T>

The returned type after indexing.
source§

fn index(&self, index: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IntoEvalResult<T> for Object<T>

source§

fn into_eval_result(self) -> Result<Value<T>, ErrorOutput>

Performs the conversion.
source§

impl<'r, T> IntoIterator for &'r Object<T>

§

type IntoIter = Map<Iter<'r, String, Value<T>>, fn(_: (&'r String, &'r Value<T>)) -> (&'r str, &'r Value<T>)>

Iterator type should be considered an implementation detail.

§

type Item = (&'r str, &'r Value<T>)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for Object<T>

§

type IntoIter = IntoIter<String, Value<T>>

Iterator type should be considered an implementation detail.

§

type Item = (String, Value<T>)

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq for Object<T>

source§

fn eq(&self, other: &Object<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> TryFromValue<T> for Object<T>

source§

fn try_from_value(value: Value<T>) -> Result<Self, FromValueError>

Attempts to convert value to a type supported by the function.
source§

impl<T> StructuralPartialEq for Object<T>

Auto Trait Implementations§

§

impl<T> Freeze for Object<T>

§

impl<T> !RefUnwindSafe for Object<T>

§

impl<T> !Send for Object<T>

§

impl<T> !Sync for Object<T>

§

impl<T> Unpin for Object<T>
where T: Unpin,

§

impl<T> !UnwindSafe for Object<T>

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.