Value

Enum Value 

Source
pub enum Value {
    Float(Float),
    Integer(Integer),
    Complex(Complex),
    Boolean(bool),
    Unit,
    List(Rc<RefCell<Vec<Value>>>),
    Range(Box<Value>, RangeKind, Box<Value>),
    Function(Function),
}
Expand description

Represents any value that can be stored in a variable.

Variants§

§

Float(Float)

A floating-point value.

§

Integer(Integer)

An integer value.

§

Complex(Complex)

A complex number value.

§

Boolean(bool)

A boolean.

§

Unit

The unit type, analogous to () in Rust.

§

List(Rc<RefCell<Vec<Value>>>)

A list of values.

In cas-rs, a list is a reference to a vector of values. This is done to allow efficient cloning of lists, as well as mutation of lists in-place. References are passed around by default, which can result in somewhat confusing behavior, for example:

a = [1, 2, 3]
b = a
b[0] = 4
print(a) // prints [4, 2, 3]

TODO: In the future, a clone method may be added to cas-rs to allow the user to explicitly clone the list instead of copying the reference.

§

Range(Box<Value>, RangeKind, Box<Value>)

A range representing a sequence of values, either half-open or closed.

§

Function(Function)

A function.

Functions are treated as values just like any other value in cas-rs; they can be stored in variables, passed as arguments to other functions, and returned from functions.

Implementations§

Source§

impl Value

Source

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

Returns the typename of this value.

Source

pub fn coerce_float(self) -> Self

Consumes and attempts to coerce the value to a real number. Note that this coercion can be lossy if converting an arbitrary-precision integer to a fixed-width float. To preserve precision, see Value::coerce_integer and Value::coerce_number.

This conversion only occurs if one of the following is true:

  • The value is an integer.
  • The value is a complex number with a zero imaginary part.

This is useful for when evaluation of an expression results in a Value::Complex with a zero value for the imaginary part. Using a complex number for certain operators, such as the bitwise operators, will result in an error, so we will need to coerce those values to Value::Float instead.

Source

pub fn coerce_integer(self) -> Self

Consumes and attempts to coerce the value to an integer. This coercion is lossless.

This conversion only occurs if one of the following is true:

  • The value is a float with a zero fractional part.
  • The value is a complex number with a zero imaginary part, and a real part with a zero fractional part.
Source

pub fn coerce_number(self) -> Self

Consumes and attempts to coerce the value to a real number or an integer, preferring integers if possible. This coercion is lossless.

This conversion follows these rules:

  • If the value is an integer, it is returned as-is.
  • If the value is a float with a zero fractional part, it is converted to an integer. Otherwise, it is returned as-is.
  • If the value is a complex number with a zero imaginary part, either an integer or float is returned if the real part is an integer or float, respectively. Otherwise, it is returned as-is.
Source

pub fn coerce_complex(self) -> Self

Consumes and attempts to coerce the value to a complex number. This coercion is lossless.

Source

pub fn into_degrees(self) -> Self

Converts this value from radians to degrees. If it is a real number, it is converted as usual. If it is a complex number, the real and imaginary parts are converted separately.

Source

pub fn into_radians(self) -> Self

Converts this value from degrees to radians. If it is a real number, it is converted as usual. If it is a complex number, the real and imaginary parts are converted separately.

Source

pub fn is_float(&self) -> bool

Returns true if this value is a real number, or can be coerced to one.

Source

pub fn is_integer(&self) -> bool

Returns true if this value is an integer, or can be coerced to one.

Source

pub fn is_complex(&self) -> bool

Returns true if this value is a complex number, or can be coerced to one.

Source

pub fn is_boolean(&self) -> bool

Returns true if this value is a boolean.

Source

pub fn is_unit(&self) -> bool

Returns true if this value is a unit type.

Source

pub fn is_list(&self) -> bool

Returns true if this value is a list.

Source

pub fn is_truthy(&self) -> bool

Returns true if this value is truthy.

For each type, the following values are considered “truthy”:

  • Float: any value except 0.0 and NaN
  • Integer: any value except 0
  • Complex: any value except 0.0 + 0.0i and NaN + NaNi
  • Bool: true
  • Unit: never true; always false
  • List: lists with at least one element; element(s) does not have to be truthy
  • Range: ranges with at least one element; element(s) does not have to be truthy
  • Function: always true
Source

pub fn fmt(&self, options: FormatOptions) -> ValueFormatter<'_>

Returns a formatter for the value with the given options.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate 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 Display for Value

Source§

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

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

impl From<()> for Value

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl From<Complex> for Value

Source§

fn from(c: Complex) -> Self

Converts to this type from the input type.
Source§

impl From<Float> for Value

Source§

fn from(n: Float) -> Self

Converts to this type from the input type.
Source§

impl From<Integer> for Value

Source§

fn from(n: Integer) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(values: Vec<Value>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(n: i64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl !RefUnwindSafe for Value

§

impl !Send for Value

§

impl !Sync for Value

§

impl Unpin for Value

§

impl !UnwindSafe for Value

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Fmt for T
where T: Display,

Source§

fn fg<C>(self, color: C) -> Foreground<Self>
where C: Into<Option<Color>>, Self: Display,

Give this value the specified foreground colour.
Source§

fn bg<C>(self, color: C) -> Background<Self>
where C: Into<Option<Color>>, Self: Display,

Give this value the specified background colour.
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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

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

Source§

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§

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

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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.