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: u64) -> Object

Construct a new object with the given capacity.

Examples

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

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: any) -> 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)

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) -> Iter

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(this: Object) -> Keys

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"]);
fn values(this: Object) -> Values

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

impl PartialEq for Object
fn eq(value: any, value1: any) -> bool

Compare two values for equality.

Examples

assert_eq!(1.eq(2), false);
assert_eq!(2.eq(2), true);
assert_eq!(2.eq(1), false);
fn ne(value: any, value1: any) -> bool

Compare two values for inequality.

Examples

assert_eq!(1.ne(2), true);
assert_eq!(2.ne(2), false);
assert_eq!(2.ne(1), true);
impl Eq for Object
impl Clone for Object
fn clone(value: any) -> any

Clone the specified value.

Examples

let a = 42;
let b = a;
let c = a.clone();

a += 1;

assert_eq!(a, 43);
assert_eq!(b, 42);
assert_eq!(c, 42);

Protocols

protocol PARTIAL_EQ
if value == b { }

Test two objects for partial equality.

Examples

let a = #{a: 42};
let b = #{a: 43};

assert_eq!(a, a);
assert_ne!(a, b);
assert_ne!(b, a);
protocol EQ
if value == b { }

Test two objects for total equality.

Examples

use std::ops::eq;

let a = #{a: 42};
let b = #{a: 43};

assert_eq!(eq(a, a), true);
assert_eq!(eq(a, b), false);
assert_eq!(eq(b, a), false);
protocol CLONE
let $out = clone(value)

Clones an object.

Examples

let a = #{a: 42};
let b = a.clone();
assert_eq!(a, b);

b.b = 43;
assert_ne!(a, b);
protocol DEBUG_FMT
format!("{:?}", value)

Write a debug representation of an object.

Examples

let a = #{a: 42, b: 43};

println!("{a:?}");
protocol INTO_ITER
for item in value { }

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