Trait budvm::DynamicValue
source · pub trait DynamicValue: Send + Sync + Debug + 'static {
Show 13 methods
// Required methods
fn is_truthy(&self) -> bool;
fn kind(&self) -> Symbol;
// Provided methods
fn as_i64(&self) -> Option<i64> { ... }
fn convert(&self, kind: &Symbol) -> Option<Value> { ... }
fn partial_eq(&self, other: &Value) -> Option<bool> { ... }
fn partial_cmp(&self, other: &Value) -> Option<Ordering> { ... }
fn checked_add(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind> { ... }
fn checked_sub(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind> { ... }
fn checked_mul(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind> { ... }
fn checked_div(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind> { ... }
fn call(
&self,
name: &Symbol,
args: &mut PoppedValues<'_>
) -> Result<Value, FaultKind> { ... }
fn to_source(&self) -> Option<String> { ... }
fn hash<H>(&self, state: &mut H) -> bool
where H: Hasher { ... }
}
Expand description
A type that can be used in the virtual machine using Value::dynamic
.
Required Methods§
sourcefn is_truthy(&self) -> bool
fn is_truthy(&self) -> bool
Returns true if the value contained is truthy. See
Value::is_truthy()
for examples of what this means for primitive
types.
sourcefn kind(&self) -> Symbol
fn kind(&self) -> Symbol
Returns a unique string identifying this type. This returned string will
be wrapped in ValueKind::Dynamic
when Value::kind()
is invoked
on a dynamic value.
This value does not influence the virtual machine’s behavior. The virtual machine uses this string only when creating error messages.
Provided Methods§
sourcefn as_i64(&self) -> Option<i64>
fn as_i64(&self) -> Option<i64>
Returns this value as an i64
, if possible.
Implementhing this function enables this type to be used in integer operations such as bitwise operations.
sourcefn convert(&self, kind: &Symbol) -> Option<Value>
fn convert(&self, kind: &Symbol) -> Option<Value>
Converts this value to the given kind, if possible.
If this function returns None, the virtual machine will return a fault from the conversion operation.
sourcefn partial_eq(&self, other: &Value) -> Option<bool>
fn partial_eq(&self, other: &Value) -> Option<bool>
Returns true if self and other are considered equal. Returns false if self and other are able to be compared but are not equal. Returns None if the values are unable to be compared.
When evaluating left = right
with dynamic values, if
left.partial_eq(right)
returns None, right.partial_eq(left)
will be
attempted before returning false.
sourcefn partial_cmp(&self, other: &Value) -> Option<Ordering>
fn partial_cmp(&self, other: &Value) -> Option<Ordering>
Returns the relative ordering of self
and other
, if a comparison is
able to be made. If the types cannot be compared, this function should
return None.
When evaluating a comparison such as left < right
with dynamic values,
if left.partial_cmp(right)
returns None,
right.partial_cmp(left).map(Ordering::reverse)
will be attempted
before returning false.
sourcefn checked_add(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind>
fn checked_add( &self, other: &Value, is_reverse: bool ) -> Result<Option<Value>, FaultKind>
Attempts to compute the result adding self and other.
While addition is normally an associative operation, is_reverse
allows
determining if self
is the left operand or the right operand to the
operation.
If is_reverse
is false, the operation being requested is self
+
other
. If is_reverse
is true, the operation being requested is
other
+ self
.
sourcefn checked_sub(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind>
fn checked_sub( &self, other: &Value, is_reverse: bool ) -> Result<Option<Value>, FaultKind>
Attempts to compute the result subtracting self and other.
If is_reverse
is false, the operation being requested is self
-
other
. If is_reverse
is true, the operation being requested is
other
- self
.
sourcefn checked_mul(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind>
fn checked_mul( &self, other: &Value, is_reverse: bool ) -> Result<Option<Value>, FaultKind>
Attempts to compute the result multiplying self and other.
While multiplication is normally an associative operation, is_reverse
allows
determining if self
is the left operand or the right operand to the
operation.
If is_reverse
is false, the operation being requested is self
*
other
. If is_reverse
is true, the operation being requested is
other
* self
.
sourcefn checked_div(
&self,
other: &Value,
is_reverse: bool
) -> Result<Option<Value>, FaultKind>
fn checked_div( &self, other: &Value, is_reverse: bool ) -> Result<Option<Value>, FaultKind>
Attempts to compute the result dividing self and other.
If is_reverse
is false, the operation being requested is self
/
other
. If is_reverse
is true, the operation being requested is
other
/ self
.
sourcefn call(
&self,
name: &Symbol,
args: &mut PoppedValues<'_>
) -> Result<Value, FaultKind>
fn call( &self, name: &Symbol, args: &mut PoppedValues<'_> ) -> Result<Value, FaultKind>
Calls a function by name
with args
.
sourcefn to_source(&self) -> Option<String>
fn to_source(&self) -> Option<String>
Returns this value as represented in source, if possible.
sourcefn hash<H>(&self, state: &mut H) -> boolwhere
H: Hasher,
fn hash<H>(&self, state: &mut H) -> boolwhere H: Hasher,
Hashes the contents of this value. Returns true if this operation is supported.
To allow the Value
type to implement Hash
but contain values
that don’t implement Hash
, this is not a trait that is required to
implement.
This allows safe collection types to be written, which check before key insertion that the value can be hashed and returns an error rather than panicking in the call to hash.