Struct rune::runtime::Object

source ·
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

source

pub fn new() -> Self

Construct a new object.

§Examples
let object = Object::new();
object.insert("Hello", "World");
source

pub fn with_capacity(capacity: usize) -> Result<Self>

Construct a new object with the given capacity.

source

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);
source

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

pub fn get<Q>(&self, k: &Q) -> Option<&Value>
where String: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

Returns a reference to the value corresponding to the key.

source

pub fn get_value<Q, T>(&self, k: &Q) -> VmResult<Option<T>>
where String: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized, T: FromValue,

Get the given value at the given index.

source

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut Value>
where String: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

Returns a mutable reference to the value corresponding to the key.

source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where String: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

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

source

pub fn remove<Q>(&mut self, k: &Q) -> Option<Value>
where String: Borrow<Q>, Q: Hash + Eq + Ord + ?Sized,

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

source

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.

source

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.

source

pub fn clear(&mut self)

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

source

pub fn into_inner(self) -> BTreeMap<String, Value>

Convert into inner.

source

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

source

pub fn keys(&self) -> Keys<'_, String, Value>

An iterator visiting all keys in arbitrary order. The iterator element type is &'a String.

source

pub fn values(&self) -> Values<'_, String, Value>

An iterator visiting all values in arbitrary order. The iterator element type is &'a Value.

source

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

source

pub fn rune_iter(this: Ref<Self>) -> 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)]);

Trait Implementations§

source§

impl Debug for Object

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Object

source§

fn default() -> Object

Returns the “default value” for a type. Read more
source§

impl FromValue for Object

source§

fn from_value(value: Value) -> VmResult<Self>

Try to convert to the given type, from the given value.
source§

impl InstallWith for Object

source§

fn install_with(module: &mut Module) -> Result<(), ContextError>

Hook to install more things into the module.
source§

impl<'a> IntoIterator for &'a Object

§

type Item = (&'a String, &'a Value)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, String, Value>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a> IntoIterator for &'a mut Object

§

type Item = (&'a String, &'a mut Value)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, String, Value>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoIterator for Object

source§

fn into_iter(self) -> Self::IntoIter

Creates a consuming iterator, that is, one that moves each key-value pair out of the object in arbitrary order. The object cannot be used after calling this.

§

type Item = (String, Value)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<String, Value>

Which kind of iterator are we turning this into?
source§

impl MaybeTypeOf for Object

source§

fn maybe_type_of() -> Option<FullTypeOf>

Type information for the given type.
source§

impl Named for Object

source§

const BASE_NAME: RawStr = _

The generic name of the named thing.
source§

fn full_name() -> Box<str>

The exact type name
source§

impl ToValue for Object

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl TryClone for Object

source§

fn try_clone(&self) -> Result<Self>

Try to clone the current value, raising an allocation error if it’s unsuccessful.
source§

fn try_clone_from(&mut self, source: &Self) -> Result<(), Error>

Performs copy-assignment from source. Read more
source§

impl TryFrom<Object> for Value

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: Object) -> Result<Self, Error>

Performs the conversion.
source§

impl TypeOf for Object

source§

fn type_hash() -> Hash

Get full type hash, including type parameters.
source§

fn type_info() -> TypeInfo

Access diagnostical information on the value type.
source§

fn type_of() -> FullTypeOf

Type information for the given type.
source§

fn type_parameters() -> Hash

Hash of type parameters.
source§

impl UnsafeToMut for Object

§

type Guard = RawMut

The raw guard returned. Read more
source§

unsafe fn unsafe_to_mut<'a>( value: Value ) -> VmResult<(&'a mut Self, Self::Guard)>

Safety Read more
source§

impl UnsafeToRef for Object

§

type Guard = RawRef

The raw guard returned. Read more
source§

unsafe fn unsafe_to_ref<'a>(value: Value) -> VmResult<(&'a Self, Self::Guard)>

Safety Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Object

§

impl !Send for Object

§

impl !Sync for Object

§

impl Unpin for Object

§

impl !UnwindSafe for Object

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> TryToOwned for T
where T: TryClone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn try_to_owned(&self) -> Result<T, Error>

Creates owned data from borrowed data, usually by cloning. Read more
source§

impl<T> UnsafeToValue for T
where T: ToValue,

§

type Guard = ()

The type used to guard the unsafe value conversion.
source§

unsafe fn unsafe_to_value( self ) -> VmResult<(Value, <T as UnsafeToValue>::Guard)>

Convert into a value. Read more
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more