pub struct Environment<T> { /* private fields */ }
Expand description

Environment containing named Values.

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

Examples

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

// Load environment from the standard containers.
let mut env = Environment::<f64>::new();
env.extend(Prelude::iter().chain(Comparisons::iter()));
// Add a custom variable for a good measure.
env.insert("x", Value::Prim(1.0));

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

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

Implementations§

source§

impl<T> Environment<T>where StdArithmetic: OrdArithmetic<T>,

source

pub fn new() -> Self

Creates a new environment.

source§

impl<T> Environment<T>

source

pub fn with_arithmetic<A>(arithmetic: A) -> Selfwhere A: OrdArithmetic<T> + 'static,

Creates an environment with the specified arithmetic.

source

pub fn get(&self, name: &str) -> Option<&Value<T>>

Gets a variable by name.

source

pub fn contains(&self, name: &str) -> bool

Checks if this environment contains a variable with the specified name.

source

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

Iterates over variables.

source

pub fn insert(&mut self, name: &str, value: Value<T>) -> &mut Self

Inserts a variable with the specified name.

source

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

Inserts a native function with the specified name.

source

pub fn insert_wrapped_fn<const CTX: bool, Args, F>( &mut self, name: &str, fn_to_wrap: F ) -> &mut Selfwhere FnWrapper<Args, F, CTX>: NativeFn<T> + 'static,

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§

source§

impl<T: Clone> Clone for Environment<T>

source§

fn clone(&self) -> Environment<T>

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<T: Debug> Debug for Environment<T>

source§

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

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

impl<T> Default for Environment<T>where StdArithmetic: OrdArithmetic<T>,

source§

fn default() -> Self

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

impl<T, S, V> Extend<(S, V)> for Environment<T>where S: Into<String>, V: Into<Value<T>>,

source§

fn extend<I: IntoIterator<Item = (S, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T> Index<&str> for Environment<T>

§

type Output = Value<T>

The returned type after indexing.
source§

fn index(&self, index: &str) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<'r, T> IntoIterator for &'r Environment<T>

§

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

The type of the elements being iterated over.
§

type IntoIter = Iter<'r, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for Environment<T>

§

type Item = (String, Value<T>)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq<Environment<T>> for Environment<T>

Compares environments by variables; arithmetics are ignored.

source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Environment<T>

§

impl<T> !Send for Environment<T>

§

impl<T> !Sync for Environment<T>

§

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

§

impl<T> !UnwindSafe for Environment<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.