Struct Expression

Source
pub struct Expression { /* private fields */ }

Implementations§

Source§

impl Expression

Source

pub fn new(string: &str, symbols: SymbolTable) -> Result<Expression, ParseError>

Compiles a new Expression. Missing variables will lead to a exprtk::ParseError.

§Example:

The above example melts down to this:

use exprtk_rs::*;

let mut symbol_table = SymbolTable::new();
symbol_table.add_variable("a", 2.).unwrap();
let mut expr = Expression::new("a + 1", symbol_table).unwrap();
assert_eq!(expr.value(), 3.);
Source

pub fn parse_vars( string: &str, symbols: SymbolTable, ) -> Result<(Expression, Vec<(String, usize)>), ParseError>

Compiles a new Expression like Expression::new. In addition, if unknown variables are encountered, they are automatically added an internal SymbolTable and initialized with 0.. Their names and variable IDs are returned as tuples together with the new Expression instance.

Source

pub fn handle_unknown<F>( string: &str, symbols: SymbolTable, func: F, ) -> Result<Expression, ParseError>
where F: FnMut(&str, &mut SymbolTable) -> Result<(), String>,

Handles unknown variables like Expression::parse_vars() does, but instead of creating a new SymbolTable, an existing one can be supplied, which may already have some variables defined. The variables are handled in a closure, which can register the names as variables, constants, strings or vectors to the supplied symbol table.

§Example
use exprtk_rs::*;

let formula = "s_string[] + a + b";
let mut expr = Expression::handle_unknown(formula, SymbolTable::new(), |name, sym| {
    if name.starts_with("s_") {
        sym.add_stringvar(name, "string").unwrap();
    } else {
        sym.add_variable(name, 1.).unwrap();
    }
    Ok(())
}).unwrap();

assert_eq!(expr.value(), 8.);

Note: this function is very flexible, but can cause problems if not registering anything for a given name. In that case, the same variable name will be brought up again and again, infinite loop, ultimately resulting in a stack overflow.

Source

pub fn value(&mut self) -> c_double

Calculates the value of the expression. Returns NaN if the expression was not yet compiled.

Note: This method requires mutable access to the underlying expression object, since executing an expression can have side-effects. Variables in the symbol table of the expression can be changed or added.

Source

pub fn symbols(&self) -> &SymbolTable

Returns a reference to the symbol table owned by the Expression

Source

pub fn symbols_mut(&mut self) -> &mut SymbolTable

Returns a mutable reference to the symbol table owned by the Expression

Trait Implementations§

Source§

impl Clone for Expression

Source§

fn clone(&self) -> Expression

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 Expression

Source§

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

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

impl Drop for Expression

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Expression

Source§

impl Sync for Expression

Auto Trait Implementations§

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