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 into_inner(self) -> BTreeMap<String, Value>
pub fn into_inner(self) -> BTreeMap<String, Value>
Convert into inner.
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
.