Enum exmex::Val[][src]

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

feature = "value" - The value type Val can contain an integer, float, bool, none, or error. To use the value type, there are the separate parse functions parse_val and parse_val_owned that wrap Express::from_regex and use the corresponding operator factory ValOpsFactory. 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());

Variants

Int(I)

Tuple Fields

0: I

Float(F)

Tuple Fields

0: F

Bool(bool)

Tuple Fields

0: bool

Error(ExError)

Tuple Fields

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The associated error which can be returned from parsing.

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

Returns the default operators.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.