pub struct Value { /* private fields */ }Expand description
An entry on the stack.
Implementations§
Source§impl Value
impl Value
Sourcepub fn take(value: &mut Self) -> Self
pub fn take(value: &mut Self) -> Self
Take a mutable value, replacing the original location with an empty value.
Sourcepub fn new<T>(data: T) -> Result<Self>where
T: Any,
pub fn new<T>(data: T) -> Result<Self>where
T: Any,
Construct a value from a type that implements Any which owns the
underlying value.
Sourcepub unsafe fn from_ref<T>(data: &T) -> Result<(Self, ValueRefGuard)>where
T: Any,
pub unsafe fn from_ref<T>(data: &T) -> Result<(Self, ValueRefGuard)>where
T: Any,
Construct an Any that wraps a pointer.
§Safety
Caller must ensure that the returned Value doesn’t outlive the
reference it is wrapping.
This would be an example of incorrect use:
use rune::Any;
use rune::runtime::Value;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
unsafe {
let (any, guard) = unsafe { Value::from_ref(&v)? };
drop(v);
// any use of `any` beyond here is undefined behavior.
}§Examples
use rune::Any;
use rune::runtime::Value;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
unsafe {
let (any, guard) = Value::from_ref(&mut v)?;
let b = any.borrow_ref::<Foo>()?;
assert_eq!(b.0, 1u32);
}Sourcepub unsafe fn from_mut<T>(data: &mut T) -> Result<(Self, ValueMutGuard)>where
T: Any,
pub unsafe fn from_mut<T>(data: &mut T) -> Result<(Self, ValueMutGuard)>where
T: Any,
Construct a value that wraps a mutable pointer.
§Safety
Caller must ensure that the returned Value doesn’t outlive the
reference it is wrapping.
This would be an example of incorrect use:
use rune::Any;
use rune::runtime::Value;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
unsafe {
let (any, guard) = Value::from_mut(&mut v)?;
drop(v);
// any use of value beyond here is undefined behavior.
}§Examples
use rune::Any;
use rune::runtime::Value;
#[derive(Any)]
struct Foo(u32);
let mut v = Foo(1u32);
unsafe {
let (any, guard) = Value::from_mut(&mut v)?;
if let Ok(mut v) = any.borrow_mut::<Foo>() {
v.0 += 1;
}
drop(guard);
assert!(any.borrow_mut::<Foo>().is_err());
drop(any);
}
assert_eq!(v.0, 2);Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Test if the value is writable.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
{
assert!(value.is_writable());
let borrowed = value.borrow_mut::<Struct>()?;
assert!(!value.is_writable());
drop(borrowed);
assert!(value.is_writable());
}
let foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_ref(&foo)? };
assert!(value.is_readable());
assert!(!value.is_writable());
}
let mut foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_mut(&mut foo)? };
assert!(value.is_readable());
assert!(value.is_writable());
}Sourcepub fn is_readable(&self) -> bool
pub fn is_readable(&self) -> bool
Test if a value is readable.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
{
assert!(value.is_writable());
let borrowed = value.borrow_mut::<Struct>()?;
assert!(!value.is_writable());
drop(borrowed);
assert!(value.is_writable());
}
let foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_ref(&foo)? };
assert!(value.is_readable());
assert!(!value.is_writable());
}
let mut foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_mut(&mut foo)? };
assert!(value.is_readable());
assert!(value.is_writable());
}Sourcepub fn display_fmt(&self, f: &mut Formatter) -> Result<(), VmError>
pub fn display_fmt(&self, f: &mut Formatter) -> Result<(), VmError>
Format the value using the DISPLAY_FMT protocol.
You must use Vm::with to specify which virtual machine this function
is called inside.
§Errors
This function errors if called outside of a virtual machine.
Sourcepub fn into_iter(self) -> Result<Iterator, VmError>
pub fn into_iter(self) -> Result<Iterator, VmError>
Convert value into an iterator using the Protocol::INTO_ITER
protocol.
You must use Vm::with to specify which virtual machine this function
is called inside.
§Errors
This function will error if called outside of a virtual machine context.
Sourcepub fn into_type_name(self) -> Result<String, VmError>
pub fn into_type_name(self) -> Result<String, VmError>
Retrieves a human readable type name for the current value.
You must use Vm::with to specify which virtual machine this function
is called inside.
§Errors
This function errors in case the provided type cannot be converted into
a name without the use of a Vm and one is not provided through the
environment.
Sourcepub fn empty_struct(rtti: Arc<Rtti>) -> Result<Self>
pub fn empty_struct(rtti: Arc<Rtti>) -> Result<Self>
Construct an empty.
Sourcepub fn tuple_struct(
rtti: Arc<Rtti>,
data: impl IntoIterator<IntoIter: ExactSizeIterator, Item = Value>,
) -> Result<Self>
pub fn tuple_struct( rtti: Arc<Rtti>, data: impl IntoIterator<IntoIter: ExactSizeIterator, Item = Value>, ) -> Result<Self>
Construct a typed tuple.
Sourcepub fn as_usize(&self) -> Result<usize, RuntimeError>
pub fn as_usize(&self) -> Result<usize, RuntimeError>
Try to coerce value into a usize.
Sourcepub fn as_string(&self) -> Result<BorrowRef<'_, str>, RuntimeError>
👎Deprecated: For consistency with other methods, this has been renamed Value::borrow_string_ref
pub fn as_string(&self) -> Result<BorrowRef<'_, str>, RuntimeError>
For consistency with other methods, this has been renamed Value::borrow_string_ref
Get the value as a string.
Sourcepub fn borrow_string_ref(&self) -> Result<BorrowRef<'_, str>, RuntimeError>
pub fn borrow_string_ref(&self) -> Result<BorrowRef<'_, str>, RuntimeError>
Borrow the interior value as a string reference.
Sourcepub fn into_string(self) -> Result<String, RuntimeError>
pub fn into_string(self) -> Result<String, RuntimeError>
Take the current value as a string.
Sourcepub fn into_unit(&self) -> Result<(), RuntimeError>
pub fn into_unit(&self) -> Result<(), RuntimeError>
Coerce into a unit.
Sourcepub fn as_ordering(&self) -> Result<Ordering, RuntimeError>
pub fn as_ordering(&self) -> Result<Ordering, RuntimeError>
Coerce into Ordering.
This gets a copy of the value.
Sourcepub fn as_ordering_mut(&mut self) -> Result<&mut Ordering, RuntimeError>
pub fn as_ordering_mut(&mut self) -> Result<&mut Ordering, RuntimeError>
Coerce into Ordering.
This gets the value by mutable reference.
Sourcepub fn as_hash(&self) -> Result<Hash, RuntimeError>
pub fn as_hash(&self) -> Result<Hash, RuntimeError>
Coerce into Hash.
This gets a copy of the value.
Sourcepub fn as_hash_mut(&mut self) -> Result<&mut Hash, RuntimeError>
pub fn as_hash_mut(&mut self) -> Result<&mut Hash, RuntimeError>
Coerce into Hash.
This gets the value by mutable reference.
Sourcepub fn as_bool(&self) -> Result<bool, RuntimeError>
pub fn as_bool(&self) -> Result<bool, RuntimeError>
Coerce into bool.
This gets a copy of the value.
Sourcepub fn as_bool_mut(&mut self) -> Result<&mut bool, RuntimeError>
pub fn as_bool_mut(&mut self) -> Result<&mut bool, RuntimeError>
Coerce into bool.
This gets the value by mutable reference.
Sourcepub fn as_char(&self) -> Result<char, RuntimeError>
pub fn as_char(&self) -> Result<char, RuntimeError>
Coerce into char.
This gets a copy of the value.
Sourcepub fn as_char_mut(&mut self) -> Result<&mut char, RuntimeError>
pub fn as_char_mut(&mut self) -> Result<&mut char, RuntimeError>
Coerce into char.
This gets the value by mutable reference.
Sourcepub fn as_signed(&self) -> Result<i64, RuntimeError>
pub fn as_signed(&self) -> Result<i64, RuntimeError>
Coerce into i64 integer.
This gets a copy of the value.
Sourcepub fn as_signed_mut(&mut self) -> Result<&mut i64, RuntimeError>
pub fn as_signed_mut(&mut self) -> Result<&mut i64, RuntimeError>
Coerce into i64 integer.
This gets the value by mutable reference.
Sourcepub fn as_unsigned(&self) -> Result<u64, RuntimeError>
pub fn as_unsigned(&self) -> Result<u64, RuntimeError>
Coerce into u64 unsigned integer.
This gets a copy of the value.
Sourcepub fn as_unsigned_mut(&mut self) -> Result<&mut u64, RuntimeError>
pub fn as_unsigned_mut(&mut self) -> Result<&mut u64, RuntimeError>
Coerce into u64 unsigned integer.
This gets the value by mutable reference.
Sourcepub fn as_float(&self) -> Result<f64, RuntimeError>
pub fn as_float(&self) -> Result<f64, RuntimeError>
Coerce into f64 float.
This gets a copy of the value.
Sourcepub fn as_float_mut(&mut self) -> Result<&mut f64, RuntimeError>
pub fn as_float_mut(&mut self) -> Result<&mut f64, RuntimeError>
Coerce into f64 float.
This gets the value by mutable reference.
Sourcepub fn as_type(&self) -> Result<Type, RuntimeError>
pub fn as_type(&self) -> Result<Type, RuntimeError>
Coerce into Type.
This gets a copy of the value.
Sourcepub fn as_type_mut(&mut self) -> Result<&mut Type, RuntimeError>
pub fn as_type_mut(&mut self) -> Result<&mut Type, RuntimeError>
Coerce into Type.
This gets the value by mutable reference.
Sourcepub fn borrow_tuple_ref(&self) -> Result<BorrowRef<'_, Tuple>, RuntimeError>
pub fn borrow_tuple_ref(&self) -> Result<BorrowRef<'_, Tuple>, RuntimeError>
Borrow as a tuple.
This ensures that the value has read access to the underlying value and does not consume it.
Sourcepub fn borrow_tuple_mut(&self) -> Result<BorrowMut<'_, Tuple>, RuntimeError>
pub fn borrow_tuple_mut(&self) -> Result<BorrowMut<'_, Tuple>, RuntimeError>
Borrow as a tuple as mutable.
This ensures that the value has write access to the underlying value and does not consume it.
Sourcepub fn into_tuple(&self) -> Result<Box<Tuple>, RuntimeError>
pub fn into_tuple(&self) -> Result<Box<Tuple>, RuntimeError>
Borrow as an owned tuple reference.
This ensures that the value has read access to the underlying value and does not consume it.
Sourcepub fn into_tuple_ref(&self) -> Result<Ref<Tuple>, RuntimeError>
pub fn into_tuple_ref(&self) -> Result<Ref<Tuple>, RuntimeError>
Borrow as an owned tuple reference.
This ensures that the value has read access to the underlying value and does not consume it.
Sourcepub fn into_tuple_mut(&self) -> Result<Mut<Tuple>, RuntimeError>
pub fn into_tuple_mut(&self) -> Result<Mut<Tuple>, RuntimeError>
Borrow as an owned tuple mutable.
This ensures that the value has write access to the underlying value and does not consume it.
Sourcepub fn into_any_obj(self) -> Result<AnyObj, RuntimeError>
pub fn into_any_obj(self) -> Result<AnyObj, RuntimeError>
Coerce into an AnyObj.
Coerce into a Shared<T>.
This type checks and coerces the value into a type which statically guarantees that the underlying type is of the given type.
Sourcepub fn into_future(self) -> Result<Future, RuntimeError>
pub fn into_future(self) -> Result<Future, RuntimeError>
Coerce into a future, or convert into a future using the Protocol::INTO_FUTURE protocol.
You must use Vm::with to specify which virtual machine this function
is called inside.
§Errors
This function errors in case the provided type cannot be converted into
a future without the use of a Vm and one is not provided through the
environment.
Sourcepub fn into_any_ref_ptr<T>(
self,
) -> Result<(NonNull<T>, RawValueGuard), RuntimeError>where
T: Any,
pub fn into_any_ref_ptr<T>(
self,
) -> Result<(NonNull<T>, RawValueGuard), RuntimeError>where
T: Any,
Try to coerce value into a typed reference.
§Safety
The returned pointer is only valid to dereference as long as the returned guard is live.
Sourcepub fn downcast<T>(self) -> Result<T, RuntimeError>where
T: Any,
pub fn downcast<T>(self) -> Result<T, RuntimeError>where
T: Any,
Downcast the value into a stored value that implements Any.
This takes the interior value, making it inaccessible to other owned references.
You should usually prefer to use rune::from_value instead of this
directly.
§Examples
use rune::Value;
use rune::alloc::String;
let a = Value::try_from("Hello World")?;
let b = a.clone();
assert!(b.borrow_ref::<String>().is_ok());
// NB: The interior representation of the stored string is from rune-alloc.
let a = a.downcast::<String>()?;
assert!(b.borrow_ref::<String>().is_err());
assert_eq!(a, "Hello World");Sourcepub fn borrow_ref<T>(&self) -> Result<BorrowRef<'_, T>, RuntimeError>where
T: Any,
pub fn borrow_ref<T>(&self) -> Result<BorrowRef<'_, T>, RuntimeError>where
T: Any,
Borrow the value as a typed reference of type T.
§Examples
use rune::Value;
use rune::alloc::String;
let a = Value::try_from("Hello World")?;
let b = a.clone();
assert!(b.borrow_ref::<String>().is_ok());
// NB: The interior representation of the stored string is from rune-alloc.
let a = a.downcast::<String>()?;
assert!(b.borrow_ref::<String>().is_err());
assert_eq!(a, "Hello World");Sourcepub fn into_ref<T>(self) -> Result<Ref<T>, RuntimeError>where
T: Any,
pub fn into_ref<T>(self) -> Result<Ref<T>, RuntimeError>where
T: Any,
Try to coerce value into a typed reference of type T.
You should usually prefer to use rune::from_value instead of this
directly.
§Examples
use rune::Value;
use rune::alloc::String;
let mut a = Value::try_from("Hello World")?;
let b = a.clone();
assert_eq!(a.into_ref::<String>()?.as_str(), "Hello World");
assert_eq!(b.into_ref::<String>()?.as_str(), "Hello World");Sourcepub fn borrow_mut<T>(&self) -> Result<BorrowMut<'_, T>, RuntimeError>where
T: Any,
pub fn borrow_mut<T>(&self) -> Result<BorrowMut<'_, T>, RuntimeError>where
T: Any,
Try to borrow value into a typed mutable reference of type T.
Sourcepub fn into_mut<T>(self) -> Result<Mut<T>, RuntimeError>where
T: Any,
pub fn into_mut<T>(self) -> Result<Mut<T>, RuntimeError>where
T: Any,
Try to coerce value into a typed mutable reference of type T.
You should usually prefer to use rune::from_value instead of this
directly since it supports transparently coercing into types like
Mut<str>.
§Examples
use rune::{Mut, Value};
use rune::alloc::String;
let mut a = Value::try_from("Hello World")?;
let b = a.clone();
fn modify_string(mut s: Mut<String>) {
assert_eq!(s.as_str(), "Hello World");
s.make_ascii_lowercase();
assert_eq!(s.as_str(), "hello world");
}
modify_string(a.into_mut::<String>()?);
assert_eq!(b.borrow_mut::<String>()?.as_str(), "hello world");Sourcepub fn type_hash(&self) -> Hash
pub fn type_hash(&self) -> Hash
Get the type hash for the current value.
One notable feature is that the type of a variant is its container enum, and not the type hash of the variant itself.
Sourcepub fn partial_eq(a: &Value, b: &Value) -> Result<bool, VmError>
pub fn partial_eq(a: &Value, b: &Value) -> Result<bool, VmError>
Perform a partial equality test between two values.
This is the basis for the eq operation (partial_eq / ‘==’).
External types will use the Protocol::PARTIAL_EQ protocol when
invoked through this function.
§Errors
This function will error if called outside of a virtual machine context.
Sourcepub fn eq(&self, b: &Value) -> Result<bool, VmError>
pub fn eq(&self, b: &Value) -> Result<bool, VmError>
Perform a total equality test between two values.
This is the basis for the eq operation (==).
External types will use the Protocol::EQ protocol when invoked
through this function.
§Errors
This function will error if called outside of a virtual machine context.
Sourcepub fn partial_cmp(a: &Value, b: &Value) -> Result<Option<Ordering>, VmError>
pub fn partial_cmp(a: &Value, b: &Value) -> Result<Option<Ordering>, VmError>
Perform a partial ordering comparison between two values.
This is the basis for the comparison operation.
External types will use the Protocol::PARTIAL_CMP protocol when
invoked through this function.
§Errors
This function will error if called outside of a virtual machine context.
Sourcepub fn cmp(a: &Value, b: &Value) -> Result<Ordering, VmError>
pub fn cmp(a: &Value, b: &Value) -> Result<Ordering, VmError>
Perform a total ordering comparison between two values.
This is the basis for the comparison operation (cmp).
External types will use the Protocol::CMP protocol when invoked
through this function.
§Errors
This function will error if called outside of a virtual machine context.
Sourcepub fn as_integer<T>(&self) -> Result<T, RuntimeError>
pub fn as_integer<T>(&self) -> Result<T, RuntimeError>
Try to coerce the current value as the specified integer T.
§Examples
let value = rune::to_value(u32::MAX)?;
assert_eq!(value.as_integer::<u64>()?, u32::MAX as u64);
assert!(value.as_integer::<i32>().is_err());
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
Available on crate feature serde only.Deserialize implementation for value pointers.
impl<'de> Deserialize<'de> for Value
serde only.Deserialize implementation for value pointers.
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Dismantle for Value
A value is the leaf of the walk: it is handed over as it is, unless it
cannot contain other values, in which case there is nothing to hand over.
impl Dismantle for Value
A value is the leaf of the walk: it is handed over as it is, unless it cannot contain other values, in which case there is nothing to hand over.
This is what makes handing a field over the same call whatever the field holds, which is what the derive writes.
Source§impl Drop for Value
A graph of values is taken apart in place rather than by recursing into it,
since how deeply one nests is decided at runtime and dropping it by
recursing would exhaust the call stack.
impl Drop for Value
A graph of values is taken apart in place rather than by recursing into it, since how deeply one nests is decided at runtime and dropping it by recursing would exhaust the call stack.
A destructor has nobody to report a failure to and must not spend the memory limit which is in effect, so it takes the graph apart without allocating. The machine hands the values over to a worklist instead, which is faster and reports running into the limit rather than working around it.
See the dismantle module for how the graph is walked.
Source§impl From<AnyObj> for Value
impl From<AnyObj> for Value
§Examples
use rune::Value;
use rune::runtime::AnyObj;
use rune::alloc::String;
let string = String::try_from("Hello World")?;
let string = AnyObj::new(string)?;
let string = Value::from(string);
let string = string.into_shared::<String>()?;
assert_eq!(string.borrow_ref()?.as_str(), "Hello World");§Examples
use rune::Value;
use rune::runtime::Shared;
use rune::alloc::String;
let string = String::try_from("Hello World")?;
let string = Shared::new(string)?;
let string = Value::from(string);
let string = string.into_any_obj()?;
assert_eq!(string.borrow_ref::<String>()?.as_str(), "Hello World");Source§impl FromValue for Value
impl FromValue for Value
Source§fn from_value(value: Value) -> Result<Self, RuntimeError>
fn from_value(value: Value) -> Result<Self, RuntimeError>
impl IntoOutput for Value
Source§impl MaybeTypeOf for Value
impl MaybeTypeOf for Value
Source§fn maybe_type_of() -> Result<DocType>
fn maybe_type_of() -> Result<DocType>
Source§impl Serialize for Value
Available on crate feature serde only.Serialize implementation for value pointers.
impl Serialize for Value
serde only.Serialize implementation for value pointers.