Struct ::std::object::Object

Overview

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()?);

Methods

fn new() -> Object

Construct a new object.

Examples

let object = Object::new();
object.insert("Hello", "World");
fn with_capacity(capacity: i64) -> Object

Construct a new object with the given capacity.

Examples

let object = Object::with_capacity(16);
object.insert("Hello", "World");
fn len(self) -> i64

Returns the number of elements in the object.

Examples

let object = Object::with_capacity(16);
object.insert("Hello", "World");
assert_eq!(object.len(), 1);
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());
fn insert(self, k: String, v) -> Option

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);
fn remove(self, key: String) -> Option

Removes a key from the map, returning the value at the key if the key was previously in the map.

Examples

let object = #{a: 42};
assert_eq!(object.remove("a"), Some(42));
assert_eq!(object.remove("a"), None);
fn clear(self) -> Tuple

Clears the object, removing all key-value pairs. Keeps the allocated memory for reuse.

fn contains_key(self, key: String) -> bool

Returns true if the map contains a value for the specified key.

Examples

let object = #{a: 42};
assert!(object.contains_key("a"));
fn get(self, key: String) -> Option

Returns a reference to the value corresponding to the key.

Examples

let object = #{a: 42};
assert_eq!(object.get("a"), Some(42));
assert_eq!(object.get("b"), None);
fn iter(this: Object) -> Iterator

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)]);
fn keys(self) -> Iterator

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();
assert_eq!(vec, ["a", "b", "c"]);

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();
assert_eq!(vec, [1, 2, 3]);

Protocols

protocol into_iter
for item in value { }

Allows the value to be converted into an iterator in a for-loop.

protocol partial_eq
if value == b { }

Allows for partial equality operations to work.

protocol eq
if value == b { }

Allows an equality operation to work.

protocol partial_cmp
if value < b { }

Allows for partial ordering to work.

protocol cmp
if value < b { }

Allows for total ordering to work.