[][src]Struct arithmetic_eval::Environment

pub struct Environment<'a, T> { /* fields omitted */ }

Environment containing named Values.

Note that the environment implements the Index trait, which allows to eloquently access or modify environment. Similarly, IntoIterator / FromIterator / Extend traits allow to construct environments.

Examples

use arithmetic_eval::{Environment, Comparisons, Prelude, Value};

// Load environment from the standard containers.
let mut env: Environment<'_, f64> = Prelude.iter()
    .chain(Comparisons.iter())
    // Add a custom variable for a good measure.
    .chain(vec![("x", Value::Number(1.0))])
    .collect();

assert_eq!(env["true"], Value::Bool(true));
assert_eq!(env["x"], Value::Number(1.0));
for (name, value) in &env {
    println!("{} -> {:?}", name, value);
}

// It's possible to base an environment on other env, as well.
let other_env: Environment<'_, _> = env.into_iter()
    .filter(|(_, val)| val.is_function())
    .collect();
assert!(other_env.get("x").is_none());

Implementations

impl<'a, T> Environment<'a, T>[src]

pub fn new() -> Self[src]

Creates a new environment.

pub fn get(&self, name: &str) -> Option<&Value<'a, T>>[src]

Gets a variable by name.

pub fn iter(&self) -> impl Iterator<Item = (&str, &Value<'a, T>)> + '_[src]

Iterates over variables.

pub fn insert(&mut self, name: &str, value: Value<'a, T>) -> &mut Self[src]

Inserts a variable with the specified name.

pub fn insert_native_fn(
    &mut self,
    name: &str,
    native_fn: impl NativeFn<T> + 'static
) -> &mut Self
[src]

Inserts a native function with the specified name.

pub fn insert_wrapped_fn<Args, F>(
    &mut self,
    name: &str,
    fn_to_wrap: F
) -> &mut Self where
    FnWrapper<Args, F>: NativeFn<T> + 'static, 
[src]

Inserts a wrapped function with the specified name.

Calling this method is equivalent to wrapping a function and calling insert_native_fn() on it. Thanks to type inference magic, the Rust compiler will usually be able to extract the Args type param from the function definition, provided that type of function arguments and its return type are defined explicitly or can be unequivocally inferred from the declaration.

Trait Implementations

impl<T: Clone, '_> Clone for Environment<'_, T>[src]

impl<'a, T: Debug> Debug for Environment<'a, T>[src]

impl<T, '_> Default for Environment<'_, T>[src]

impl<'a, T, S, V> Extend<(S, V)> for Environment<'a, T> where
    S: Into<String>,
    V: Into<Value<'a, T>>, 
[src]

impl<'a, T, S, V> FromIterator<(S, V)> for Environment<'a, T> where
    S: Into<String>,
    V: Into<Value<'a, T>>, 
[src]

impl<'a, T, '_> Index<&'_ str> for Environment<'a, T>[src]

type Output = Value<'a, T>

The returned type after indexing.

impl<'a, T> IntoIterator for Environment<'a, T>[src]

type Item = (String, Value<'a, T>)

The type of the elements being iterated over.

type IntoIter = IntoIter<'a, T>

Which kind of iterator are we turning this into?

impl<'r, 'a, T> IntoIterator for &'r Environment<'a, T>[src]

type Item = (&'r str, &'r Value<'a, T>)

The type of the elements being iterated over.

type IntoIter = Iter<'r, 'a, T>

Which kind of iterator are we turning this into?

impl<T: PartialEq, '_> PartialEq<Environment<'_, T>> for Environment<'_, T>[src]

impl<'a, T: Clone> VariableMap<'a, T> for Environment<'a, T>[src]

Auto Trait Implementations

impl<'a, T> !RefUnwindSafe for Environment<'a, T>

impl<'a, T> !Send for Environment<'a, T>

impl<'a, T> !Sync for Environment<'a, T>

impl<'a, T> Unpin for Environment<'a, T> where
    T: Unpin

impl<'a, T> !UnwindSafe for Environment<'a, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

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