pub struct Object { /* private fields */ }
Expand description
Struct representing a dynamic anonymous object.
§Rust Examples
use rune::alloc::String;
let mut object = rune::runtime::Object::new();
assert!(object.is_empty());
object.insert_value(String::try_from("foo")?, 42).into_result()?;
object.insert_value(String::try_from("bar")?, true).into_result()?;
assert_eq!(2, object.len());
assert_eq!(Some(42), object.get_value("foo").into_result()?);
assert_eq!(Some(true), object.get_value("bar").into_result()?);
assert_eq!(None::<bool>, object.get_value("baz").into_result()?);
Implementations§
Source§impl Object
impl Object
Sourcepub fn with_capacity(capacity: usize) -> Result<Self>
pub fn with_capacity(capacity: usize) -> Result<Self>
Construct a new object with the given capacity.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the object.
§Examples
let object = Object::with_capacity(16);
object.insert("Hello", "World");
assert_eq!(object.len(), 1);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the object is empty.
§Examples
let object = Object::with_capacity(16);
assert!(object.is_empty());
object.insert("Hello", "World");
assert!(!object.is_empty());
Sourcepub fn get<Q>(&self, k: &Q) -> Option<&Value>
pub fn get<Q>(&self, k: &Q) -> Option<&Value>
Returns a reference to the value corresponding to the key.
Sourcepub fn get_value<Q, T>(&self, k: &Q) -> VmResult<Option<T>>
pub fn get_value<Q, T>(&self, k: &Q) -> VmResult<Option<T>>
Get the given value at the given index.
Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut Value>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut Value>
Returns a mutable reference to the value corresponding to the key.
Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true
if the map contains a value for the specified key.
Sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<Value>
pub fn remove<Q>(&mut self, k: &Q) -> Option<Value>
Removes a key from the map, returning the value at the key if the key was previously in the map.
Sourcepub fn insert_value<T>(&mut self, k: String, v: T) -> VmResult<()>where
T: ToValue,
pub fn insert_value<T>(&mut self, k: String, v: T) -> VmResult<()>where
T: ToValue,
Inserts a key-value pair into the dynamic object, converting it as
necessary through the ToValue
trait.
Sourcepub fn insert(&mut self, k: String, v: Value) -> Result<Option<Value>>
pub fn insert(&mut self, k: String, v: Value) -> Result<Option<Value>>
Inserts a key-value pair into the map.
If the map did not have this key present, None
is returned.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the object, removing all key-value pairs. Keeps the allocated memory for reuse.
Sourcepub fn iter(&self) -> Iter<'_, String, Value>
pub fn iter(&self) -> Iter<'_, String, Value>
An iterator visiting all key-value pairs in arbitrary order.
The iterator element type is (&'a String, &'a Value)
.
Sourcepub fn keys(&self) -> Keys<'_, String, Value>
pub fn keys(&self) -> Keys<'_, String, Value>
An iterator visiting all keys in arbitrary order.
The iterator element type is &'a String
.
Sourcepub fn values(&self) -> Values<'_, String, Value>
pub fn values(&self) -> Values<'_, String, Value>
An iterator visiting all values in arbitrary order.
The iterator element type is &'a Value
.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, String, Value>
pub fn iter_mut(&mut self) -> IterMut<'_, String, Value>
An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values.
The iterator element type is (&'a String, &'a mut Value)
.
Sourcepub fn rune_iter(this: Ref<Self>) -> RuneIter
pub fn rune_iter(this: Ref<Self>) -> RuneIter
An iterator visiting all keys and values in arbitrary order.
§Examples
let object = #{a: 1, b: 2, c: 3};
let vec = [];
for key in object.iter() {
vec.push(key);
}
vec.sort_by(|a, b| a.0.cmp(b.0));
assert_eq!(vec, [("a", 1), ("b", 2), ("c", 3)]);
Sourcepub fn rune_keys(this: Ref<Self>) -> RuneIterKeys
pub fn rune_keys(this: Ref<Self>) -> RuneIterKeys
An iterator visiting all keys in arbitrary order.
§Examples
let object = #{a: 1, b: 2, c: 3};
let vec = [];
for key in object.keys() {
vec.push(key);
}
vec.sort_by(|a, b| a.cmp(b));
assert_eq!(vec, ["a", "b", "c"]);
Sourcepub fn rune_values(this: Ref<Self>) -> RuneValues
pub fn rune_values(this: Ref<Self>) -> RuneValues
An iterator visiting all values in arbitrary order.
§Examples
let object = #{a: 1, b: 2, c: 3};
let vec = [];
for key in object.values() {
vec.push(key);
}
vec.sort_by(|a, b| a.cmp(b));
assert_eq!(vec, [1, 2, 3]);