rune::runtime

Struct 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: ?Sized + Hash + Eq + Ord,

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: ?Sized + Hash + Eq + Ord, 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: ?Sized + Hash + Eq + Ord,

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: ?Sized + Hash + Eq + Ord,

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: ?Sized + Hash + Eq + Ord,

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 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>) -> 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)]);
Source

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"]);
Source

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

Source§

const ANY_TYPE_INFO: AnyTypeInfo = _

The compile-time type information know for the type.
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 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

Source§

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

The type of the elements being iterated over.
Source§

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

Source§

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

The type of the elements being iterated over.
Source§

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.

Source§

type Item = (String, Value)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<String, Value>

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

impl IntoOutput for Object

Source§

fn into_output(self) -> Result<Value, RuntimeError>

Coerce the current value into an output.
Source§

impl MaybeTypeOf for Object

Source§

fn maybe_type_of() -> Result<DocType>

Type information for the given type.
Source§

impl Named for Object

Source§

const ITEM: &'static Item = _

The name item.
Source§

fn full_name(f: &mut Formatter<'_>) -> Result

The exact type name
Source§

fn display() -> impl Display

Return a display wrapper for the named type.
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

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl TypeHash for Object

Source§

const HASH: Hash = _

The complete type hash of the type including type parameters which uniquely identifiers a given type. Read more
Source§

impl TypeOf for Object

Source§

const PARAMETERS: Hash = _

Type parameters for the type. Read more
Source§

const STATIC_TYPE_INFO: AnyTypeInfo = <Self as rune::Any>::ANY_TYPE_INFO

Access diagnostical type information for the current type. Read more
Source§

fn type_info() -> TypeInfo

Get type info associated with the current type.
Source§

impl UnsafeToMut for Object

Source§

type Guard = RawValueGuard

The raw guard returned. Read more
Source§

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

Safety Read more
Source§

impl UnsafeToRef for Object

Source§

type Guard = RawValueGuard

The raw guard returned. Read more
Source§

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

Safety Read more
Source§

impl UnsafeToValue for &Object

Source§

type Guard = ValueRefGuard

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

unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>

Convert into a value. Read more
Source§

impl UnsafeToValue for &mut Object

Source§

type Guard = ValueMutGuard

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

unsafe fn unsafe_to_value(self) -> Result<(Value, Self::Guard), RuntimeError>

Convert into a value. Read more

Auto Trait Implementations§

§

impl Freeze for Object

§

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> FromValue for T
where T: AnyMarker,

Source§

fn from_value(value: Value) -> Result<T, RuntimeError>

Try to convert to the given type, from the given value.
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

Source§

type Output = T

Should always be Self
Source§

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

Source§

fn to_return(self) -> VmResult<Value>

Convert something into a return value.
Source§

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

Source§

fn to_value(self) -> Result<Value, RuntimeError>

Convert into a value.
Source§

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

Source§

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>,

Source§

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,

Source§

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,

Source§

type Guard = ()

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

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

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T