Struct arithmetic_eval::env::Environment
source · pub struct Environment<T> { /* private fields */ }
Expand description
Environment containing named Value
s.
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>,
impl<T> Environment<T>where StdArithmetic: OrdArithmetic<T>,
source§impl<T> Environment<T>
impl<T> Environment<T>
sourcepub fn with_arithmetic<A>(arithmetic: A) -> Selfwhere
A: OrdArithmetic<T> + 'static,
pub fn with_arithmetic<A>(arithmetic: A) -> Selfwhere A: OrdArithmetic<T> + 'static,
Creates an environment with the specified arithmetic.
sourcepub fn contains(&self, name: &str) -> bool
pub fn contains(&self, name: &str) -> bool
Checks if this environment contains a variable with the specified name.
sourcepub fn insert(&mut self, name: &str, value: Value<T>) -> &mut Self
pub fn insert(&mut self, name: &str, value: Value<T>) -> &mut Self
Inserts a variable with the specified name.
sourcepub fn insert_native_fn(
&mut self,
name: &str,
native_fn: impl NativeFn<T> + 'static
) -> &mut Self
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.
sourcepub 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,
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 wrap
ping 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>
impl<T: Clone> Clone for Environment<T>
source§fn clone(&self) -> Environment<T>
fn clone(&self) -> Environment<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for Environment<T>
impl<T: Debug> Debug for Environment<T>
source§impl<T> Default for Environment<T>where
StdArithmetic: OrdArithmetic<T>,
impl<T> Default for Environment<T>where StdArithmetic: OrdArithmetic<T>,
source§impl<T, S, V> Extend<(S, V)> for Environment<T>where
S: Into<String>,
V: Into<Value<T>>,
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)
fn extend<I: IntoIterator<Item = (S, V)>>(&mut self, iter: I)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)