pub struct Value<'v>(/* private fields */);
Expand description
A Starlark value. The lifetime argument 'v
corresponds to the Heap
it is stored on.
Many of the methods simply forward to the underlying StarlarkValue
.
The Display
trait is equivalent to the repr()
function in Starlark.
Implementations§
source§impl<'v> Value<'v>
impl<'v> Value<'v>
sourcepub fn new_frozen(x: FrozenValue) -> Self
pub fn new_frozen(x: FrozenValue) -> Self
Turn a FrozenValue
into a Value
. See the safety warnings on
OwnedFrozenValue
.
sourcepub fn unpack_frozen(self) -> Option<FrozenValue>
pub fn unpack_frozen(self) -> Option<FrozenValue>
Obtain the underlying FrozenValue
from inside the Value
, if it is one.
sourcepub fn unpack_bool(self) -> Option<bool>
pub fn unpack_bool(self) -> Option<bool>
Obtain the underlying bool
if it is a boolean.
sourcepub fn unpack_i32(self) -> Option<i32>
pub fn unpack_i32(self) -> Option<i32>
Obtain the underlying integer if it fits in an i32
.
Note floats are not considered integers, i. e. unpack_i32
for 1.0
will return None
.
sourcepub fn unpack_starlark_str(self) -> Option<&'v StarlarkStr>
pub fn unpack_starlark_str(self) -> Option<&'v StarlarkStr>
Like unpack_str
, but gives a pointer to a boxed string.
Mostly useful for when you want to convert the string to a dyn
trait, but can’t
form a dyn
of an unsized type.
Unstable and likely to be removed in future, as the presence of the Box
is
not a guaranteed part of the API.
sourcepub fn unpack_str(self) -> Option<&'v str>
pub fn unpack_str(self) -> Option<&'v str>
Obtain the underlying str
if it is a string.
sourcepub fn ptr_eq(self, other: Value<'_>) -> bool
pub fn ptr_eq(self, other: Value<'_>) -> bool
Are two Value
s equal, looking at only their underlying pointer. This function is
low-level and provides two guarantees.
- It is reflexive, the same
Value
passed as both arguments will result intrue
. - If this function is
true
, thenValue::equals
will also consider them equal.
Note that other properties are not guaranteed, and the result is not considered part of the API. The result can be impacted by optimisations such as hash-consing, copy-on-write, partial evaluation etc.
sourcepub fn identity(self) -> ValueIdentity<'v>
pub fn identity(self) -> ValueIdentity<'v>
Returns an identity for this Value
, derived from its pointer. This function is
low-level and provides two guarantees. Those are valid until the next GC:
- Calling it multiple times on the same
Value
will returnValueIdentity
that compare equal. - If two [
Value]
haveValueIdentity
that compare equal, thenValue::ptr_eq
andValue::equals
will also consider them to be equal.
sourcepub fn slice(
self,
start: Option<Value<'v>>,
stop: Option<Value<'v>>,
stride: Option<Value<'v>>,
heap: &'v Heap
) -> Result<Value<'v>>
pub fn slice( self, start: Option<Value<'v>>, stop: Option<Value<'v>>, stride: Option<Value<'v>>, heap: &'v Heap ) -> Result<Value<'v>>
x[start:stop:stride]
.
sourcepub fn parameters_spec(self) -> Option<&'v ParametersSpec<Value<'v>>>
pub fn parameters_spec(self) -> Option<&'v ParametersSpec<Value<'v>>>
Callable parameters if known.
For now it only returns parameter spec for def
and lambda
.
sourcepub fn get_type_value(self) -> FrozenStringValue
pub fn get_type_value(self) -> FrozenStringValue
type(x)
.
sourcepub fn freeze(self, freezer: &Freezer) -> Result<FrozenValue>
pub fn freeze(self, freezer: &Freezer) -> Result<FrozenValue>
Convert a value to a FrozenValue
using a supplied Freezer
.
sourcepub fn to_str(self) -> String
pub fn to_str(self) -> String
Implement the str()
function - converts a string value to itself,
otherwise uses repr()
.
sourcepub fn to_json(self) -> Result<String>
pub fn to_json(self) -> Result<String>
Convert the value to JSON.
Return an error if the value or any contained value does not support conversion to JSON.
sourcepub fn to_json_value(self) -> Result<Value>
pub fn to_json_value(self) -> Result<Value>
Convert the value to JSON value.
sourcepub fn set_attr(self, attribute: &str, alloc_value: Value<'v>) -> Result<()>
pub fn set_attr(self, attribute: &str, alloc_value: Value<'v>) -> Result<()>
Forwards to StarlarkValue::set_attr
.
sourcepub fn set_at(self, index: Value<'v>, alloc_value: Value<'v>) -> Result<()>
pub fn set_at(self, index: Value<'v>, alloc_value: Value<'v>) -> Result<()>
Forwards to StarlarkValue::set_at
.
sourcepub fn documentation(self) -> Option<DocItem>
pub fn documentation(self) -> Option<DocItem>
Forwards to StarlarkValue::documentation
.
sourcepub fn iterate(self, heap: &'v Heap) -> Result<StarlarkIterator<'v>>
pub fn iterate(self, heap: &'v Heap) -> Result<StarlarkIterator<'v>>
Produce an iterable from a value.
sourcepub fn equals(self, other: Value<'v>) -> Result<bool>
pub fn equals(self, other: Value<'v>) -> Result<bool>
Are two values equal. If the values are of different types it will
return false
. It will only error if there is excessive recursion.
sourcepub fn compare(self, other: Value<'v>) -> Result<Ordering>
pub fn compare(self, other: Value<'v>) -> Result<Ordering>
How are two values comparable. For values of different types will return Err
.
sourcepub fn describe(self, name: &str) -> String
pub fn describe(self, name: &str) -> String
Describe the value, in order to get its metadata in a way that could be used
to generate prototypes, help information or whatever other descriptive text
is required.
Plan is to make this return a data type at some point in the future, possibly
move on to StarlarkValue
and include data from members.
sourcepub fn export_as(
self,
variable_name: &str,
eval: &mut Evaluator<'v, '_>
) -> Result<()>
pub fn export_as( self, variable_name: &str, eval: &mut Evaluator<'v, '_> ) -> Result<()>
Call export_as
on the underlying value, but only if the type is mutable.
Otherwise, does nothing.
sourcepub fn get_attr(
self,
attribute: &str,
heap: &'v Heap
) -> Result<Option<Value<'v>>>
pub fn get_attr( self, attribute: &str, heap: &'v Heap ) -> Result<Option<Value<'v>>>
Return the attribute with the given name.
sourcepub fn get_attr_error(
self,
attribute: &str,
heap: &'v Heap
) -> Result<Value<'v>>
pub fn get_attr_error( self, attribute: &str, heap: &'v Heap ) -> Result<Value<'v>>
Like get_attr
but return an error if the attribute is not available.
sourcepub fn has_attr(self, attribute: &str, heap: &'v Heap) -> bool
pub fn has_attr(self, attribute: &str, heap: &'v Heap) -> bool
Query whether an attribute exists on a type. Should be equivalent to whether
get_attr
succeeds, but potentially more efficient.
sourcepub fn dir_attr(self) -> Vec<String>
pub fn dir_attr(self) -> Vec<String>
Get a list of all the attributes this function supports, used to implement the
dir()
function.
sourcepub fn request_value<T: AnyLifetime<'v>>(self) -> Option<T>
pub fn request_value<T: AnyLifetime<'v>>(self) -> Option<T>
Request a value provided by StarlarkValue::provide
.
Trait Implementations§
source§impl<'v> AllocValue<'v> for Value<'v>
impl<'v> AllocValue<'v> for Value<'v>
source§impl<'v> Allocative for Value<'v>
impl<'v> Allocative for Value<'v>
source§impl Equivalent<FrozenValue> for Value<'_>
impl Equivalent<FrozenValue> for Value<'_>
source§fn equivalent(&self, key: &FrozenValue) -> bool
fn equivalent(&self, key: &FrozenValue) -> bool
key
and return true
if they are equal.source§impl Equivalent<Value<'_>> for FrozenValue
impl Equivalent<Value<'_>> for FrozenValue
source§fn equivalent(&self, key: &Value<'_>) -> bool
fn equivalent(&self, key: &Value<'_>) -> bool
key
and return true
if they are equal.source§impl<'v> Freeze for Value<'v>
impl<'v> Freeze for Value<'v>
§type Frozen = FrozenValue
type Frozen = FrozenValue
source§impl<'v> PartialEq for Value<'v>
impl<'v> PartialEq for Value<'v>
source§impl<'v> ProvidesStaticType<'v> for Value<'v>
impl<'v> ProvidesStaticType<'v> for Value<'v>
§type StaticType = Value<'static>
type StaticType = Value<'static>
source§impl<'v> StarlarkTypeRepr for Value<'v>
impl<'v> StarlarkTypeRepr for Value<'v>
source§fn starlark_type_repr() -> Ty
fn starlark_type_repr() -> Ty
source§impl<'v> UnpackValue<'v> for Value<'v>
impl<'v> UnpackValue<'v> for Value<'v>
source§fn unpack_value(value: Value<'v>) -> Option<Self>
fn unpack_value(value: Value<'v>) -> Option<Self>
Value
, try and unpack it into the given type, which may involve some element of conversion.source§fn unpack_value_err(value: Value<'v>) -> Result<Self>
fn unpack_value_err(value: Value<'v>) -> Result<Self>
None
if unpacking fails.source§fn unpack_param(value: Value<'v>) -> Result<Self>
fn unpack_param(value: Value<'v>) -> Result<Self>
None
return error about incorrect argument type.source§impl<'v> ValueLike<'v> for Value<'v>
impl<'v> ValueLike<'v> for Value<'v>
§type String = ValueTyped<'v, StarlarkStr>
type String = ValueTyped<'v, StarlarkStr>
StringValue
or FrozenStringValue
.source§fn from_frozen_value(v: FrozenValue) -> Self
fn from_frozen_value(v: FrozenValue) -> Self
FrozenValue
.source§fn downcast_ref<T: StarlarkValue<'v>>(self) -> Option<&'v T>
fn downcast_ref<T: StarlarkValue<'v>>(self) -> Option<&'v T>
None
if contained object has different type than requested.source§fn collect_repr(self, collector: &mut String)
fn collect_repr(self, collector: &mut String)
repr(x)
.source§fn write_hash(self, hasher: &mut StarlarkHasher) -> Result<()>
fn write_hash(self, hasher: &mut StarlarkHasher) -> Result<()>
source§fn invoke(
self,
args: &Arguments<'v, '_>,
eval: &mut Evaluator<'v, '_>
) -> Result<Value<'v>>
fn invoke( self, args: &Arguments<'v, '_>, eval: &mut Evaluator<'v, '_> ) -> Result<Value<'v>>
source§fn get_hashed(self) -> Result<Hashed<Self>>
fn get_hashed(self) -> Result<Hashed<Self>>
source§fn collect_str(self, collector: &mut String)
fn collect_str(self, collector: &mut String)
str(x)
.source§fn downcast_ref_err<T: StarlarkValue<'v>>(self) -> Result<&'v T>
fn downcast_ref_err<T: StarlarkValue<'v>>(self) -> Result<&'v T>
Err
if contained object has different type than requested.impl<'v> Coerce<Value<'v>> for FrozenValue
impl<'v, T: StarlarkValue<'v>> Coerce<Value<'v>> for FrozenValueTyped<'v, T>
impl<'v> Coerce<Value<'v>> for Value<'v>
impl<'v, T: StarlarkValue<'v>> Coerce<Value<'v>> for ValueTyped<'v, T>
impl<'v> CoerceKey<Value<'v>> for FrozenValue
impl<'v, T: StarlarkValue<'v>> CoerceKey<Value<'v>> for FrozenValueTyped<'v, T>
impl<'v> CoerceKey<Value<'v>> for Value<'v>
impl<'v, T: StarlarkValue<'v>> CoerceKey<Value<'v>> for ValueTyped<'v, T>
impl<'v> Copy for Value<'v>
impl Eq for Value<'_>
Auto Trait Implementations§
impl<'v> !RefUnwindSafe for Value<'v>
impl<'v> Send for Value<'v>
impl<'v> !Sync for Value<'v>
impl<'v> Unpin for Value<'v>
impl<'v> UnwindSafe for Value<'v>
Blanket Implementations§
source§impl<'a, T> AnyLifetime<'a> for Twhere
T: ProvidesStaticType<'a> + 'a + ?Sized,
impl<'a, T> AnyLifetime<'a> for Twhere
T: ProvidesStaticType<'a> + 'a + ?Sized,
source§fn static_type_id() -> TypeId
fn static_type_id() -> TypeId
TypeId
of Self
but where the lifetimes are changed
to 'static
. Must be consistent with static_type_of
.source§fn static_type_of(&self) -> TypeId
fn static_type_of(&self) -> TypeId
TypeId
of Self
but where the lifetimes are changed
to 'static
. Must be consistent with static_type_id
. Must not
consult the self
parameter in any way.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.