Struct starlark::values::Value

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

source

pub fn new_none() -> Self

Create a new None value.

source

pub fn new_bool(x: bool) -> Self

Create a new boolean.

source

pub fn new_frozen(x: FrozenValue) -> Self

Turn a FrozenValue into a Value. See the safety warnings on OwnedFrozenValue.

source

pub fn unpack_frozen(self) -> Option<FrozenValue>

Obtain the underlying FrozenValue from inside the Value, if it is one.

source

pub fn is_none(self) -> bool

Is this value None.

source

pub fn unpack_bool(self) -> Option<bool>

Obtain the underlying bool if it is a boolean.

source

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.

source

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.

source

pub fn unpack_str(self) -> Option<&'v str>

Obtain the underlying str if it is a string.

source

pub fn ptr_eq(self, other: Value<'_>) -> bool

Are two Values equal, looking at only their underlying pointer. This function is low-level and provides two guarantees.

  1. It is reflexive, the same Value passed as both arguments will result in true.
  2. If this function is true, then Value::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.

source

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:

  1. Calling it multiple times on the same Value will return ValueIdentity that compare equal.
  2. If two [Value] have ValueIdentity that compare equal, then Value::ptr_eq and Value::equals will also consider them to be equal.
source

pub fn get_type(self) -> &'static str

type(x).

source

pub fn to_bool(self) -> bool

bool(x).

source

pub fn at(self, index: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x[index].

source

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

source

pub fn length(self) -> Result<i32>

len(x).

source

pub fn is_in(self, other: Value<'v>) -> Result<bool>

other in x.

source

pub fn plus(self, heap: &'v Heap) -> Result<Value<'v>>

+x.

source

pub fn minus(self, heap: &'v Heap) -> Result<Value<'v>>

-x.

source

pub fn sub(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x - other.

source

pub fn mul(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x * other.

source

pub fn percent(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x % other.

source

pub fn div(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x / other.

source

pub fn floor_div(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x // other.

source

pub fn bit_and(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x & other.

source

pub fn bit_or(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x | other.

source

pub fn bit_xor(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x ^ other.

source

pub fn bit_not(self, heap: &'v Heap) -> Result<Value<'v>>

~x.

source

pub fn left_shift(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x << other.

source

pub fn right_shift(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

x >> other.

source

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.

source

pub fn get_type_value(self) -> FrozenStringValue

type(x).

source

pub fn add(self, other: Value<'v>, heap: &'v Heap) -> Result<Value<'v>>

Add two Values together. Will first try using radd, before falling back to add.

source

pub fn freeze(self, freezer: &Freezer) -> Result<FrozenValue>

Convert a value to a FrozenValue using a supplied Freezer.

source

pub fn to_str(self) -> String

Implement the str() function - converts a string value to itself, otherwise uses repr().

source

pub fn to_repr(self) -> String

Implement the repr() function.

source

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.

source

pub fn to_json_value(self) -> Result<Value>

Convert the value to JSON value.

source

pub fn set_attr(self, attribute: &str, alloc_value: Value<'v>) -> Result<()>

source

pub fn set_at(self, index: Value<'v>, alloc_value: Value<'v>) -> Result<()>

Forwards to StarlarkValue::set_at.

source

pub fn documentation(self) -> Option<DocItem>

source

pub fn iterate(self, heap: &'v Heap) -> Result<StarlarkIterator<'v>>

Produce an iterable from a value.

source

pub fn get_hashed(self) -> Result<Hashed<Self>>

Get the Hashed version of this Value.

source

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.

source

pub fn compare(self, other: Value<'v>) -> Result<Ordering>

How are two values comparable. For values of different types will return Err.

source

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.

source

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.

source

pub fn get_attr( self, attribute: &str, heap: &'v Heap ) -> Result<Option<Value<'v>>>

Return the attribute with the given name.

source

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.

source

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.

source

pub fn dir_attr(self) -> Vec<String>

Get a list of all the attributes this function supports, used to implement the dir() function.

source

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>

source§

fn alloc_value(self, _heap: &'v Heap) -> Value<'v>

Allocate the value on a heap and return a reference to the allocated value. Read more
source§

impl<'v> Allocative for Value<'v>

source§

fn visit<'allocative_a, 'allocative_b: 'allocative_a>( &self, visitor: &'allocative_a mut Visitor<'allocative_b> )

source§

impl<'v> Clone for Value<'v>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Value<'_>

source§

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

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

impl Default for Value<'_>

source§

fn default() -> Self

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

impl Display for Value<'_>

source§

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

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

impl<'v> Dupe for Value<'v>

source§

fn dupe(&self) -> Self

source§

impl Equivalent<FrozenValue> for Value<'_>

source§

fn equivalent(&self, key: &FrozenValue) -> bool

Compare self to key and return true if they are equal.
source§

impl Equivalent<Value<'_>> for FrozenValue

source§

fn equivalent(&self, key: &Value<'_>) -> bool

Compare self to key and return true if they are equal.
source§

impl<'v> Freeze for Value<'v>

§

type Frozen = FrozenValue

When type is frozen, it is frozen into this type.
source§

fn freeze(self, freezer: &Freezer) -> Result<FrozenValue>

Freeze a value. The frozen value must be equal to the original, and produce the same hash. Read more
source§

impl<'v> PartialEq for Value<'v>

source§

fn eq(&self, other: &Value<'v>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'v> ProvidesStaticType<'v> for Value<'v>

§

type StaticType = Value<'static>

Same type as Self but with lifetimes dropped to 'static. Read more
source§

impl<'v> Serialize for Value<'v>

source§

fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'v> StarlarkTypeRepr for Value<'v>

source§

fn starlark_type_repr() -> Ty

The representation of a type that a user would use verbatim in starlark type annotations
source§

impl<'v> Trace<'v> for Value<'v>

source§

fn trace(&mut self, tracer: &Tracer<'v>)

Recursively “trace” the value. Read more
source§

impl<'v> UnpackValue<'v> for Value<'v>

source§

fn expected() -> String

Description of values acceptable by unpack_value, e. g. list or str.
source§

fn unpack_value(value: Value<'v>) -> Option<Self>

Given a 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>

Unpack a value, but return error instead of None if unpacking fails.
source§

fn unpack_param(value: Value<'v>) -> Result<Self>

Unpack value, but instead of None return error about incorrect argument type.
source§

fn unpack_named_param(value: Value<'v>, param_name: &str) -> Result<Self>

Unpack value, but instead of None return error about incorrect named argument type.
source§

impl<'v> ValueLike<'v> for Value<'v>

§

type String = ValueTyped<'v, StarlarkStr>

StringValue or FrozenStringValue.
source§

fn to_value(self) -> Value<'v>

Produce a Value regardless of the type you are starting with.
source§

fn from_frozen_value(v: FrozenValue) -> Self

Convert from FrozenValue.
source§

fn downcast_ref<T: StarlarkValue<'v>>(self) -> Option<&'v T>

Get a reference to underlying data or None if contained object has different type than requested.
source§

fn collect_repr(self, collector: &mut String)

repr(x).
source§

fn write_hash(self, hasher: &mut StarlarkHasher) -> Result<()>

Hash the value.
source§

fn equals(self, other: Value<'v>) -> Result<bool>

x == other. Read more
source§

fn compare(self, other: Value<'v>) -> Result<Ordering>

x <=> other.
source§

fn invoke( self, args: &Arguments<'v, '_>, eval: &mut Evaluator<'v, '_> ) -> Result<Value<'v>>

Call this value as a function with given arguments.
source§

fn get_hashed(self) -> Result<Hashed<Self>>

Get hash value.
source§

fn collect_str(self, collector: &mut String)

str(x).
source§

fn downcast_ref_err<T: StarlarkValue<'v>>(self) -> Result<&'v T>

Get a reference to underlying data or Err if contained object has different type than requested.
source§

impl<'v> Coerce<Value<'v>> for FrozenValue

source§

impl<'v, T: StarlarkValue<'v>> Coerce<Value<'v>> for FrozenValueTyped<'v, T>

source§

impl<'v> Coerce<Value<'v>> for Value<'v>

source§

impl<'v, T: StarlarkValue<'v>> Coerce<Value<'v>> for ValueTyped<'v, T>

source§

impl<'v> CoerceKey<Value<'v>> for FrozenValue

source§

impl<'v, T: StarlarkValue<'v>> CoerceKey<Value<'v>> for FrozenValueTyped<'v, T>

source§

impl<'v> CoerceKey<Value<'v>> for Value<'v>

source§

impl<'v, T: StarlarkValue<'v>> CoerceKey<Value<'v>> for ValueTyped<'v, T>

source§

impl<'v> Copy for Value<'v>

source§

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<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T> AnyLifetime<'a> for T
where T: ProvidesStaticType<'a> + 'a + ?Sized,

source§

fn static_type_id() -> TypeId

Must return the 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

Must return the 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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

source§

impl<T> ToAst for T

source§

fn ast(self, begin: usize, end: usize) -> Spanned<Self>

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.