Enum exmex::Val

source ·
pub enum Val<I = i32, F = f64>
where I: DataType + PrimInt + Signed, F: DataType + Float,
{ Array(ArrayType<F>), Int(I), Float(F), Bool(bool), Error(ExError), None, }
Expand description

feature = "value" - The value type Val can contain an integer, float, bool, a vector of floats, none, or error. To use the value type, there are the is a parse function parse_val. In the following example, the ternary Python-style a if condition else b is used. This is equivalent to if condition {a} else {b} in Rust or condition ? a : b in C.

use exmex::{Express, Val};
let expr = exmex::parse_val::<i32, f64>("1.0 if x > y else 73")?;
assert_eq!(expr.eval(&[Val::Float(3.4), Val::Int(3)])?.to_float()?, 1.0);
assert_eq!(expr.eval(&[Val::Int(34), Val::Float(132.0)])?.to_int()?, 73);

Note that the ternary operator is actually implemented as two binary operators called if and else. To this end, we return Val::None from the if-operator if and only if the condition is false. On the flipside, this has strange side effects such as 5 else 3 being a valid expression evaluating to 5.

use exmex::Express;
let expr = exmex::parse_val::<i32, f64>("5 else 3")?;
assert_eq!(expr.eval(&[])?.to_int()?, 5);

We use the variant Error to report errors, since the trait Try is not yet stable.

use exmex::Express;
let expr = exmex::parse_val::<i32, f64>("fact(3.5)")?;
let res = expr.eval(&[])?;
assert!(format!("{:?}", res) == "Error(ExError { msg: \"did not expect Float(3.5)\" })");

When converting the value to the expected primitive type with to_int, to_float, or to_bool, the case Val::Error(ExError) is converted to ExResult::Err(ExError).

assert!(res.to_int().is_err());

Vectors can have arbitrary length and are represented as Val::Array(SmallVec<[F; 4]>). That is, a vector always consists of floats. Vectors of bools or ints do not exist. Further, 4 elements are stored on the stack, and if more are needed, they are stored on the heap. An example with vectors is shown in the following.

use exmex::Val;
use smallvec::smallvec;

// dot product of two vectors, one as parameter the other as literal
let expr = exmex::parse_val::<i32, f64>("dot(v, [1, 0, 0])")?;
let v = Val::Array(smallvec![3.0, 4.0, 2.0]);
let res = expr.eval(&[v])?;
assert!(res.to_float()? == 3.0);

// The following example shows how to get the second component of a vector
let expr = exmex::parse_val::<i32, f64>("(v + [1, 0, 0]).2")?;
let v = Val::Array(smallvec![3.0, 4.0, 2.0]);
let res = expr.eval(&[v])?;
assert!(res.to_float()? == 2.0);

Variants§

§

Array(ArrayType<F>)

§

Int(I)

§

Float(F)

§

Bool(bool)

§

Error(ExError)

Since the trait Try is experimental, we keep track of an error in an additional variant.

§

None

Sometimes, Val does not contain a value

Implementations§

source§

impl<I, F> Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float,

source

pub fn to_bool(self) -> ExResult<bool>

source

pub fn to_float(self) -> ExResult<F>

source

pub fn to_int(self) -> ExResult<I>

source

pub fn to_float_val(self) -> Self

source

pub fn to_array(self) -> ExResult<ArrayType<F>>

Trait Implementations§

source§

impl<I, F> Clone for Val<I, F>
where I: DataType + PrimInt + Signed + Clone, F: DataType + Float + Clone,

source§

fn clone(&self) -> Val<I, F>

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<I, F> Debug for Val<I, F>
where I: DataType + PrimInt + Signed + Debug, F: DataType + Float + Debug,

source§

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

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

impl<I, F> Default for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float,

source§

fn default() -> Val<I, F>

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

impl<I, F> From<f32> for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float,

source§

fn from(value: f32) -> Self

Converts to this type from the input type.
source§

impl<I, F> From<u8> for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float,

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl<I, F> FromStr for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float, <I as FromStr>::Err: Debug, <F as FromStr>::Err: Debug,

§

type Err = ExError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl<I, F> MakeOperators<Val<I, F>> for ValOpsFactory<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float, <I as FromStr>::Err: Debug, <F as FromStr>::Err: Debug,

source§

fn make<'a>() -> Vec<Operator<'a, Val<I, F>>>

Returns the default operators.

source§

impl<I, F> PartialEq for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float, <I as FromStr>::Err: Debug, <F as FromStr>::Err: Debug,

source§

fn eq(&self, other: &Val<I, F>) -> 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<I, F> PartialOrd for Val<I, F>
where I: DataType + PrimInt + Signed, F: DataType + Float, <I as FromStr>::Err: Debug, <F as FromStr>::Err: Debug,

source§

fn partial_cmp(&self, other: &Val<I, F>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

§

impl<I, F> Freeze for Val<I, F>
where I: Freeze, F: Freeze,

§

impl<I, F> RefUnwindSafe for Val<I, F>

§

impl<I, F> Send for Val<I, F>
where I: Send, F: Send,

§

impl<I, F> Sync for Val<I, F>
where I: Sync, F: Sync,

§

impl<I, F> Unpin for Val<I, F>
where I: Unpin, F: Unpin,

§

impl<I, F> UnwindSafe for Val<I, F>

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> 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<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> NeutralElts for T
where T: From<u8> + PartialEq,

source§

fn zero() -> T

source§

fn one() -> T

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, 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.
source§

impl<T> DataType for T
where T: Clone + FromStr + Debug + Default,

source§

impl<T> DiffDataType for T
where T: DataType + From<f32> + NeutralElts,