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)?;
object.insert_value(String::try_from("bar")?, true)?;
assert_eq!(2, object.len());
assert_eq!(Some(42), object.get_value("foo")?);
assert_eq!(Some(true), object.get_value("bar")?);
assert_eq!(None::<bool>, object.get_value("baz")?);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.
§Examples
let object = Object::with_capacity(16);
object.insert("Hello", "World");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) -> Result<Option<T>, VmError>
pub fn get_value<Q, T>(&self, k: &Q) -> Result<Option<T>, VmError>
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) -> Result<(), VmError>where
T: ToValue,
pub fn insert_value<T>(&mut self, k: String, v: T) -> Result<(), VmError>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.
§Examples
let map = #{};
assert_eq!(map.insert("a", 1), None);
assert_eq!(map.is_empty(), false);
map.insert("b", 2);
assert_eq!(map.insert("b", 3), Some(2));
assert_eq!(map["b"], 3);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]);Trait Implementations§
Source§impl Any for Object
impl Any for Object
Source§const ANY_TYPE_INFO: AnyTypeInfo = _
const ANY_TYPE_INFO: AnyTypeInfo = _
Source§impl Dismantle for Object
An object is made of the values put into it, and a script can nest one
inside another without any bound, so it hands over what it is made of rather
than being dropped in place.
impl Dismantle for Object
An object is made of the values put into it, and a script can nest one inside another without any bound, so it hands over what it is made of rather than being dropped in place.